Java uses Socket for network communication
The following shows a program that uses a client to send a message to the server. After receiving the message, the server prints it on the control panel.
1) the client sends data to the server
2) The server, after receiving the data, prints it on the control panel
1. Main steps of the client:
1) create a udp Service. Use the initramsocket object.
2) create and encapsulate data into data packets.
3) use the Socket service to send existing data packets. By sending.
4) Close the resource.
The code for sending data to the client is as follows:
Package udp. send. qdj; import java.net. datagramPacket; import java.net. datagramSocket; import java.net. inetAddress; // Udp sender public class CUdpsend {public static void main (String [] args) throws Exception {// 1. create a udp Service. Use the initramsocket object. DatagramSocket ds = new DatagramSocket (1234); // 2. Create and encapsulate data into data packets. // DatagramPacket (byte [] buf, int length, InetAddress address, int port) // constructs a datagram packet to send the packet with length to the specified port number on the specified host. Byte [] buf = udp send from Server. getBytes (); DatagramPacket dp = new DatagramPacket (buf, buf. length, InetAddress. getByName (10.100.56.210), 10000); // 3. use the Socket service to send existing data packets. By sending. Ds. send (dp); // 4. close resource ds. close ();}}
2. The main steps on the server side are as follows:
1) create a Udp Socket and an endpoint. Listening to a port is actually a digital identifier for the Network Program, which is convenient and clear and which data can be processed by the application.
2) define data packets. Used to store data.
3) store the received data into the data packet through the receive method of the service.
4) obtain the data in the data packet.
5) Close the resource.
The server receives and prints the data on the control panel. The Code is as follows:
Package udp. rece. qdj; import java.net. datagramPacket; import java.net. datagramSocket; // Udp acceptor public class CUdprece {public static void main (String [] args) throws Exception {// 1. create a Udp Socket and an endpoint. Listening to a port is actually a digital identifier for the network program, // convenient and clear, which data can be processed by the application. DatagramSocket ds = new DatagramSocket (10000); // 2. Define data packets. Used to store data. Byte [] buf = new byte [1024]; DatagramPacket dp = new DatagramPacket (buf, buf. length); // 3. Use the receive method of the Service to save the received data to the data packet. // This method is a blocking method. If no data is accepted, the method is blocked. Ds. receive (dp); // 4. Obtain the data in the data packet. // Obtain the ip address String ip = dp. getAddress (). getHostAddress (); // get the transmitted data String data = new String (dp. getData (), 0, dp. getLength (); // obtain the port number int port = dp. getPort (); System. out. println (ip Address: + ip + sent content data: + data + port: + port); // 5. disable resource ds. close ();}}Running result display:
References: Lecture on Java video Bi Xiangdong
Address: http://blog.csdn.net/qingdujun/article/details/39300293