Java UDP connection

1. In the Server DatagramPacket used to represent data or information. Referring to the specific configuration method API
2.DatagramSocket port disposed first step, the second step receives DatagramPacket
3. In the Client, DatagramSocket DatagramPacket for transmitting
code is as follows:
. Import the java.net *;

public class UdpServer {
public static void main(String[] args) throws Exception {
byte [] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
DatagramSocket dgp = new DatagramSocket(9999);

	while(true) {
		dgp.receive(dp);
		System.out.println(new String(buf,0,dp.getLength()));	
	}
	
}

}
import java.net.*;

public class UdpClient {
public static void main(String[] args) throws Exception {
byte [] buf = (new String(“康楚明”)).getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress(“127.0.0.1”,5666));
DatagramSocket ds = new DatagramSocket(3333);

	ds.send(dp);
	ds.close();	
	
}

}

Published 12 original articles · won praise 0 · Views 164

Guess you like

Origin blog.csdn.net/Valentine95/article/details/104754198