Wiznet makers

ronpang

Published August 26, 2025 © Apache License 2.0 (Apache-2.0)

132 UCC

75 WCC

32 VAR

0 Contests

1 Followers

0 Following

Hardwired TCP/IP Chapter 25: W55MH32 TCP_Server_Multi_Socket Example

Hardwired TCP/IP Chapter 25: W55MH32 TCP_Server_Multi_Socket Example

COMPONENTS
PROJECT DESCRIPTION

Hardwired TCP/IP Chapter 25: W55MH32 TCP_Server_Multi_Socket Example

In this article, we will provide a detailed explanation on how to implement TCP communication on the W55MH32 chip. By using the TOE engine of W55MH32, we only need to perform simple socket programming and register reading and writing, and we can easily implement TCP protocol applications. Next, through practical examples, we will explain to you how to use the 8 sockets on the TOE engine, set them to TCP Server mode, and allow multiple clients to connect for data loopback testing.

Other network protocols used in this routine, such as DHCP, please refer to the relevant sections. Regarding the initialization process of W55MH32, please refer to the Network Install section. We will not elaborate on this here.

1 Introduction to the TCP Protocol

TCP (Transmission Control Protocol) is a connection-oriented and reliable transport layer protocol, which is used to reliably transmit data over the network. TCP is one of the core protocols in the Internet protocol family and is usually used together with the IP protocol (Internet Protocol) to form socket communication.

2 Characteristics of the TCP Protocol

  • Connection-oriented: Before transmitting data, TCP needs to establish a connection to ensure that the sender and the receiver can communicate with each other. The connection is established through the Three-Way Handshake process, ensuring reliable communication between the two parties.
  • Reliability: TCP provides reliable data transmission, ensuring that the data is complete and arrives at the receiving end in order. If data is lost or corrupted, TCP will automatically retransmit the lost data packets.
  • Flow Control: TCP uses a flow control mechanism to regulate the speed of data transmission, preventing the receiving end from being overwhelmed and resulting in data loss. The commonly used flow control method is Sliding Window.
  • Congestion Control: TCP can dynamically adjust the transmission rate to avoid network congestion. Algorithms such as Slow Start, Congestion Avoidance, and Fast Retransmission are adopted.
  • Full-duplex Communication: After the TCP connection is established, data can be transmitted simultaneously in both directions, supporting two-way communication.
  • Ordered Data Transmission: TCP numbers the data packets to ensure that the data is transmitted in order, even if there is network delay, the receiving end can receive the data in order.
  • Byte Stream Service: The data transmitted by TCP is a byte stream, it does not care about the boundaries of the application layer data, and the application layer needs to parse the data boundaries itself.

3 The differences between TCP and UDP

  • TCP (Transmission Control Protocol) is a reliable, connection-oriented protocol, suitable for applications that require data integrity and sequence guarantee, such as web browsing and file transfer.
  • UDP (User Datagram Protocol) is an unconnected, unreliable protocol, suitable for applications that have high requirements for timeliness and can tolerate packet loss, such as video streaming and online games.

4 TCP application scenarios

Next, let's explore what operations and applications can be accomplished using the TCP protocol on the W55MH32?

  • Remote monitoring and data collection: Embedded devices are typically used to collect sensor data and upload it to a remote server via Ethernet. The TCP protocol ensures the reliability and integrity of data transmission.
  • Remote device control: Many embedded systems need to receive control instructions over the network (such as PLC control in industrial automation), and the TCP protocol provides a reliable communication channel.
  • Internet of Things (IoT): Many IoT devices use the TCP protocol to communicate with cloud servers or other devices, transmitting data, executing commands, and so on.
  • Embedded Web server: Some embedded devices have built-in Web servers (such as routers, gateways, sensor devices, etc.), and through the TCP protocol, they provide web interfaces for users to configure and monitor.

5 The process of conducting data interaction using TCP

TCP connection establishment (three-way handshake)

Before starting to transmit data, TCP establishes the connection through three handshakes:

1. The first handshake: The client sends a data packet with the SYN flag to the server, indicating a request to establish a connection.

2. The second handshake: After receiving the SYN data packet, the server replies with a data packet containing the SYN and ACK flags, indicating agreement to establish the connection.

3. The third handshake: After receiving the SYN+ACK from the server, the client sends a data packet with the ACK flag, completing the connection establishment. 

 

Data interaction 

 

TCP connection termination (four-way handshake)

When communication ends, TCP needs to terminate the connection through a four-way handshake:

1. The first handshake: The client sends a FIN data packet, indicating that data transmission is complete and it is ready to close the connection.

2. The second handshake: After receiving the FIN data packet from the server, the server replies with an ACK data packet, indicating its agreement to close the connection.

3. The third handshake: The server sends a FIN data packet, indicating that data transmission is complete and it is ready to close the connection.

4. The fourth handshake: After receiving the server's FIN data packet, the client sends an ACK data packet, and the connection is officially closed.

TCP Three-way Handshake Diagram

TCP Four-way Handshake Diagram

6 The ACK mechanism, retransmission mechanism and Keepalive mechanism of TCP

The ACK mechanism of TCP

ACK is a mechanism used by TCP to confirm that data packets have been successfully received. In TCP communication, each data packet contains a sequence number, and the receiver uses ACK to inform the sender that it has successfully received the byte sequence.

1. ACK field: The ACK is included in the TCP header and indicates the next byte sequence number that the receiver expects to receive.

2. Cumulative confirmation: TCP uses the cumulative confirmation method, indicating that the receiver has received all data up to a certain sequence number consecutively.

3. Timeout retransmission: If the sender does not receive ACK within the timeout period, it will retransmit the data packet.

The retransmission mechanism of TCP

The retransmission mechanism of TCP ensures reliable data transmission. The following are common retransmission mechanisms:

1. Timeout Retransmission:

  • The sender sets a timer. If the sent data packet does not receive an ACK within the specified time, it triggers retransmission.
  • The timeout period is dynamically adjusted and estimated based on the TCP's round-trip time (RTT).

2. Fast Retransmission:

  • When the receiver detects a lost data packet, it sends a duplicate ACK (referred to as redundant ACK) to remind the sender that a certain data packet has not arrived.
  • If the sender receives three consecutive duplicate ACKs, it will immediately retransmit the corresponding data packet instead of waiting for the timeout.

3. Selective Retransmission (Selective Repeat, SACK):

  • Based on cumulative acknowledgments, TCP can also use the SACK option to inform the sender which specific blocks have been received and which have not.
  • This can reduce unnecessary retransmissions and improve efficiency.

TCP Keepalive Mechanism

TCP Keepalive is an optional mechanism of the TCP protocol used to detect whether a long-time idle connection is still valid. Its main functions are:

1. Maintaining connection status: Detecting whether the remote host is still online to avoid resources being occupied for a long time.

2. Releasing dead connections: If the connection has failed (such as network interruption or the remote host crashing), Keepalive can release resources in time.

3. Preventing intermediate devices from timing out and closing the connection: Some NAT, routers, or firewalls may automatically close the connection when it is idle for a long time. Keepalive can prevent this situation.

Usage: In the TOE engine of W55MH32, the Keepalive time needs to be set in the Sn_KPALVTR register, and then a data packet needs to be sent after successfully connecting to the server to activate Keepalive.

7 The implementation process

Next, we implement a TCP server on the W55MH32 to enable multiple devices to connect and communicate data.

Note: The test instance requires that the PC and W55MH32 be on the same network segment.

First, call the multi_tcps_socket() function in the main loop:

1. while (1)
2. {
3.    multi_tcps_socket(ethernet_buf, local_port);
4. } 

