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

What exactly is fast charging?

1. Definition of fast charging [[343166]] There i...

5G is not here yet, but it is within reach

5G is currently the most eye-catching new technol...

A Preliminary Study on Software Defined Network (SDN)

【51CTO.com Quick Translation】Before 2008, the ent...

Thirty years of changes and evolution of Internet core protocols

When the Internet began to be widely used in the ...

Revolutionizing Networking with Edge Computing

Compared with cloud computing, edge computing foc...

6G will usher in a new era for all industries

At this early stage, 6G wide-area wireless has fe...

AT&T requires all hardware vendors to support Open RAN specifications

According to Light Reading, executives of US tele...

Perhaps it is easier to understand HTTPS this way

We won’t talk about HTTP and HTTPS first. Let’s s...

VIAVI attended the 19th China Optical Network Symposium and China FTTH Forum

Beijing, China, June 13, 2019 - VIAVI Solutions (...