Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as

Sent mesg: hello
Bytes Sent to Client: 6

bytes_received = recv(clientSockD, data, MAX_DATA, 0);
if(bytes_received)
{
                        send(clientSockD, data, bytes_received, 0);
                        data[bytes_received] = '\0';
                        printf("Sent mesg: %s", data);
                        cout<<"Bytes Sent to Client: "<<bytes_received<<endl;
}

clients send that data as follows:

cout<<"you: ";
fgets(clientInput, MAX_DATA, stdin); 
send(sockfd, clientInput, strlen(clientInput), 0);

Why?

bytes_received should be 5, not 6 ..... Any idea?

**************************************************************

Lets say we have:

cout<<"Enter a port: ";
cin>>portNum;

fgets(clientInput, MAX_DATA, stdin);

Does fgets() takes a "\0" from the previous input, as you know cin leaves \0 there? getline() does...

Hi,

send() and recv() won't distinguish with null character and space. It just send the number of bytes that is defined in the argument. Just do strlen() on sent and received bytes and you will get the answer.

Venky

Thanks!!!