3

Im trying to make a simple game but cant understand how to make it work and send more then one thing over the network. It works the first time but it supposed to go 10 times. It only sends 1 random number now but i want it to send one new when the game goes again and want a new number.

Server

import socket, random 
sock = socket.socket()
host = socket.gethostname()
port = 12345
sock.bind((host, port))
sock.listen(5)
c, addr = sock.accept()
cpu = random.choice(range(0, 3))
c.send(cpu)
gameon = c.recv(int(1024))

Client

import socket, random
sock = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
sock.connect((host, port))
GAMEON = 'Rock', 'Paper', 'Scissors'
game = 0
iwin = 0
ilose = 0
tie = 0
while game < 10:
 for i in range(0, 3):
 print "%d %s" % (i + 1, GAMEON[i])
 player = int(input ("Choose from 1-3: ")) - 1
 cpu = int(sock.recv(1024))
 print cpu
 print""
 print "%s vs %s" % (GAMEON[player], GAMEON[cpu])
 print ""
 if cpu != player:
 if (player - cpu) % 3 < (cpu - player) % 3:
 print "Player wins\n"
 iwin += 1
 else:
 print "CPU wins\n"
 ilose += 1
 else:
 print "TIE!\n"
 tie += 1 
 game += 1
 sock.send(str(game))
print"Game is done"
print"you win: ", (iwin), "Times"
print"computer wins: ", (ilose), "Times"
print"tie: ", (tie), "Times"
asked Jan 12, 2013 at 16:08

2 Answers 2

2

The problem is that your server will serve one request and then stop. You need to put it in some kind of while loop.

I wrote a basic IM server/client in Python which might help you: https://github.com/hdgarrood/PyMess/blob/master/server/basictcpserver.py#L59

answered Jan 12, 2013 at 16:15
Sign up to request clarification or add additional context in comments.

Comments

2

You need threads to do your bidding.

Example code

# Listen
s.listen(10)
print 'Socket now listening on port ' + str(PORT) + "..."
while 1:
 # wait
 conn, addr = s.accept()
 print 'Connected with ' + addr[0] + ":" + str(addr[1])
 # Let's fork a thread for each request
 processThread = threading.Thread(target=processConnection, args=(conn, addr[0]));
 processThread.start()
s.close()

Your processConnection will look like this:

# Process Connection
def processConnection(conn, ip):
 print "Thread started..."
 print "-------------------------------------------------------------";
 cpu = random.choice(range(0, 3))
 conn.send(cpu)
 gameon = conn.recv(int(1024))
 conn.close()

Update 1

If you need the server to keep talking with the client, then do this. Server will wait for the client to send back a message. If client sends anything, server will return a random number. If the client doesn't need anymore data, just close the connection and server loop will end.

import socket, random 
sock = socket.socket()
host = socket.gethostname()
port = 12345
sock.bind((host, port))
sock.listen(5)
c, addr = sock.accept()
white True:
 cpu = random.choice(range(0, 3))
 c.send(cpu)
 gameon = c.recv(int(1024))
 if gameon is None:
 break
answered Jan 12, 2013 at 16:12

5 Comments

Hmm cant get this to work :( it still just send on request then stop.
This is exactly what it does. What should it do? Send 10 random numbers one after another?
No when server needs one but it only works 1 time like before :) How can i get it to work 10 times? :P
Please explain. What should happen when a client connects?
i think the client and server are connected all the time and i could just close the connection in the end of the code right? I know threads could be usefull if u want more then one client connected but thats not important atm. Right now the game works first time cuz server sends one number with conn.send(cpu) but it never sends another number when game loops second time bcuz it dont know when to send it, it never get a request or anything when it want it :( maybe i should just start over and put the game server side and client just connects.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.