Reputation: 489
I have used python 3 to write a forking server which will accept many clients (I have not set a limit of number of clients). Each client will be assigned an index value in the server in order of their connection with the server. for example 1st client will be referred as CLIENT 1 then 2nd one is CLIENT 2 and so on.
But the problem is this index is not being assigned to clients correctly. Please help me out for this. I am sending the python code for server and client and also sample output
For the server the code is
import os, socket
host="127.0.0.1"
port=7000
s=socket.socket()
s.bind((host, port))
s.listen(10)
def handle_client(s, addr, i):
while True:
data=s.recv(1024)
decoded_data=data.decode("utf-8")
if not decoded_data:
print("\nconnection with client " + str(i) + " broken\n")
break
print(" CLIENT " + str(i) + " -> " + decoded_data)
def server():
i=1
while i<=10:
c, addr=s.accept()
child_pid=os.fork()
if child_pid==0:
print("\nconnection successful with client " + str(i) + str(addr) + "\n")
handle_client(c, addr, i)
else:
i+=1
server()
For the client the code is
import socket
def client():
host="127.0.0.1"
port=7000
s=socket.socket()
s.connect((host, port))
msg=str(input("\n -> "))
encoded_msg=bytes(msg, "utf-8")
while msg!='q':
s.send(encoded_msg)
msg=str(input("\n -> "))
encoded_msg=bytes(msg, "utf-8")
client()
Now I open 2 terminals and run the server in one and in the other terminal I run clients one by one
The terminal in which I run the clients is like this
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
-> hii
-> q
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
-> hello
-> q
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
-> hii how are you???
-> q
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
-> good day
-> q
debesh@laptop:~/Documents/programs/python/network$
The terminal in which the server is running is like this
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_server.py
connection successful with client 1('127.0.0.1', 50362)
CLIENT 1 -> hii
connection with client 1 broken
connection successful with client 2('127.0.0.1', 50363)
CLIENT 2 -> hello
connection with client 2 broken
connection successful with client 1('127.0.0.1', 50364)
CLIENT 1 -> hii how are you???
connection with client 1 broken
connection successful with client 3('127.0.0.1', 50365)
CLIENT 3 -> good day
connection with client 3 broken
As you can see in the server terminal the index 1 is assigned to two clients-the 1st one and the 3rd one. How does this happen? Whats the fault in my code? Please help me
Upvotes: 1
Views: 8382
Reputation: 104712
The issue is that your forked processes keep running the server loop after handling a client. Try adding break
after calling handle_client(c, addr, i)
:
def server():
i=1
while i<=10:
c, addr=s.accept()
child_pid=os.fork()
if child_pid==0:
print("\nconnection successful with client " + str(i) + str(addr) + "\n")
handle_client(c, addr, i)
break # fix here!
else:
i+=1
Upvotes: 2