OSError: [WinError 10022] An invalid argument was supplied | Python Socket Error

OSError: [WinError 10022] An invalid argument was supplied | Python Socket Error

I was working on socket programming. The code was running fine on Python 2 but it was throwing an error while running on Python 3 version.

Error is as follows:

>>py server.py
Traceback (most recent call last):
File "server.py", line 20, in <module>
c, cAddr = s.accept()
File "C:\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 212, in accept
fd, addr = self._accept()
OSError: [WinError 10022] An invalid argument was supplied

This is the exception thrown by the Operating System while executing socket program on Windows OS.

Solution:

It is mandatory to add listen()

s.listen(20)

Where,

  • “s” is the socket object.
  • 2o is the maximum number of queued connections. You can set any number as per your requirement.

I had missed this line to add in my socket program. Because of this, my program was crashing.

After adding this line of code just before the s.accept(), my issue is solved.

How to debug WinError in Socket Programming?

It is not easy to debug any socket programming errors.

WinError 10022 is for not passing valid arguments to the socket. This is just one example. There are many socket errors you will come across. Each error code has different meaning.  You can check the error codes and their meanings on Official Microsoft website.

To avoid this crash, it is always good practice to add exception handling in any socket program you write.

If you are getting any error in your socket program and not able to fix it, write in the comment. I will help you on my best.

Happy Pythoning!

2 Comments

  1. hi! I was making an IP logger in python using sockets but got this error instead

    # import
    import os
    import datetime
    import socket
    import sys
    
    
    # Function
    def logger():
        s = socket.socket()
        ip = input("Enter address:")
        
        try:
            target = ip
            port = 80
    
    
            s.bind((ip , port))
    
        except:
            print("Error!")
    
        while True:
            s.listen(5)
            conn , address = s.accept()
    
            print("[+] IP LOGGED!" + str(address)[0])
    
    
    logger()
    

    This is my source code

    and this is the error :

    Traceback (most recent call last):
      File "C:\Pycharm\scratch.py", line 30, in 
        logger()
      File "C:\Pycharm\scratch.py", line 24, in logger
        s.listen(5)
    OSError: [WinError 10022] An invalid argument was supplied
    

    pls help me fix this error!

    1. You can not give any random IP for creating socket connection. Use your computer IP address that ou can get using gethostname() method. This is how you can make a connection.

          #Getting host name 
          ip = socket.gethostname()
      
          #printing hostname
          print(socket.gethostbyname(hostName))
      
          #creating server socket
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
      
          port = 12345
      
          s.bind((ip , port))
      
      

Leave a Reply

Your email address will not be published. Required fields are marked *