• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

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

Aniruddha Chaudhari/16731/2
CodePython

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!

Python Interview Questions eBook

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Shasank Prasad
    May 3, 2020 at 6:35 pm

    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!

    • Reply
      Aniruddha Chaudhari
      May 8, 2020 at 3:07 pm

      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 Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard