-
Using recv() and send()
I want to write client-server program. The server should send many strings (messeges) and get response from the user on the client-side, here is in example:
on the server-side:
....
char buffer[buff_size];
......
recv(...buffer...);
.....
sprintf(buffer,"...");
send(....buffer....);
.......
sprintf(buffer,"...");
send(....buffer....);
.......
sprintf(buffer,"...");
send(....buffer....);
.......
sprintf(buffer,"...");
send(....buffer....);
.......
recv(buffer);
on the client-side:
....
char buffer[buff_size];
......
do{
fgets(buffer, stdin);
send(...buffer...);
...
recv(...buffer...);
printf("%s", buffer); //I don't know if printf() allow me to get all the transmitted buffer from the server-side?!!
}while(1)
any help appritiated?!!
-
TCP/IP is a stream based protocol, which means you need to define message boundaries/lengths. A common way to do this is to send a length indicator through the socket first, so that the app at the other end knows how much data to expect. The other way is to use a specific byte value as a terminator, eg a \0. I think there are some examples of this code around, see what you can find. If you need more info, ask away.
-
Thanks mate.
In case of using '\0', as a teminator of send. does this may cause the client hung and not recieving the other strings may be send after the first one?!!
-
>>does this may cause the client hung and not recieving the other strings may be send after the first one?!!
I think the answer is yes, if I understand the question right.
Using a \0 as a terminator makes life a little different, you need to handle the buffering correctly. Read my posts here:
http://cboard.cprogramming.com/showt...333#post321228
http://cboard.cprogramming.com/showt...333#post321434