Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:16:55+00:00 2026-06-14T18:16:55+00:00

I have simple tcp socket program and I would like to send strings in

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:16:56+00:00Added an answer on June 14, 2026 at 6:16 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple TCP socket client and server application. They are communicating using
I have a simple TCP connection with a server and a client program. I
I have been working on a (relatively) simple tcp client/server chat program for my
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I have built a simple TCP server and need to compare client input with
I have a program (written in C/C++) that behaves as a socket server. I'd
In sockets I have written the client server program. First I tried to send
I have written a very simple socket server. It listens in post 63254. First
I have a small server program that accepts connections on a TCP or local
I have a very simple TCP server written in C. It runs indefinitely, waiting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.