Start New Discussion within our Software Development Community

Hi all,
Here is mu scenario.I am trying to open multiple port on a compute.
I am trying to make the code multi threaded.So here is my code:

import socket
import thread
from threading import *
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def handler(clientsock,addr):
    while 1:
        data = clientsock.recv(BUFSIZ)
        if not data:
            break

def openport(port):
	HOST = socket.gethostname()
	BUFSIZ = 1024
	s.bind((HOST,port))
	s.listen(5)
	print 'Listening on port ',port
for n in range(1, 10):
	thread.start_new_thread(openport, (n,))
while 1:
    print 'Waiting for connection:'
    clientsock, addr = s.accept()
    print 'Connected with: ', addr
    thread.start_new_thread(handler, (clientsock, addr))

And i am getting following error;

C:\Python27>python m.py
Waiting for connection:
Traceback (most recent call last):
  File "m.py", line 21, in <module>
    clientsock, addr = s.accept()
  File "C:\Python27\lib\socket.py", line 202, in accept
    sock, addr = self._sock.accept()
socket.error: [Errno 10022] An invalid argument was supplied

C:\Python27>

Through this code i want my PC to listen on port 1 to 10.Line 17.It will then start a new thread for that port.And also start another thread if any client connects any of those port.
(Assuming all the selected ports are not in used by another program.)To change the number number of port we have to modify line 17.
So how to solve that.and is there any better way to do that.

Thanks and Regard

Debasish

This article has been dead for over six months. Start a new discussion instead.