1

This is my code, which recvieves the messages from the client:

def RecieveFromClient(clientSocket, address):
    print "Connection from " + str(clientSocket) 
    msg = None
    while True:
        msg = clientSocket.recv(1024)
        print msg
        if msg == 'c':
            break

    clientSocket.close()
    print "client gone"

Now when I telnet to the server that is listening to the client, and press a single character, it immediately prints out the character on the server side. What I want is receive a line or complete sentence from the user. Is it possible?

Mr PyCharm
  • 83
  • 1
  • 1
  • 7

1 Answers1

0

recv() will return whenever it gets new data. recv(size) will return only once it has size worth of data.

You're going to have to make the length of the message an explicit part of your protocol. Here's the best way: prefix every message with a length, either as a fixed-size integer (converted to network byte order using socket.ntohs() or socket.ntohl() please!) or as a string followed by some delimiter (like '123:').

Source :https://stackoverflow.com/a/1716173/1802922

Community
  • 1
  • 1
Iceman
  • 365
  • 1
  • 3
  • 13