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

  • SEARCH
  • Home
  • 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 150795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:20:49+00:00 2026-05-11T09:20:49+00:00

I have a server that’s written in C, and I want to write a

  • 0

I have a server that’s written in C, and I want to write a client in python. The python client will send a string ‘send some_file’ when it wants to send a file, followed by the file’s contents, and the string ‘end some_file’. Here is my client code :

 file = sys.argv[1] host = sys.argv[2] port = int(sys.argv[3]) sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((host,port)) send_str = 'send %s' % file end_str = 'end %s' % file sock.send(send_str) sock.send('\n') sock.send(open(file).read()) sock.send('\n') sock.send(end_str) sock.send('\n') 

The problem is this :

  • the server receives the ‘send some_file’ string from a recv

  • at the second recv, the file’s content and the ‘end file’ strings are sent together

In the server code, the buffer’s size is 4096. I first noticed this bug when trying to send a file that’s less than 4096k. How can I make sure that the server receives the strings independently?

  • 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. 2026-05-11T09:20:50+00:00Added an answer on May 11, 2026 at 9:20 am

    With socket programming, even if you do 2 independent sends, it doesn’t mean that the other side will receive them as 2 independent recvs.

    One simple solution that works for both strings and binary data is to: First send the number of bytes in the message, then send the message.

    Here is what you should do for each message whether it is a file or a string:

    Sender side:

    • Send 4 bytes that holds the number of bytes in the following send
    • Send the actual data

    Receiver side:

    • From the receiver side do a loop that blocks on a read for 4 bytes
    • Then do a block on a read for the number of characters specified in the preceding 4 bytes to get the data.

    Along with the 4-byte length header I mentioned above, you could also add a constant size command type header (integer again) that describes what’s in the following recv.

    You could also consider using a protocol like HTTP which already does a lot of the work for you and has nice wrapper libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to have a server that can accept 2GB file over network,
I have an ASP.NET server that provides its client as an MSI download (similar
In our FreeBSD-environment where we have one server that acts as a file-server, we
I have server application that listens for clients. Let's client lost internet connection and
I have a server that receives a compressed string (compressed with zlib) from a
I have a server that waits for incoming client messages and uses UDP. When
I have a server that should send a broadcast transimission, which I have to
I have a server that exposes its services over rpc and a client that
i have a server that if i make a call to it will return
I Have a server that if I sent it https://MyDomain/Dir1/Dir2/login.html?u=1234&t=5678 will respond with an

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.