Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First advice: simplify. Lets first see if we can get this thing working <em>without</em> protocol buffers:</p> <h1>client.py</h1> <pre><code>import socket import sys import struct address = ('localhost', 6005) client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) client_socket.connect(address) messages = ["foobar", "barbaz", "bazquxfad", "Jimmy Carter"] for s in messages: totallen = 4 + len(s) pack1 = struct.pack('&gt;I', totallen) # the first part of the message is length client_socket.sendall(s) </code></pre> <h1>server.py</h1> <pre><code>import socket import sys import struct address = ('localhost', 6005) server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.bind(address) while True: print "Listening" # totallen = server_socket.recv(4) # totallenRecv = struct.unpack('&gt;I', totallen)[0] # messagelen = totallenRecv - 4 message = server_socket.recv(1000) print message </code></pre> <p>Now, I'm not an expert on sockets (this might be the first question I've answered about them), but if you run this example, you get the expected output. What that tells me is that each send corresponds to a single <code>.recv</code>. Presumably, the <code>sockets</code> library is handling the details of the <em>length</em>. Of course, on the recieving side, it's quite possible that you <em>want</em> to know the length so that you can pass a proper maxlen. If this is the case, then I think you probably need to send 2 messages. The first message will have length 4 and be the integer length. The second message should have the data. e.g.:</p> <h1>client2.py</h1> <pre><code>import socket import sys import struct address = ('localhost', 6005) client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) client_socket.connect(address) messages = ["foobar", "barbaz", "bazquxfad", "Jimmy Carter"] for s in messages: totallen = len(s) pack1 = struct.pack('&gt;I', totallen) # the first part of the message is length client_socket.sendall(pack1) client_socket.sendall(s) </code></pre> <h1>server2.py</h1> <pre><code>import socket import sys import struct address = ('localhost', 6005) server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.bind(address) while True: print "Listening" totallen = server_socket.recv(4) totallenRecv = struct.unpack('&gt;I', totallen)[0] message = server_socket.recv(totallenRecv) print message </code></pre> <p>Now the serialization should be pretty easy. After all, proto buffers serialize to strings just fine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload