I have simple tcp socket program and I would like to send strings in chunks of 10 bytes. The server will join the chunks.
However I’m not sure how to split a string into binary and how to send the chunks of binaries. Instead of sending 512 bytes at one time I want to send 10 byte several times.
I have found a module Pickle that can serialize data into bytestrings (?) but how do I apply socket.send() on this?
Server:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", my_port))
server_socket.listen(5)
client_socket, address = server_socket.accept()
data = client_socket.recv(512)
Client:
message = "some string I want to send in chunks"
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, my_port))
client_socket.send(message)
client_socket.close()
First of all, your code is not actually sending 512 bytes at a time, it’s receiving 512 bytes at a time.
Now, I think you’re really asking two questions.
socket.SOCK_STREAM)Let’s answer 2. first: If you call socket.send on a byte string, it should be sent out in binary as TCP payload.
For 1., the simplest approach would be to split the data into chunks (now that you know you’re operating on strings, you can simply do that using the slice operations (see the Python tutorial on strings – e.g.
s[0:10],s[10:20]etc). Next, you need to ensure these slices are sent individually. This could be done by callingsocket.send, but the problem is, that your TCP/IP stack may group these into packets even if you don’t want it to – you have after all asked it to provide you with a stream socket,socket.SOCK_STREAM. If you were writing to a file, what you’d do in this case is you’d flush the file, but this does not appear to be easy for Python sockets (see this question).Now, I think the answer to that question, saying it’s impossible, is not quite right. It appears that Scapy will let you send 10 byte TCP chunks (I got the chunks() function from here). I checked it in wireshark and tried it multiple times with consistent results, but I didn’t check the implementation to make sure this is guaranteed to happen.
You should probably ask yourself why you want to send data in chunks of 10 bytes, rather than let TCP deal with what it was designed for, and consider using delimiters.
Anyway, here’s the code using Scapy (fun fact: it looks like running this does not require root privileges):
client:
server: