1

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()
mskager
  • 51
  • 1
  • 2
  • 10
  • 2
    `message` is already a bytestring on Python 2. [To iterate in chunks see this](http://stackoverflow.com/a/434328/4279). Why do you need to send in chunks only to join it immediately on the server? It seems like [XY problem](http://www.perlmonks.org/index.pl?node_id=542341). – jfs Nov 22 '12 at 23:33

1 Answers1

9

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.

  1. How do you send 10 bytes at a time over a TCP connection (you are using socket.SOCK_STREAM)
  2. How do you send raw bytes using a Python socket.

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 calling socket.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:

from scapy.all import *

import socket
host = '192.168.0.x' #replace with your IP
my_port = 3002
message = "some string I want to send in chunks"

def chunks(lst, n):
    "Yield successive n-sized chunks from lst"
    for i in xrange(0, len(lst), n):
        yield lst[i:i+n]

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, my_port))
ss=StreamSocket(client_socket,Raw)
for chunk in chunks(message, 10):
    print "sending: " + chunk
    ss.send(Raw(chunk) )

client_socket.close()

server:

import socket
my_port=3002
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()
while (True):
    data = client_socket.recv(512)
    if (data):
        print data
    else:
        break
Community
  • 1
  • 1
m01
  • 9,033
  • 6
  • 32
  • 58
  • why not open and close client socket for every ten bytes? – Jean Jul 03 '19 at 18:58
  • It's not quite the same in the general case. Depending on the protocol that's running on top of the socket you may need to exchange headers again, login etc. In some cases what you propose might not even be possible, for example if you wanted to emulate uploading a larger file in `X` byte chunks via HTTP, because the file transfer would be aborted (at least with a naive implementation) – m01 Jul 04 '19 at 11:20