---
title: "W5500 EVB PICO: How to handle multiple clients as a TCP Server"
url: "https://maker.wiznet.io/forum/15592"
markdown_url: "https://maker.wiznet.io/forum/15592/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "Lautarosco"
created: "2025-02-18T21:12:20+09:00"
last_activity: "2025-02-19T19:01:58+09:00"
language: "en"
tags: ["W5500"]
views: 0
replies: 2
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# W5500 EVB PICO: How to handle multiple clients as a TCP Server

## Question

Asked by Lautarosco on 2025-02-18 in Ethernet Chips.

Hello hope you are all doing well

I want to handle multiple connections with the W5500-EVB-Pico board working as a server. So far I can reproduce and make a server with one socket in one port that listens for a client and then establishes a connection with it. But I can't have another client since this socket that was previously listening is now established, sending or receiving data. What I want instead is to have one socket for listening, that receives connections and delegates the connection to another socket in the same port so I can handle multiple clients.

What I want is:

wiznet_ip:12345 - SOCKET 1 (Listening)

Then a client connects, the SOCKET 1 handles the connection and I end up with:

wiznet_ip:12345 - SOCKET 1 (Listening)
wiznet_ip:12345 - SOCKET 2 (Stream with client)

What happens instead is that the Listening socket remains in SOCK_ESTABLISHED and the new socket is in SOCK_INIT, so the transfer is not done correctly. My code is something like this

```plaintext
listen(listeningSocket);
uint32_t status_register = getSn_SR(listeningSocket);

if (status_register == SOCK_ESTABLISHED) {
// ...
new_socket = assign_socket("TCP_Stream");; // this returns a valid socket number

     if (socket(new_socket, Sn_MR_TCP, self->port, 0) != new_socket) {  // i want the new socket to be in the same server port
                    printf("[ERROR] Failed to create new socket");
                    return NULL;
                }
// here i get the ip/port of the client that connected
uint8_t remote_ip[4];
getSn_DIPR(self->listeningSocket, remote_ip);   // Destination IP Register
int remote_port = getSn_DPORT(self->listeningSocket); // Destination Port Register

// here i set the new socket destination ip/port with the previous data
setSn_DIPR(new_socket, remote_ip);
setSn_DPORT(new_socket, remote_port);


// ...


int ret = recv(new_socket, buffer, bytesToRead);
 // <--- This line returns a SOCKERR_SOCKSTATUS, since the StatusRegister is in SOCK_INIT.
```

How should this be handled? I couldn't find any examples of this kind of behaviour, the loopback example just uses the listening port for the client connection and that is far from what I need

## Replies

### Reply 1 by Lautarosco, 2025-02-18

Okay, but in that example the new socket is in SOCK_INIT just as in my current code. How should I handle the connection on the new socket?

### Reply 2 by Lautarosco, 2025-02-19

[SOLVED] If someone wonders how I managed to accomplish this behaviour, what I did is basically transforming the listening socket to a communication socket when it is established, and then opening a new socket for listening on the same port.

---

Source: https://maker.wiznet.io/forum/15592