The two parameters passed to the multi_tcps_socket() function are the interaction cache array and the local port number. The specific content of the function is as follows:

 1. int32_t multi_tcps_socket(uint8_t *buf, uint16_t localport)
 2. {
 3.     int32_t  ret;
 4.     uint16_t size = 0, sentsize = 0;
 5. 
 6. #ifdef _LOOPBACK_DEBUG_
 7.     uint8_t  destip[4];
 8.     uint16_t destport;
 9. #endif
10. 
11.     switch (getSn_SR(socket_sn))
12.     {
13.     case SOCK_ESTABLISHED:
14.         if (getSn_IR(socket_sn) & Sn_IR_CON)
15.         {
16. #ifdef _LOOPBACK_DEBUG_
17.             getSn_DIPR(socket_sn, destip);
18.             destport = getSn_DPORT(socket_sn);
19. 
20.             printf("%d:Connected - %d.%d.%d.%d : %d\r\n", socket_sn, destip[0], destip[1], destip[2], destip[3], destport);
21. #endif
22.             setSn_IR(socket_sn, Sn_IR_CON);
23.         }
24.         if ((size = getSn_RX_RSR(socket_sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
25.         {
26.             if (size > DATA_BUF_SIZE)
27.                 size = DATA_BUF_SIZE;
28.             ret = recv(socket_sn, buf, size);
29. 
30.             if (ret <= 0)
31.                 return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
32.             size      = (uint16_t)ret;
33.             sentsize  = 0;
34.             buf[size] = 0x00;
35.             printf("%d:rece data:%s\r\n", socket_sn, buf);
36.             while (size != sentsize)
37.             {
38.                 ret = send(socket_sn, buf + sentsize, size - sentsize);
39.                 if (ret < 0)
40.                 {
41.                     close(socket_sn);
42.                     return ret;
43.                 }
44.                 sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
45.             }
46.         }
47.         break;
48.     case SOCK_CLOSE_WAIT:
49. #ifdef _LOOPBACK_DEBUG_
50.         printf("%d:CloseWait\r\n", socket_sn);
51. #endif
52.         if ((ret = disconnect(socket_sn)) != SOCK_OK)
53.             return ret;
54. #ifdef _LOOPBACK_DEBUG_
55.         printf("%d:Socket Closed\r\n", socket_sn);
56. #endif
57.         break;
58.     case SOCK_INIT:
59. #ifdef _LOOPBACK_DEBUG_
60.         printf("%d:Listen, TCP server loopback, port [%d]\r\n", socket_sn, localport);
61. #endif
62.         if ((ret = listen(socket_sn)) != SOCK_OK)
63.             return ret;
64.         break;
65.     case SOCK_CLOSED:
66. #ifdef _LOOPBACK_DEBUG_
67.         printf("%d:TCP server loopback start\r\n", socket_sn);
68. #endif
69.         if ((ret = socket(socket_sn, Sn_MR_TCP, localport, 0x00)) != socket_sn)
70.             return ret;
71. #ifdef _LOOPBACK_DEBUG_
72.         printf("%d:Socket opened\r\n", socket_sn);
73. #endif
74.         break;
75.     default:
76.         break;
77.     }
78. 
79.     if (socket_sn < _WIZCHIP_SOCK_NUM_)
80.     {
81.         socket_sn++;
82.     }
83.     else
84.     {
85.         socket_sn = 0;
86.     }
87.     return 1;
88. }
89. 

In this program, the TCP Server state machine will be executed. Corresponding operations will be performed based on the different states of the SOCKET. The state changes of the SOCKET are as shown in the following figure:

 

  • SOCK_CLOSED: The current SOCKET is not open. After configuring the connection to the server and the port number, open the SOCKET. Once the SOCKET is opened successfully, it will enter the SOCK_INIT state.

Note: When the W55MH32 abnormally disconnects from the server, the server does not know that we have disconnected. Therefore, when using the previous connection port to connect, the server will reject the connection. So, here, after a connection failure, the connection port will be automatically incremented by 1. If in some specific scenarios, the server only allows the client to connect using a fixed port, then this operation of automatically incrementing the connection port cannot be used.

  • SOCK_INIT: The SOCKET performs the operation of connecting to the server. If the connection is successful, the SOCKET state changes to SOCK_ESTABLISHED; if the connection fails, the SOCKET state changes to the closed state.
  • SOCK_ESTABLISHED: First, clear the interruption of the successful connection and send 1 packet of data to activate KeepAlive. Then, read the value of the Sn_RX_RSR (idle receive buffer register) register. When receiving server data, the value of the Sn_RX_RSR register will be greater than 0. At this time, we will print the received data and send the data in a loop.
  • SOCK_CLOSE_WAIT: When the server actively disconnects the connection, the SOCKET state changes to the SOCK_CLOSE_WAIT state. This is a semi-closed state, allowing for the final data transmission before closing. When using the disconnect() function to completely disconnect the connection, the SOCKET state will change to the SOCK_CLOSED state.

After the state machine is executed, it will automatically switch to the next socket, enabling up to 8 devices to connect to W55MH32 for loopback data testing.

8 Run results

After the burning routine is executed, the first thing you will notice is that the PHY link detection has been carried out, and then the set network address information is printed out, as shown in the following figure:

Next, eight sockets are initialized and a single port is listened to.

We launched 8 SocketTester network debugging tools, set them to the TCP Client mode, and entered the IP address of W55MH32. Connect after the port, and then you will be able to see the connection information of the W55MH32 printing client. Finally, use SocketTester to send data to W55MH32 for a loopback test.

 

9 Summary

This article explains how to use 8 sockets on the W55MH32 chip to implement the TCP server mode, allowing multiple clients to connect for data loopback testing. Through practical examples, it demonstrates the complete process from initializing the socket to listening on the port, handling client connections, data interaction, and connection closing. The article details the concept, characteristics, differences from UDP, application scenarios, data interaction process, ACK mechanism, retransmission mechanism, and Keepalive mechanism of the TCP protocol, helping readers understand its practical application value in reliable data transmission.

The next article will focus on the host machine search and configuration functions of W55MH32, analyzing its core principles and application in equipment management, and explaining the specific steps and key points for implementing the host machine search and configuration function of W55MH32. Stay tuned!

 

WIZnet is a non-fabrication semiconductor company founded in 1998. Its products include the Internet processor iMCU™, which adopts TOE (TCP/IP Offloading Engine) technology and is based on a unique patented fully hardwired TCP/IP. iMCU™ is designed for embedded Internet devices in various applications.

WIZnet has over 70 distributors worldwide, with offices in Hong Kong, South Korea, and the United States, providing technical support and product marketing.

The region managed by the Hong Kong office includes: Australia, India, Turkey, and Asia (excluding South Korea and Japan).

Documents
Comments Write