Reputation: 23
I just began on the TCP server and client programming on python. however one of the of the if statement does not work and it does not make any sense:
while (1):
data = s.recv(1024)
if data == 'finish':
break
print data
print 'Finish'
and sometimes the last lane will print 'finish' in stead of 'Finish' and it cant exit the loop. This should be never happened because the 'print data' statement is skipped if data='finish'. Could someone tell me why this is happening?
Upvotes: 1
Views: 96
Reputation: 17960
Just because you sent the bytes in one call to send
does not mean that you will receive them in one call to recv
. They might arrive in smaller or larger groups. E.g. perhaps you send:
c.send('one')
c.send('two')
c.send('three')
c.send('finish')
But you receive
s.recv(1024) -> 'onetwothreefinish'
Or maybe you receive
s.receive(1024) -> 'one'
s.receive(1024) -> 'two'
s.receive(1024) -> 'three'
s.receive(1024) -> 'fin'
s.receive(1024) -> 'ish'
Upvotes: 3
Reputation: 9634
you can just compare with data.lower
to ensure that all uppercase letters are changed to lowercase letters, also change the while loop and remove the redundant braces
while True:
data = s.recv(1024)
if not data:
break
print data
print 'Finish
Upvotes: 1
Reputation: 621
The comparison is case sensitive. Use data.lower() in the if statement
Upvotes: 0