views:

97

answers:

2

I established a TCP client server connection in python where the server sends code to the client and the client evaluates the code sent.The code is sent from a file in the server. While runnig the code, it works fine at times, but at times the client code doesn't work.. It gets stuck.. Is it some problem with the network connection or the ports? But if so, how does it work at other times?

server side sending code(being read from a .py file)

    info = finp.readlines() 
    for line in info:
        self.channel.send(line)
    self.channel.send("#p") 

client side receiving code

    while b!="#p":  
        b=client.recv(1024) 
        print >>fout, b
    fout.close()

    client.close()

I am reading the data from a file in the server side, sending it over the network and storing it into another .py file on the client side. This runs properly at times, but at times it gets stuck.

A: 

Posting some code exhibiting your problem would do wonders for determining the problem.

Most likely, you are making assumptions about send and recv. The usual answer to this behavior is to read the documentation carefully:

  • send (which is not guaranteed to send all bytes requested, see sendall)
  • recv (which can receive more or less data than you expect)

Keep in mind TCP/IP is a stream-based protocol. There are no message boundaries, so the data transmitted must include following a protocol between the client and server to determine the size of a complete message, such as sending the size of the message before the message or terminating the message with a unique value.

Mark Tolonen
A: 

try

while !b.endswith("#p"):
    b = client.recv(1024) 
    print >> fout, b

becouse in b variable may be a string which ends with a #p like last line of the file#p

or if you have some staff after a #p use this

while b.find("#p") == -1:
   ...

or better

while True:
    b = client.recv(1024)
    if not b:
        break
    print >> fout, b

In this case you don't need #p tag.

jcubic