0

I am making a game in python. It uses sockets. It is my first time using sockets.

Here is part of my code:

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# get local machine name
host = socket.gethostname()                           

port = 9999                                           

# bind to the port
serversocket.bind((host, port))                                  

# queue up to 5 requests
serversocket.listen(5)                                       

player = 1
keys = []
playerSockets = []
print "Waiting for players..."
while True:
    # establish a connection
    clientsocket, addr = serversocket.accept()

    key = id_generator()
    keys.append(key)

    playerSockets.append(clientsocket)
    clientsocket.send(str(player)+"\r\n")
    clientsocket.send(str(key)+"\r\n")
    print "Player " + str(player) + " connected." 
    player = player + 1

    if player > 3 or currentEpochtime() > endConn:
        break

print str(player-1) + " active players.\n"

if(player < 3):
    print "Not enough Players. Closing..."
    serversocket.close()
    quit()


#Other Stuff Continues

Now, it only sends the above data (player and key) only after the next send statement comes in my code. But then, that next send statement never sends because it is waiting for a reply from the client. But the client is not sending anything as it still hasn't received the data from the previous send statement in the server.

I have been trying to find a solution for a very long time, but cannot do so.

Please help!

Sid Prasad
  • 137
  • 1
  • 2
  • 10
  • Possible duplicate of [Python Socket Multiple Clients](http://stackoverflow.com/questions/10810249/python-socket-multiple-clients) – JimmyB May 19 '16 at 12:22

1 Answers1

0

Not quite sure about your question, do you mean client couldn't receive info from server?

I try with the following client code:

import socket

s = socket.socket()
host = socket.gethostname()
port = 9999
s.connect((host, port))
print s.recv(1024)
input = raw_input('type anything and click enter... ')
s.send(input)
print "the message has been sent"

It seems to receive data sent by server and be able to send data back to server.

Does this help?

linpingta
  • 2,324
  • 2
  • 18
  • 36
  • I don't think it's to do with the client side. I think it's the server that's not sending the data. It's a multiplayer game, so there are multiple clients. Therefore I'm not closing the connections after accepting. Because I'll need them later. Could this be the problem? – Sid Prasad May 19 '16 at 12:01