How to Fix OSError: [WinError 10038] an Operation Was Attempted on Something That Is Not a Socket

Salman Mehmood Feb 02, 2024
  1. Create an Application With the Help of the socket Module Without Error in Python
  2. the OSError: [WinError 10038] An Operation Was Attempted on Something That Is Not a Socket
How to Fix OSError: [WinError 10038] an Operation Was Attempted on Something That Is Not a Socket

With this explanation, we will learn what server and client are and how to use the socket module to create an application with the help of server and client. We will also learn how to avoid errors when making these types of applications in Python.

Create an Application With the Help of the socket Module Without Error in Python

Around the internet, the node can be a server or a client; of course, it can also be a client-to-client network where you have all the nodes and are talking to each other. It is a peer-to-peer network.

We have servers on the internet listening to the clients, and they are waiting for the client to send a request.

For example, when you go to google.com, the Google server, and you send a request to the Google server, the Google server will send you some response, i.e., a page or a Page Not Found message.

Let’s start by creating two Python files, demo.py and client.py. We want to send the message from the server to the client.

The server and client could be from different locations or outside the network.

We will create the server, and then we will pass a message and see how this client receives the message from the server. To keep it simple, we are doing it on the localhost, but you can also do it on different systems.

The first thing we need to do is to import the socket and create an instance. We will call the socket class and then specify the socket family.

The socket family will be AF_INET, and the socket formula will be SOCK_STREAM.

In the next step, we will need to bind the socket to the hostname on the port using the bind() method. Now, we have to listen for connections using the listen() method; the maximum number of connections would be 5.

import socket

SKT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SKT.bind((socket.gethostname(), 6060))
SKT.listen(5)

To find a connection with a client, we send a message from the server so that the client will receive the message.

To do that, we will use the while loop that would be True. In this loop, we will create another socket that would be the client socket using the connect() method; it will be used in the client.py file.

We are using the accept() method that returns two values; that is why we declared two variables that store the incoming socket and the address of the incoming connection. Now, we will print a message that shows a connection has been established from the specified address.

while True:
    Client_Socket, Adress = SKT.accept()
    print(f"Connection has been established from {Adress}")

Now we will need to send a message in bytes. The encoding will be utf-8, sent to each client connected with the socket.

Client_Socket.send(bytes("Welcome to the server!!!", "utf-8"))
Client_Socket.close()

Our socket is ready to listen for connection. Now we will work in the client.py file and use the same code except the connect() method.

In the connect() method, we will use the same hostname, localhost, and port.

Now, we are going to receive the message that is coming from the server. We will use the recv() method and pass it to 2048 bytes.

We will print out this message to verify whether the message has been received or not.

import socket

SKT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SKT.connect((socket.gethostname(), 6060))

MSG = SKT.recv(2048)

print(f"Message has been received {MSG}")

Now, the application is completed, so we will run the server first, the demo.py file; it is running, but nothing has happened. If we go to the client.py and run this file in an interactive window, we can see the response in the console when these files are running simultaneously.

Application With the Socket Module Without Error - Output

the OSError: [WinError 10038] An Operation Was Attempted on Something That Is Not a Socket

There are some common reasons we get errors when creating this application. One reason might be that you forgot to run one file, both of them, or you are making a mistake anywhere.

Let’s look at an example where the users often get this error.

import socket

SKT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SKT.bind((socket.gethostname(), 6060))
SKT.listen(5)

while True:
    Client_Socket, Adress = SKT.accept()

    while True:

        print(f"Connection has been established from {Adress}")
        Client_Socket.send(bytes("Welcome to the server!!!", "utf-8"))
        Client_Socket.close()

Output:

OSError: [WinError 10038] An operation was attempted on something that is not a socket

This is because we are running a nested while loop, and both are True. When the outer while loop is executed, we accept the incoming connection from the client.

The problem occurs when the inner while loop is executed.

Since the inner while loop is True the first time, this loop executes perfectly, but in the second iteration, the while loop needs to accept an incoming connection. The accept() method calls from the outer loop, which will never be called; that is why it finds the socket no longer exists.

Another reason the problem can occur is when you run these two files from the terminal. If you are working in VS code, run these two files individually in (current file in an interactive window).

Application With the Socket Module - Run Current File in Interactive Window

Complete demo.py File Code:

import socket

SKT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SKT.bind((socket.gethostname(), 6060))
SKT.listen(5)

while True:
    Client_Socket, Adress = SKT.accept()
    print(f"Connection has been established from {Adress}")
    Client_Socket.send(bytes("Welcome to the server!!!", "utf-8"))
    Client_Socket.close()
Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Python Error