---
title: "IO Library Issue"
url: "https://maker.wiznet.io/irina/projects/io-library-issue/"
markdown_url: "https://maker.wiznet.io/irina/projects/io-library-issue/md"
type: "WCC: WIZnet Created Content"
author: "irina"
author_url: "https://maker.wiznet.io/irina/"
original_author: "irina"
published: "2023-11-21"
language: "en"
likes: 0
views: 347
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# IO Library Issue

> IO Library Issue #127

Original author: irina

## Article

"**IO Library Issue #127**:** Socket Closure Bug in listen() Function**"

One of the prominent issues among various concerns is identified within the listen() function in the socket.c file.

The issue arises when reading **getSn_SR(sn)**; if a TCP connection is already established, it leads to a socket closure state.

![](https://blog.kakaocdn.net/dn/zRzmT/btsAFud0sn7/a3jx5EO9VjJOyKHhOnDr21/img.png)

Upon testing, under normal circumstances, this issue does not manifest.

However, introducing a delay of approximately 1 second before line 254 triggers the problem. In this scenario, **getSn_SR(sn)** outputs **SOCK_ESTABLISHED** instead of **SOCK_LISTEN**, resulting in a socket closure.

![](https://blog.kakaocdn.net/dn/b6ITGT/btsAF5dHYuH/kdZBKTvvuh7GPSQNH3JW10/img.png)

When observing the **serial output**, errors become evident as described below.

![](https://blog.kakaocdn.net/dn/b5gajF/btsAF6X0stt/kChvsmfwHnxo3sBrRvfvPK/img.png)

I have made the following changes and have already submitted a Pull Request with the alterations.

![](https://blog.kakaocdn.net/dn/cmjWqY/btsAGXl9yqw/VgLRxqXn0BTLp0Ym6mtI6K/img.png)

Here is the **Python code** utilized for testing:

```python
import socket
import threading

# Server IP address and port number
server_ip = "192.168.100.109"
server_port = 5000

# Create a socket
def create_client():
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        # Attempt to connect to the server
        client_socket.connect((server_ip, server_port))
        print(f"Connected to the server: {server_ip}:{server_port}")
    except Exception as e:
        print(f"Error occurred during connection attempt: {e}")
    finally:
        # Close the client socket
        client_socket.close()

# Create and run multiple clients
num_clients = 10  # Number of clients to create

threads = []
for _ in range(num_clients):
    thread = threading.Thread(target=create_client)
    thread.start()
    threads.append(thread)

# Wait for all threads to finish
for thread in threads:
    thread.join()
```

Feel free to provide any additional context or details, and I'll be happy to assist further.

IoLibrary issue: https://github.com/Wiznet/ioLibrary_Driver/issues/127

---

Source: https://maker.wiznet.io/irina/projects/io-library-issue/
