Sending strings over socket

Hello guys!

I'm trying to send a string value from client to server. Basically, the client will send any string and the server will reply with Hello + any string you input.

The thing is that after I'm reading the string from keyboard, the client doesn't go further to the next line.After I force quit the client, on the server will appear information regarding the message. I think I'm doing something wrong when reading the string as I made a test and the below cout doesn't print the string value.Please see the below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
answer.empty();
input.empty();
 
 
cout<<"[client]Enter your name: ";
 
getline(cin, input);
cout<<input; //doesn't reach this
 
 
if(n = (send(sd, input.data(), input.size(), 0))<0)
{
    perror("[client]Write() error to server.\n");
    return errno;
}
I have also tried with cin and getline(cin, input,'\n');
The server and client need to agree on the length of the string.

You're just sending an unbounded stream of characters. The client doesn't know how much data to receive.
Last edited on
ok, but, before sending anything to server, shouldn't the program cout the input value? As I said, after I;m force quiting the client, on the server are displayed correct information about the received message and the returned message. Is just that the client remains stucked after I'm doing that getline. input is std::string type
Last edited on
In your view, how many bytes should the client read?

The string is variable length, so there needs to be some indication of how much data the client should read.

It's a common problem when working with streams. HTTP solves it by reading until it sees the "\n\n" sequence.

You have to devise some scheme where the client reads and knows when to stop. You have to think in terms of a stream of data and stop thinking about the string as an object.
Topic archived. No new replies allowed.