Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:34:33+00:00 2026-06-14T07:34:33+00:00

I am trying to implement a socket server that listens to a particular port

  • 0

I am trying to implement a socket server that listens to a particular port number. When I write the code without any class, it works fine. But fails to work when I implement the class as below:

import socket;
from ServerConfig import ServerConfig;

class SyncServerRK:
    def __init__(self):
        self.config = ServerConfig()         #Call Initialize config class        
        #Send my IP address to managing_agent
        self.Listener()         #Call listener method

    def Listener(self):
        s = socket.socket()          # Create a socket object
        host = socket.gethostname()                     # Get local machine name
        port = self.config.Connect_Port()               # Reserve a port for your service.
        s.bind((host, port))         # Bind to the port
        while True:
            c, addr = s.accept()     # Establish connection with client.
            print ('Got connection from', addr)
            c.send('Thank you for connecting'.encode())
            print ('Message received:',c.recv(1024).decode())
            c.close()                # Close the connection            
        print(self.config.Managing_Agent())

if __name__ == "__main__":
    SyncServerRK()

The error I received is:

Traceback (most recent call last):
  File "C:/Share/SyncServerRK.py", line 24, in <module>
    SyncServerRK()
  File "C:/Share/SyncServerRK.py", line 8, in __init__
    self.Listener()         #Call listener method
  File "C:/Share/SyncServerRK.py", line 16, in Listener
    c, addr = s.accept()     # Establish connection with client.
  File "C:\Python33\lib\socket.py", line 135, in accept
    fd, addr = self._accept()
OSError: [WinError 10022] An invalid argument was supplied

Can someone please advise how to implement a server socket with threads using object-oriented philosophy.

The Non class version that worked well:

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print ('Got connection from', addr)
   c.send('Thank you for connecting'.encode())
   print ('Message received:',c.recv(1024).decode())
   c.close()                # Close the connection      
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:34:34+00:00Added an answer on June 14, 2026 at 7:34 am

    You are missing the s.listen(5) in your class based version.
    The socket must be bound to an address and listening for connections before accepting connections.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a socket server that will run in most shared PHP
I'm currently trying to implement a socket server that enables the clients to send
I'm trying to implement a server-client socket program in Java that can support multiple
I'm trying to implement a simple HTTP server in Android that handles POST requests,
I'm trying to write a proxy server and right now I want to implement
I am trying to adapt my simple socket server so that it can have
I'm trying to implement Sun's example Socket program, i.e. the KnockKnock server and client
I'm trying to implement a simple web server on Linux that connects to the
I am currently trying implement a QThread that contains a socket connection. The socket
I am just trying to implement a simple web server in C++. For that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.