myself
myself

Reputation: 89

problems with double send/recv

i've a problem with send/recv: if the client send two strings, server receive the two in only one recv and the second recv attend a third send from the client (that i don't want to send). I would like to send 2 strings and receive 2 strings. How can i do?

My code: CLIENT

char login[] = "admin";
char password[] = "admin";

send(sd, login, strlen(login), 0);
send(sd, password, strlen(password), 0);

SERVER:

bzero(login,MAX);
bzero(password,MAX);

recv(sd_client, login, sizeof(login), 0);
recv(sd_client, password, sizeof(password), 0);

Upvotes: 0

Views: 1062

Answers (2)

jlledom
jlledom

Reputation: 650

I assume you're using two char arrays for the variables "login" and "password" in your server side.

In this case, for create them, you had to give them a fixed size in its declaration, what is the size of this arrays?, what is the size of the MAX macro?

In your client you're using five-length arrays, if the MAX macro is 10-bytes length of more, then the first recv() will read the both sendings.

Upvotes: 1

Martin James
Martin James

Reputation: 24847

TCP cannot send/recv strings. TCP cannot send/recv messages longer than one byte. TCP cannot send/recv structs longer than one byte.

TCP transport is a byte stream.

If you want to transfer anything more complex than one byte, you need an extra protocol on top, hence HTTP, SMTP, etc. etc. protocols.

If you specifically want to send null-terminated strings, for example, you need to buffer and concatenate received data until a null is detected - then you have your 'C'-style string and can proceed to assemble the next string.

Rgds, Martin

Upvotes: 3

Related Questions