Manually implement UDP and TCP communication

Manually implement UDP and TCP communication

[[336022]]

UDP

UDP is a connectionless protocol, so the integrity of data cannot be guaranteed during transmission. JDK provides a network communication package, and provides two classes DatagramPacket and DatagramSocket under the java.net package to implement UDP communication. These two classes can be understood as follows: DatagramSocket is a road, and DatagramPacket is a car with people on it. In other words, DatagramSocket determines the communication target, and DatagramPacket represents the encapsulated data.

The characteristics of UDP determine the application scenarios of the UDP protocol. The most common application scenario around me is the communication between IoT devices and servers.

UDPClient Implementation

  1. public class UDPClient {
  2. public   static void main(String[] args) throws Exception {
  3. byte[] data = "I am a client, I sent a message" .getBytes();
  4. InetAddress address = InetAddress.getByName( "localhost" );
  5. DatagramPacket packet = new DatagramPacket(data,data.length,address,8888);
  6. DatagramSocket socket = new DatagramSocket();
  7. socket.send(packet);
  8.  
  9. byte[] message = new byte[1024];
  10. DatagramPacket packet1 = new DatagramPacket(message,message.length);
  11. socket.receive(packet1);
  12. String replyContent = new String(message,0,message.length);
  13. System. out .println( "UDPClient received the message: " +replyContent);
  14. socket.close () ;
  15. }
  16. }

UDPServer Implementation

  1. public class UDPServer {
  2. public   static void main(String[] args) throws Exception {
  3. DatagramSocket socket = new DatagramSocket(8888);
  4. byte[] data = new byte[1024];
  5. DatagramPacket packet = new DatagramPacket(data,data.length);
  6. socket.receive(packet);
  7. String message = new String(data,0,packet.getLength());
  8. System. out .println( "UDPServer received the message: " +message);
  9.  
  10. InetAddress address = packet.getAddress();
  11. int port = packet.getPort();
  12. byte[] replyContent = "I am the server, I replied to a message" .getBytes();
  13. DatagramPacket packet1 = new DatagramPacket(replyContent, replyContent.length, address, port);
  14. socket.send(packet1);
  15. socket.close () ;
  16. }
  17. }

TCP

TCP is a connection-oriented service. It establishes a connection through a three-way handshake and communicates by transmitting byte streams. Therefore, the integrity of the message can be guaranteed. Similarly, the java.net package also provides two packages, Socket and ServerSocket, to implement TCP communication.

TCPClient

  1. public class TCPClient {
  2.  
  3. public   static void main(String[] args) throws Exception {
  4.  
  5. Socket socket = new Socket( "localhost" ,8081);
  6. DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  7. String data = "I am a client and I sent a message" ;
  8. out .writeUTF(data);
  9.  
  10. DataInputStream in = new DataInputStream(socket.getInputStream());
  11. String message = in .readUTF();
  12. System. out .println( "TCPClient: " +message);
  13. socket.close () ;
  14. }
  15. }

TCPServer

  1. public class TCPServer {
  2.  
  3. public   static void main(String[] args) throws Exception{
  4. ServerSocket serverSocket = new ServerSocket(8081);
  5. Socket socket = serverSocket.accept();
  6. DataInputStream in = new DataInputStream(socket.getInputStream());
  7. String message = in .readUTF();
  8. System. out .println( "TCPServer: " +message);
  9.  
  10. DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  11. out .writeUTF( "I am the server, I replied a message" );
  12. socket.close () ;
  13. serverSocket.close () ;
  14. }
  15. }

This article is reprinted from the WeChat public account "Java Journey", which can be followed through the following QR code. To reprint this article, please contact the Java Journey public account.

<<:  Gartner: Global 5G network infrastructure spending will nearly double in 2020

>>:  The latest data on 5G construction is released: huge potential and promising future

Recommend

Strong partner ecosystem helps Denodo grow in Greater China

Beijing, March 10, 2021 - Denodo, a leader in dat...

Customized COSU equipment management solutions

Corporate Owned Single-Use Units (COSUs) are used...

5G is here! How long will it take for it to be truly universally adopted?

4G, the mobile network that allows us to make cal...

5G will revolutionize the Internet of Things, but not soon

5G technology is the most anticipated network upd...

How to ensure the reliability and number of nodes in CAN network communication

In CAN-bus circuit design, the transceiver can th...