Now that we know how to write a client and a server in very simple terms, lets see how a real world client server pair works˙.
The first thing you should be wondering about is how can the same port of the server handle so many client requests ? This thing is called concurrent server. When you make a server listen through a socket and a port, you are not actually assigning the same socket to respond to the requests. When a client connects to a server’s listening socket, the server redirects the connection to a new socket , while the listening process continues on the same socket. When you see the upcoming server code you’ll see how this thing works.
for example, if listeningSocket is a socket listening on some port and it gets connection request from a client, then it goes for assigning a new socket to the client to connect and then continues listening.by the function call:
(redirectedSocket,clientConnected)=listeningSocket.accept() // you must have guessed that the function returns a tuple.
Another thing is about TCP which you’ll observe in the code below. The sockets you’ll be seeing are all TCP. So, you need to be sure that all the data that you transferred on client side is received on the server end. (or the vice versa)
Now although we see sockets as abstraction, there is a lot of events going on behind the scenes when you send data through the socket. Say you want to send a string across a socket, then your string may be packed into one or more packets, for which you might never know the number of packets. say we do mySocket.sendall(‘Hello I am Muktabh Mayank’) . So this string is to sent across the network, but then there are many packets in which this string will be transferred and you dont know the number.
Also these packets may need to be resend if the acknowledge packets for them are not received, so sendall() is a function which needs to occupy the port’s data output whatever code executes after it. Its exactly like socket.listen() which continues listening on some port even if some code has been executed already. The only way to free a socket after you have used sendall() function is to close the socket already. Such functions are called blocking functions. To visualise you can think of this, you cant use 2 sendall()s one after the other, until unless you close the socket in between and then reconnect through another socket.
recv() which is the function to receive what is being sent by the sendall() is not a blocking function and hence needs to be used in an infinite loop, so that the port is occupied once it starts receiving, so that no packet is missed. send() the function wrapper for actual system call is also available, but here Ill be using sendall().
Another thing, my code works on Linux system, and I have not tested it on windows and I ve heard some old version of Python had a problem with networking interface in Windows (which later turned out to be a windows fault and not Python from the article I read), so try using Linux :
first the server code :
(I dont know why My indents arent visible in my post, make sure you put indents after try , except, while, if etc blocks else code wont work)
host=” #We didnt fix the hosts to which we’ll cater
newSocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)# An IPV4 socket in #TCP
print ‘Cant Create Socket’
newSocket.bind((host,port)) # we are binding because
# Since This is the server side the socket must have fixed port address to listen to requests
print ‘cant bind’
#fixed port address coz you have to connect to it, In client there was no such fixed port
print ‘Server Will now run on port %d’%port
newSocket.listen(1)# Now its ready to take in any connections from clients]
clientSock,clientAddress=newSocket.accept() #new connections redirected to other sockets
# So that the port continues to listen
print ‘cant connect’
print ‘connected to %s’%(clientAddress,)
print ‘client says :’
while 1 :
if not buf : # a None is received if the sender socket closes down
break # leave the while loop
sys.exit(0) # successful termination
Now the Client code :
except socket.gaierror,e :
print ‘Unable to connect, %s’%e
print ‘message from client side %s’%message
make sure you run the server code first😛 and see your packet reach the other side of socket