Socket communication with TCP server[STM32Nucleo]
Simply making MCU Ethernet-enabled and joining the network with ping passing through it does not mean anything, but by installing protocols such as TCP and UDP in the upper transport layer, communication between the server and the client becomes possible.
Socket communication is a means of communication according to the TCP protocol, which belongs to the transport layer of the OSI reference model described in the previous article. To realize TCP socket communication with MCU equipped with the W5500 controller, the built-in TCP/IP protocol stack is manipulated with registers. The operation itself is easy since the manufacturer provides a driver.
Once a TCP server is built and a socket communication implementation is in place, data can be communicateed like serial communication at any given time.
Table of contents
What is TCP Socket Communication?
Since there are countless articles explaining socket communication on the Internet, I will not go into details here, but to summarize briefly, socket communication is a method based on the TCP protocol in which the server and client communicate while mutually checking each other. It is also called the handshake method because of the mutual exchange of information.
Socket communication is similar to a telephone conversation, with the socket being the equivalent of the receiver, ear, and mouth, which are the entry and exit points of the conversation. A conversation (communication) is established only when the other party responds, and the conversation is terminated when the other party or we disconnect in the middle of the conversation. When the other party is connected (with the receiver in the up position), the receiver does not respond to our request for connection.
Other protocols belonging to the transport layer include UDP, which is a method in which the server gives information unilaterally without checking with the client. It is suitable for sending large amounts of information without any problems even if there are some imperfections in the content, such as streaming video.
The figure below shows a flowchart of communication based on Berkeley Sockets (an API for networking implemented in BSD unix), which is the standard for socket communication. Both the server and the client create sockets in advance, and the client makes a connection request when necessary, and the server responds to the request to establish communication. Once established, socket communication is characterized by the ability to send and receive at any time without disconnection from either side.
TCP server implementation
The driver soket.c provides a set of functions that conform to the standard BSD socket interface, here is a sample of socket communication using only some of the functions defined in the driver.
In the sample program, the TCP server is configured as a dedicated task prvTask_Tcp that runs with a sampling time of 5 ms.
Although a generic TCP server can be created using the driver's standard socket functions, here we use the driver's register operator functions to perform processing according to the socket state monitored at each sampling time.
Socket register block settings
① Monitor socket status
state = getSn_SR(SN)；
The states of the socket are mainly following 4 types;
and these states are read by the getSn_SR(SN) function according to the socket number SN, and the socket's register values are read by the getSn_SR(SN) function.
② Socket Generation
srcSocket = socket(SN,Sn_MR_TCP, PORT, SF_IO_NONBLOCK)；
The socket state after program startup is "SOCK_CLOSED", so the socket is first created with socket().
The first argument of the function is the socket number SN, which is defined as 1 in the header file net_conf.h, a collection of self-made functions.
The second argument specifies the protocol is the TCP protocol. The third argument specifies the PORT number, and the fourth argument specifies non-block, making it a non-blocking socket.
③ Transition to STABE_LISTEN
When the socket is created, listen(SN) is executed and the socket shifts to the SOCK_LISTEN state, waiting for a connection from the client.
When the socket is in SOCK_LISTEN state, a connection request is made from the client terminal specifying the IP address and port, and when the connection is established, the socket is SOCK_ESTABLISHED. Once the connection is established and the IP address of the server is displayed on the client terminal, the client can send and receive at any time.
④ Data Send and Receive
len=recv(SN, (uint8_t*)rcvbuffer, sizeof(rcvbuffer));
Use recv() to receive data. The first argument is the socket number, the second argument is a pointer to the receive buffer, and the third argument is the data length. When data is received, the data length is returned.
send(SN, (uint8_t*)sendbuffer, sizeof(sendvbuffer));
Use send() to send data. The first argument is the socket number, the second argument is a pointer to the send buffer, and the third argument is the data length.
The transmit TX and receive RX buffers, which are the buffer memory for communication, consist of blocks of 16 Kbytes each.
⑤ Socket Control Functions
Sn_CR() is a function defined in socket.c to control the socket. The first argument is the socket number and the second argument is the state of the socket; a value of Sn_CR_CLOSE closes the socket.
Other sockets include "OPEN", "LISTEN", "CONNECT", "DISCON", "SEND", "SEND_MAC", "SEND_KEEP", and "RECV". Check the driver socket definition file socket.c for details.
The socket state transitions of the TCP server are summarized in the figure above; there are other socket states in the W5500, but for now, the four main states can be controlled to function.
Image of Socket Communication
The state of socket communication can be compared to a telephone conversation, as shown in the figure below. Let's check the operation by actually performing socket communication while assuming this state of communication.
Actual socket communication
The client uses terminal emulator. Again, Tera Term is used here. Use any tool you are familiar with, not just this one.
After startup, enter the IP address and port number registered with the server for TCP socket communication and press "OK".
① Connection request
When the IP address of the server appears in the upper left corner of the screen, the configuration port is open and the client and server are connected.
② Socket connection being established
Verify that communication is possible by giving commands to the TCP server. A file is created and executed with commands to return a response message for communication test, close the socket (close), and disconnect (disconnect).
First, enter the string "ABC". If the string stored in the receive buffer matches "ABC", the response is "Matched ministry". The response is sent using send().
If the string "abc" is entered, it will not match "ABC" and an "Error" will be returned.
A new connection is attempted where the socket has established a connection.
Connection refused because the port is already open. On the phone, the other party's receiver is up and talking.
③ Socket close
To close the socket, enter the command "scls", which returns a "socket close" message and sends Sn_CR_CLOSE to the CR register before closing the socket. As a result, the socket is not connected.
④ Connection request + socket connection established
When a new connection is tried again, this time the socket is closed, so the port opens again and a connection can be made.
Finally, to disconnect the connection itself, enter the command "srst", which sends Sn_CR_DISCON to the CR register and performs a disconnect operation (disconnect).
The upper left corner of the screen is unconnected, indicating that the connection has been interrupted.
The above is a demonstration of socket communication by entering test commands. In actual socket communication, the commands can be further diversified depending on the programming, and various processes can be performed by the commands.
TCP socket communication is characterized by the ability to exchange data between servers and clients in real time, so if a TCP server is installed in an embedded device, it will be possible to remotely manage data via the Internet in real time as IoT.