3

I'm developing a server-client application using Winsock in c++ and have a problem.

For getting the message from the client by the server I use the code below.

int result;
char buffer[200];

while (true)
{
    result = recv(client, buffer, 200, NULL);

    if (result > 0)
        cout << "\n\tMessage from client: \n\n\t" << message << ";";
}

I send the message "Hello" from the client to the server. However the buffer is actually this:

HelloÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ

What am I missing?

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
user3530012
  • 720
  • 2
  • 20
  • 37
  • 1
    If you want a null terminator you have to add one yourself. – Alan Stokes Dec 17 '14 at 14:08
  • thank you. What is the null terminator exactly? and Let's say I add a null terminator or any thing like that. How may I retrieve the string from the buffer? would you please show me a sample code? – user3530012 Dec 17 '14 at 14:15
  • 1
    sending null-terminated strings over a network is inviting all kinds of security problems. Writing a protocol from scratch is a big task. You can save yourself a huge amount of work by using something like google protocol buffers to encode/decode the contents of packets. – Richard Hodges Dec 17 '14 at 14:23
  • std::string str(buffer); – RC Brand Dec 17 '14 at 14:29
  • @RC that has exactly the same problem as the original; it requires a null terminator which clearly isn't present. – Alan Stokes Dec 17 '14 at 14:55
  • I've actually fixed the problem by adding the null terminator '\0' at the end of the message sent by the client. – user3530012 Dec 17 '14 at 15:04
  • 1
    True Alan, my mistake. memcpy(buffer+result, "\0", 1); // This should work I guess. – RC Brand Dec 17 '14 at 15:09

4 Answers4

8

Since recv might not receive as many bytes as you told it, you typically use a function like this to receive specified number of bytes. Modified from here

int receiveall(int s, char *buf, int *len)
{
    int total = 0;        // how many bytes we've received
    int bytesleft = *len; // how many we have left to receive
    int n = -1;

    while(total < *len) {
        n = recv(s, buf+total, bytesleft, 0);
        if (n <= 0) { break; }
        total += n;
        bytesleft -= n;
    }

    *len = total; // return number actually received here

    return (n<=0)?-1:0; // return -1 on failure, 0 on success
} 

It's up to you to null terminate the string if you receive string which is not null terminated.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • I actually fixed the problem by adding the null terminator '\0' at the end of the message sent by the client. – user3530012 Dec 17 '14 at 14:35
  • 2
    @user3530012: yes but in general you should keep in mind probably that `recv` as I said might not receive all the data you tell it to (read the link I gave for more details) – Giorgi Moniava Dec 17 '14 at 14:39
4

The result tells you how many bytes were received. recv doesn't add a terminator since, in general, network data is binary data which might not be usable as a C-style string.

You can add a terminator yourself, if you know the message won't contain the termination character:

buffer[result] = 0;  // make sure the buffer is large enough

or make a string (or vector, or whatever) from it:

std::string message_str(message, result);

Note that what you receive might not be a single "message", especially if you're uses a stream protocol like TCP. It might contain more than one message, or just the start of one.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

memset(&receive[0], 0, sizeof(receive));
To clear the buffer

0

You didn't initialize your buffer

char buffer[200] = {0};

while (true)
{
    result = recv(client, buffer, 200, NULL);

    if (result > 0)
        cout << "\n\tMessage from client: \n\n\t" << message << ";";
    memset(buffer, 0, 200);
}
Tim
  • 4,790
  • 4
  • 33
  • 41
Natio2
  • 235
  • 1
  • 9