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 8913593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:30:04+00:00 2026-06-15T04:30:04+00:00

I have a TCP server and client, with an established socket. Let’s say I

  • 0

I have a TCP server and client, with an established socket. Let’s say I have the following case:

SERVER:

char *fn = "John";
char *ln = "Doe";

char buffer[512];

strcpy(buffer, fn);
send(*socket, buffer, strlen(buffer), 0);
strcpy(buffer, ln);
send(*socket, buffer, strlen(buffer), 0);

CLIENT:

char *fn;
char *ln;

char buffer[512];

bytes = recv(*socket, buffer, sizeof(buffer), 0);
buffer[bytes] = '\0';
strcpy(fn, buffer);

bytes = recv(*socket, buffer, sizeof(buffer), 0);
buffer[bytes] = '\0';
strcpy(ln, buffer);

printf("%s", fn); 
printf("%s", ln);

Expected result:
I want to receive each string separately (as I am saving them in different variables), instead the first recv() gets both the fn “John” and ln “Doe” concatenated “JohnDoe”, and the program gets stuck on the second recv()… since I’ve already done the sending.

I tried the following:
– Adding \0 to the fn and ln strings like so:

char *fn = "John\0";
char *ln = "Doe\0";

But the problem persisted.

Is there some sort of condition I can impose so each string is received separately?

EDIT TO ADD MORE QUESTIONS:

1) Why does it in certain instances actually send them separately, and others sends them jointly?

2) Should I send() and recv() alternatively maybe? In some other parts of my code, it seems it is self regulating and only receiving the right strings at the right time without me having to check. Does this do the trick?

3) About sending a header with the length of the string I am about to send, how do I parse the incoming string to know where to split? Any examples would be great.

4) I will definitely check if I’m getting something bigger than my buffer, thanks for pointing to it.

Edit 2:

5) Since I’m always putting things in a buffer char array of size 512. Shouldn’t every send operation take the full size of the buffer even if I am sending a 1 letter string? That’s what confusing. I have “John” but I’m putting it in an array of size 512 and sending it, shouldn’t that fill up the send buffer, and so the recv() function empties the network buffer by also pulling the whole array of size 512?

  • 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-15T04:30:05+00:00Added an answer on June 15, 2026 at 4:30 am

    First, when the client calls recv(*socket, buffer, sizeof(buffer), 0);, it will receive up to sizeof(buffer) characters (here, 512) from the network. This is from the network buffer, and has nothing to do with how many times send has been called on the server. recv does not look for \0 or any other character – it’s a binary pull from the network buffer. It will read either the entire network buffer, or sizeof(buffer) characters.

    You need to check in the first recv to see if it has both the first and last name and handle it appropriately.

    You can do so either by having the server send the length of the strings as the first few bytes of a stream (a “header”), or by scanning the received data to see if there are two strings.

    You really need to perform checks whenever you call recv – what if the string is longer than 512 bytes? Then you need to call recv multiple time sto get the string. And what if you get the first string, and only half of the second string? Usually these two cases only happen when dealing with larger amounts of data, but I’ve seen it happen in my own code before, so it’s best to incorporate good checks on recv even when dealing with small strings like this. If you practice good coding tactics early on you won’t have as many headaches down the road.

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

Sidebar

Related Questions

I have the following code: (ns alephtest.core (:use lamina.core aleph.tcp aleph.formats)) (defn connection-established [socket]
I have a server-client application [TCP sockets, .NET 4.0].. the application about : do
I have built a simple TCP server and need to compare client input with
I have a client/server program in TCP written in C, and I would like
I have a Client program by tcp connection that send data to a server.
I have a server application that receives some special TCP packet from a client
I have two utilities written in C++, TCP/IP server and client that I have
When I have a TCP connection that uses packets.. (Server / Client Application) Should
I have a TCP Server/Client where the Server listens on port 5000 and the
I am trying to develop a client server TCP/IP application. I have a server

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.