Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending and receiving strings over TCP socket separately

Tags:

c

tcp

sockets

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?

like image 716
user1202888 Avatar asked Dec 09 '22 19:12

user1202888


1 Answers

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.

like image 108
cegfault Avatar answered Dec 11 '22 08:12

cegfault