Introduction to the

Whether in common network programming or in Netty, there is often a word called socket, as if socket is a magical thing, we can use socket to establish client-side connection to the server, and conduct communication between the client and the server, so what is socket in the end? What are its categories? Take a look.

What is a Socket

The Chinese translation of socket is socket, which I think is really bad. Socket sounds meaningless, so many people are confused when they first hear the word socket.

So what is a socket? Socket is a method for different programs to communicate with each other. These programs can run on the same or different servers.

Socket establishes connections based on the IP protocol. The IP protocol is used to encapsulate and group data before it can be transmitted on the network. The SOCKET that relies on the IP protocol is also called a network socket.

A network socket is used to establish a connection between the client and server. The client and server discover each other through the socket address.

Using Java as an example, let’s look at the SocketAddress definition:

public abstract class SocketAddress implements java.io.Serializable {

    static final long serialVersionUID = 5215720748342549866L;

}
Copy the code

SocketAddress is just a general definition. It can be implemented in many ways, including transport protocols such as TCP or UDP. It also needs to contain an IP address and a connected port.

The IP address and port define the connection object, and the protocol defines the connection mode.

Different types of sockets can be derived based on different protocols. For example, TCP dependent Stream Sockets, UDP dependent Datagram Sockets, Unix Domain Sockets that rely on local files for data transfer.

We’ll look at the use of these protocols in detail on a Unix system.

Before going into detailed examples, we need to use the network commands, which are SS, NC, and SOcat.

In this article, I am using a centOS system, so you can use the following commands to install it:

yum install iproute2 netcat-openbsd socat
Copy the code

Stream Socket

What is a Stream Socket? This Socket connection is used for streaming. If streaming is to be transmitted, a stable network connection needs to be established first. Transmission Control Protocol (TCP) is undoubtedly the most commonly used and highly efficient Protocol.

For the Stream Socket, it is directed. The data package needs to be transmitted from one address to another address over the network, and also needs to receive the processing result from the other address. In this process, TCP protocol is usually used.

TCP ensures data stability and order. TCP packets ensure the sequence of packets sent to physical network interfaces. If the packets received by the network interface are out of order, the network adapter and the operating system ensure that they are reassembled in the correct order for use by the application.

Nginx or Apache provides the following Stream Socket addresses:

124.225.141.53:80
124.225.141.53:443
Copy the code

I used the IP address of NetEase above, where 80 stands for HTTP and 443 stands for HTTPS.

Create a TCP server using SOcat

Common TCP servers can be Apache or Nginx, but for the sake of simplicity, we will use SOcat to create a TCP server.

So what is socat? It is short for SOcket CAT and can be used to simulate a TCP server.

Socat commands are very complex, so here is a brief overview of their applications:

socat -h
socat by Gerhard Rieger and contributors - see www.dest-unreach.org
Usage:
socat [options] <bi-address> <bi-address>
Copy the code

As you can see from the above results, SOcat can accept some addresses and then add some options.

Here we use Socat to create two connections, TCP6 and TCP4. Socat has two options to do this:

      tcp-connect:<host>:<port> groups=FD,SOCKET,CHILD,RETRY,IP4,IP6,TCP
      tcp-listen:<port> groups=FD,SOCKET,LISTEN,CHILD,RETRY,RANGE,IP4,IP6,TCP
      tcp4-connect:<host>:<port>        groups=FD,SOCKET,CHILD,RETRY,IP4,TCP
      tcp4-listen:<port>        groups=FD,SOCKET,LISTEN,CHILD,RETRY,RANGE,IP4,TCP
      tcp6-connect:<host>:<port>        groups=FD,SOCKET,CHILD,RETRY,IP6,TCP
      tcp6-listen:<port>        groups=FD,SOCKET,LISTEN,CHILD,RETRY,RANGE,IP6,TCP
Copy the code

Here we only need to set up two services that listen on TCP, so we use the following command:

socat TCP4-LISTEN:8888,fork /dev/null&
socat TCP6-LISTEN:8888,ipv6only=1,fork /dev/null&
Copy the code

The above command, we listen on port 8888 TCP4 and TCP6 connection information, including the fork parameter indicates the program continues to run after receive the package, if it’s not the fork, the program will automatically exit.

/dev/null: discard all income information. /dev/null: discard all income information

Tcp6-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? In a nutshell, IPv4 is mapped to an IPv6 address.

Executing the above command, we get the following output:

[1] 30877
[2] 30885
Copy the code

Since it is background execution, we return the process ID.

Use SS to check TCP connections

Ss is a very powerful command, we can use ss to monitor TCP sockets information, it is used as follows:

ss -h
Usage: ss [ OPTIONS ]
       ss [ OPTIONS ] [ FILTER ]
Copy the code

Let’s focus on the following parameters to be used:

   -4, --ipv4          display only IP version 4 sockets
   -6, --ipv6          display only IP version 6 sockets
   -t, --tcp           display only TCP sockets
   -l, --listening     display listening sockets
  -n, --numeric       don't resolve service names
Copy the code

Since we only listen for ipv4 and ipv6 data, we use the -4 and -6 parameters.

Also, since only TCP sockets need to be listened for, use the -t parameter.

Because we are listening, we use the -l argument, and at the end we want to see the actual number rather than being resolved to the service name, so we use the -n argument.

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

ss -4 -tln
Copy the code

The results are as follows:

State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              

LISTEN      0      5                                       *:8888                                                *:*  
Copy the code

Port 8888 is being listened on, but if there are other processes on your server, there may be more than this data.

The command above only listens for Ipv4, let’s look at Ipv6:

ss -6 -tln
Copy the code

You might get the following output:

ss -6 -tln
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              

LISTEN      0      5                                      :::8888                                               :::* 
Copy the code

Similar to Ipv4, we’re listening on port 8888 on Ipv6.

Use NC to connect the socket

Now that we have set up the server to listen for TCP connections, let’s try to connect using the NC command.

Nc is short for Ncat, which is a very useful network tool that can do a lot of things. Let’s look at the parameters used in this example:

  -4                         Use IPv4 only
  -6                         Use IPv6 only
  -v, --verbose              Set verbosity level (can be used several times)
  -z                         Zero-I/O mode, report connection status only
Copy the code

Since you need to connect to Ipv4 and Ipv6, the -4 and -6 parameters are required.

In addition, we need to output detailed information, so we need the -v parameter. Finally, we establish the connection directly, and do not send any data, so we use the -z parameter.

Nc-4-vz 127.0.0.1 8888Copy the code

Take a look at the following output:

Nc-4-vz 127.0.0.1 8888 Ncat: Version 7.50 (https://nmap.org/ncat) Ncat: Connected to 127.0.0.1:88888.ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.Copy the code

You can see that nc successfully established the connection and sent 0 bytes of content.

Similarly, we establish a connection to Ipv6:

nc -6 -vz ::1 8888
Copy the code

Here ::1 represents the local Ipv6 address. The following output is displayed:

Ncat: Version 7.50 (https://nmap.org/ncat) Ncat: Connected to :: 1:888.ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.Copy the code

conclusion

Here, we introduced the basic classification of Socket Stream Socket meaning, and the use of Unix tools to build a Socket server and client, of course, this is only the most simple description of the description, we can experience the Stream Socket process.

This article is available at www.flydean.com/15-stream-s…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!