python socket, how to receive all the message when buffer is not big enough?
Question: 
    # addition_server.py
    
    import socket
    
    buf_size = 4
    host = ''
    port = 8000
    server_addr = (host, port)
    
    
    def get_msg(soc):
        msg = ''
        while True:
            temp = soc.recv(buf_size)
            if not temp:
                break
            msg += temp
        return msg
    
    if __name__ == '__main__':
        soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # socket.error: [Errno 98] Address already in use
        soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        soc.bind(server_addr)
        soc.listen(0)
    
        runnnig = True
        while runnnig:
            client_soc, client_addr = soc.accept()
            print client_addr
            # socket.error: [Errno 104] Connection reset by peer
            message = client_soc.recv(buf_size)
            #message = get_msg(client_soc)
            if message == 'q':
                runnnig = False
            numbers = message.split(' ')
            numbers = filter(None, numbers)
            try:
                numbers = map(int, numbers)
                s = sum(numbers)
                numbers = map(str, numbers)
                answer = ' + '.join(numbers)
                answer = '%s = %s' % (answer, s)
            except Exception as e:
                print e
                answer = 'error'
            client_soc.sendall(answer)
            client_soc.close()
        soc.close()
    

* * *
    
    
    # addition_client.py
    
    import socket
    from addition_server import get_msg
    from addition_server import server_addr
    
    buf_size = 1
    runnnig = True
    while runnnig:
        soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        soc.bind(('', 6060))  # without this, random port will be assigned
        soc.connect(server_addr)
        msg = raw_input('> ')
        if not msg:
            soc.close()
            break
        else:
            if msg == 'q':
                runnnig = False
        soc.sendall(msg)
        #reply = soc.recv(buf_size)
        # socket.error: [Errno 104] Connection reset by peer
        reply = get_msg(soc)
        print reply
        soc.close()
    

* * *
    
    
    ~/nuts/git/socket_learn/pairs$ python addition_client.py 
    > 1 2
    1 + 2 = 3
    > 1 2 3
    Traceback (most recent call last):
      File "addition_client.py", line 23, in <module>
        reply = get_msg(soc)
      File "/home/phy/nuts/git/socket_learn/pairs/addition_server.py", line 14, in get_msg
        temp = soc.recv(buf_size)
    socket.error: [Errno 104] Connection reset by peer
    

* * *

The buffer size in the server is intentionally small, so the you can see the
above error. but the get_msg(client_soc) method not works in the server, I
don't know why.


Answer: The socket stream protocol implements a **stream** and there are no implicit
message boundaries.

There is no way for the reader to know if a message is complete or not except
if this is specified in the message content itself. No extra boundaries are
added on the wire.

When you call `recv` and there is no data in the buffer the call will either
raise an exception (non-blocking socket) or will just wait (blocking socket).

The `sendall` facility is just for avoiding writing a loop when sending a
buffer but of course there is no way to implement `recvall` because there's no
way to know when the message is complete.

You need to add a message boundary to your protocol; this could be a newline
separating messages or prefixing each message with a message size. This way
the reader will know when a message is complete before starting processing.

