1

I'm working on a python client which is supposed to converse with a C programmed server. For now it just answers a few recognised commands with "ok" (the server part works when I test it with netcat as the client). But my python client only gets the ok 1 out of four times (and empty lines the rest of the time). I don't get why as the socket is set as blocking so I figure it does receive something but I don't know what.

Here is what I have. "info" is just an object with a few strings stored in it, everything before the loop works and a bit simplified for readability : I'm just supposed to receive a welcome message then send back the team name of the connected player then receive a position.

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();

try:
    remote_ip = socket.gethostbyname(info.host)
except socket.gaierror:
    print "Hostname could not be resolved. Exiting"
    sys.exit()

s.setblocking(1)
s.settimeout(60)
s.connect((remote_ip, info.port))

data = s.recv(1024)
print data
team = info.team + "\n"
s.sendall(team)
data = s.recv(1024)
print data

while 42:
    msg = raw_input('>> ')
    s.sendall(msg + "\n")
    data = s.recv(1024)
    if data == "":
        break
    else:
        print data

s.shutdown(socket.SHUT_WR)
s.close()
Andy G
  • 19,232
  • 5
  • 47
  • 69
Rhapsody
  • 13
  • 4
  • Perhaps not related, but if you want blocking behaviour, there is not really a need to set a timeout or to use setblocking(1,) as it is default socket behaviour. It really just adds complexity: http://stackoverflow.com/questions/16745409/what-does-pythons-socket-recv-return-for-non-blocking-sockets-if-no-data-is-r. Also, what you are saying is that only the first recv(1024) call gives "ok" back, and all subsequent calls are empty? Just trying to figure out what the problem really is. – martin.code Jun 07 '14 at 10:17
  • Yeah setblocking and set timeout were just testing around, I figured it changed nothing, I'll remove them, thanks for pointing it out. And I'm saying that the two first recv(1024) call do give me back accurate data but in the loop, when I give a valid command via "raw_input", I get ok, then the next three commands get empty returns then ok again etc... – Rhapsody Jun 07 '14 at 10:26
  • How can you get three consecutive "empty returns" if an empty return ("") would break the while loop? It therefore seems "empty returns" is not the same as ""? It is difficult to debug as we have no clue about the specifics of the server side implementation. An idea to find the error might to to create a simple python server and see if the error replicates, otherwise it might be related to the C server. – martin.code Jun 07 '14 at 10:38
  • Exactly that's what I really don't understand, I tried to break it on a "\n" but it's not this either. And the C server uses the function write (also tried with dprintf, same reults) if that can help, also a netcat client gets expected behavior from it that's what lead me to think the problem was with my client. Thank's for the lead about the python sever I'll do just that. – Rhapsody Jun 07 '14 at 10:42
  • It is strange however, because the socket is just an implementation of the universal TCP protocol which by the definiton is supposed to be platform and language-agnostic. Perhaps try this first: skip the while loop and just do a sequence of 5 raw_input to see if the error still replicates. – martin.code Jun 07 '14 at 10:50
  • Use `print repr(data)` to see what you really receive. Also it might be that netcat ends lines with "\r\n" or something like that. – Aleksi Torhamo Jun 07 '14 at 12:18

1 Answers1

0

It seems to me you do receive something : cast data to ascii with ord() to see it. My guess is that there's 3 unprintable lines before the "ok" in the response the server sends you. Just identify then catch them in a new loop.

Frolanta
  • 82
  • 1
  • 5
  • ...So the server was sending me a whole bunch of zeros before and after the ok. A loop with (ord(data[0])!= 0) did it, thanks ! @feat.martin, trying it with a new serv in python made the client work like a charm so I went back to tweaking the server(still can figure out where I send these 0 thought...), thanks for taking the time to try and help, you were right. – Rhapsody Jun 07 '14 at 13:19
  • @Aleksi Torhamo : print repr() gave me a screenfull of \x00, I'm going to look into that and see if I understand better where my server goes wrong. – Rhapsody Jun 07 '14 at 13:19