I am working on a client-server architecture in which several clients shall transfer files to a running server. I am wondering whether it is possible for the server to receive input on one port from different clients at the same time.
My code so far:
Server
import socket
import time
mySocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
# EDIT: This line was added based on @Aleksander Gurin's response below. The problem persists.
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mySocket.bind( ('localhost', 1234 ) )
mySocket.listen( 2 )
channel, details = mySocket.accept()
while True:
incoming = channel.recv( 100 )
if incoming:
print "Received >%s<" % incoming
incoming = ''
Client
import sys
import socket
import time
mySocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
mySocket.connect( ('127.0.0.1', 1234) )
counter = 1
while True:
mySocket.send( "Message %03d from client %s." % (counter, sys.argv[1]) )
time.sleep(2)
counter += 1
I am starting two instances of the client like so:
./client.py 1 &
./client.py 2 &
So far, however, my server is only receiving input from one client:
Received >Message 001 from client 1.<
Received >Message 002 from client 1.<
Received >Message 003 from client 1.<
Received >Message 004 from client 1.<
Received >Message 005 from client 1.<
...
My question therefore is: Is it possible to receive the postings from the second client on the server as well - and if so, how?
P. S.: I checked this related SO post but could not really extract an answer from that either.