All this program should do is accept inputs from 2 clients simultaneously and print them out, but instead it accepts 1 input from the first client to connect, then starts accepting inputs infinitely from the second client, but not from the first anymore. Any tips on how to fix this code? (shown below)
import socket
import select
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 4000))
s.listen(5)
sockets = []
sockets.append(s)
while True:
(read, write, err) = select.select(sockets, [], [])
for socket in read:
if (socket == s):
(c, a) = socket.accept()
sockets.append(c)
print ("Received connection from: ", a)
message = c.recv(80).decode()
print (message + " from " + str(a[1]))
You need to receive data from client only when the data is available to prevent blocking.
The above line read regarlessly; and it will read only from the last client that accepted.
Here's a modified version of the
forloop:I renamed
sockettopeerto prevent name shadowing; Usingsocketas a variable name will shadowsocketmodule reference.