黑马程序员技术交流社区
标题:
udp接收
[打印本页]
作者:
fmi110
时间:
2015-9-18 23:36
标题:
udp接收
a
package udp.communication;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receive {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 建套接字对象
DatagramSocket ds = new DatagramSocket(10086);
// 建包对象
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
// 解析数据包
while(true){
ds.receive(dp);
String line = new String(dp.getData(),0,dp.getLength());
System.out.println("from"+dp.getAddress().getHostAddress()+" :"+line);
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2