Introduction

In the last article, we explained the classification of Sockets and the most commonly used Stream Sockets. Stream Sockets are generally based on the TCP protocol, so we can often see them in web services. Of course, the TCP protocol has a twin brother called UDP, so the socket protocol based on UDP as the transmission protocol is called Datagram Socket. Today we will learn more about Datagram Socket.

What is Datagram Socket

Unlike connected Stream Sockets, Datagram Sockets are connectionless. A connected Stream Socket indicates that the socket is stable and reliable, so we can perform stable data transmission in the Stream socket. Of course, this stability means that the data packet will not be lost, but it does not necessarily ensure that the data packet is not tampered with.

The connectionless Datagram Socket is usually used in scenarios that allow partial data loss, such as voice, video, etc. The advantage of connectionless is that it does not require the complicated steps of establishing a connection as TCP, so it is relatively simpler.

Datagram Socket usually uses the UDP protocol as the underlying data transmission protocol.

For UDP, because the UDP protocol itself does not guarantee the order of data and the handling of data exceptions, these need to be implemented in the application itself.

Common UDP applications include DNS (Domain Name System) service, NTP (Network Time Protocol) service and so on.

The encapsulation of Datagram Socket is provided in the java.net package of JDK, in which three connection states are defined:

 class DatagramSocket implements java.io.Closeable {
    ...
    static final int ST_NOT_CONNECTED = 0;
    static final int ST_CONNECTED = 1;
    static final int ST_CONNECTED_NO_IMPL = 2;
    ...
}

Indicates that the connection is not established, the connection is established and the connection is established, but it has not yet reached the level of implementation.

In addition, the DatagramSocket also contains a connection address and port:

 InetAddress connectedAddress = null;
int connectedPort = -1;

Use socat to create a UDP service

Note that before using the subsequent commands, you need to execute the installation command in the unix environment: yum install iproute2 netcat-openbsd socat

Like the previous Stream Socket, we can also use the socat command to create a UDP server. We need to use the following parameters of socat:

       udp4-listen:<port>        groups=FD,SOCKET,LISTEN,CHILD,RANGE,IP4,UDP
      udp6-listen:<port>        groups=FD,SOCKET,LISTEN,CHILD,RANGE,IP6,UDP

We need to monitor the data of udp4 and udp6, so two parameters udp4-listen and udp6-listen are used here.

The following port numbers can be customized. Here we still use the same port 8888. The corresponding commands are as follows:

 socat UDP4-LISTEN:8888,fork /dev/null&
socat UDP6-LISTEN:8888,ipv6only=1,fork /dev/null&

In the above command, we monitor the connection information of UDP4 and UDP6 on port 8888. The fork parameter indicates that the program will continue to run after receiving the package. If fork is not used, the program will automatically exit.

Socat was originally going to be followed by a bi-address. Here we use /dev/null, which means to discard all income information.

UDP6-LISTEN has a special parameter called ipv6only, which means that received packets should not be sent to IPv4-mapped IPv6 addresses.

What are IPv4-mapped IPv6 addresses? Simply put, it maps IPv4 to IPv6 addresses.

Executing the above command, we will get the following output:

 [1] 16174
[2] 16184

Because it is executed in the background, we return the ID of the process.

Use the ss command to monitor Datagram Sockets

The ss command can be used to check the status of the socket. Here we need to use the following parameters of ss:

    -4, --ipv4          display only IP version 4 sockets
   -u, --udp           display only UDP sockets
   -l, --listening     display listening sockets
   -n, --numeric       don't resolve service names

Because we only monitor ipv4 and ipv6 data, here we use the two parameters -4 and -6.

In addition, because you only need to listen to udp sockets, you need to use the -u parameter.

Because it is listening, the -l parameter is used. Finally, we want to see the specific number instead of being parsed into a service name, so the -n parameter is used here.

Let's use the following command to see the result:

 ss -4 -uln

The following results can be obtained:

 State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
UNCONN      0      0                                       *:8888                                                *:*  

The above command only listens to Ipv4, let's look at Ipv6:

 ss -6 -uln

The following results can be obtained:

 State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
UNCONN      0      0                                      :::8888                                               :::*  

Similar to Ipv4, it means that we are listening on port 8888 on Ipv6.

Use nc to establish a connection with UDP Socket

Now that we have established a server listening for UDP connections, let's try to connect using the nc command.

nc, short for Ncat, is a very small and efficient network tool. Let's take a look at the parameters that will be used in this example:

   -4                         Use IPv4 only
  -6                         Use IPv6 only
  -u, --udp                  Use UDP instead of default TCP
  -v, --verbose              Set verbosity level (can be used several times)
  -z                         Zero-I/O mode, report connection status only

The -4 and -6 parameters are required because it needs to connect to Ipv4 and Ipv6.

By default, nc uses the TCP protocol. If you want to use udp, you need to use the -u parameter.

In addition, we need to output detailed information, so the -v parameter is required. Finally, we directly establish the connection without sending any data, so the -z parameter is used here. Let's execute it to see the effect:

 nc -4 -u -vz 127.0.0.1 8888

Take a look at the output below:

 Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:8888.
Ncat: UDP packet sent successfully
Ncat: 1 bytes sent, 0 bytes received in 2.02 seconds.

Indicates that the UDP connection is successful.

Likewise, we can use the following command to connect to a UDP socket:

 nc -6 -u -vz ::1 8888

Where ::1 represents the ipv6 address of the machine.

The following results can be obtained:

 Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ::1:8888.
Ncat: UDP packet sent successfully
Ncat: 1 bytes sent, 0 bytes received in 2.02 seconds.

Indicates that the UDP connection is successful.

Summarize

This article explains the basic concepts of datagram socket, and uses some basic unix commands to build a udp server and client, which is convenient for everyone to understand.

This article has been included in http://www.flydean.com/16-datagram-socket/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com