UDP socket编程_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > UDP socket编程

UDP socket编程

 2012/1/5 14:00:04  hbiao68  http://hbiao68.iteye.com  我要评论(0)
  • 摘要:服务器端publicclassUDPServer{publicstaticvoidmain(String[]args)throwsException{byte[]buf=newbyte[100];DatagramPacketdatagramPacket=newDatagramPacket(buf,buf.length);DatagramSocketdatagramSocket=newDatagramSocket(9995);while(true){datagramSocket.receive
  • 标签:编程 socket

服务器端

public class UDPServer {

	public static void main(String[]args) throws Exception{
		
		byte[] buf = new byte[100];
		
		DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
		
		DatagramSocket datagramSocket = new DatagramSocket(9995);
		
		while(true){
			datagramSocket.receive(datagramPacket);
			System.out.println("data  is :" + new String(datagramPacket.getData()));
		}
	}
}

?

客户端

public class UDPClient {

	public static void main(String[] args) throws Exception {
		byte[] buf = new String("UDP TEST data from client").getBytes(); ;
		DatagramPacket datagramPacket = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),9995);
		DatagramSocket datagramSocket = new DatagramSocket();
		datagramSocket.send(datagramPacket);
	}
}

?

发表评论
用户名: 匿名