开发者

Socket data length questions

I have a couple of questions related to the following code:

char buffer[256];
memset(buffer,0,256);

read(socket_fd,buffer,255);

The questions:

  1. Why I read 255 not 25开发者_如何转开发6 ?
  2. Let's say I want to send the word: "Cool" from the client to the server. How many bytes should I write "in client" and how many bytes should i read "in the server"?

I'm really confused.


You already have good answers here, but I think there's a concept we should explain.

When you send data through streams (that is, something that writes a number of bytes from one end, and those bytes can be read in the same order in the other end), you almost always want to know when to stop reading. This is mandatory if you'll send more than one thing: when does the first message stop, and the second begin? In the stream, things get mixed up.

So, how do we delimit messages? There are three simple ways (and many other not so simple ones, of course):

1 Fixed-length messages: If you know beforehand that every message is, say, 10-bytes long, then you don't have a problem. You just read 10 bytes, and the 11th one will be part of another message. This is very simple, but also very rigid.

2 Delimiting characters, or strings: If you are sending human-readable text, you might delimit your messages the same way you delimit strings in your char*: putting a 0 character at the end. That way, when you read a 0, you know the message ended and any remaining data in the stream belongs to another message.

This is okay for ascii text, but when it comes to arbitrary data it's also somewhat rigid: there's a character, or a sequence of characters, that your messages can't contain (or your program will get confused as to where a message ends).

3 Message headers: This is the best approach for arbitrary length, arbitrary content messages. Before sending any actual message data, send a fixed-length header (or use technique nr 2 to mark the end of the header), specifying metadata about your message. For example, it's length.

Say you want to send the message 'Cool', as you said. Well, first send a byte (or a 2-byte short, or a 4-byte integer) containing '4', the length of the message, and receive it on the other end. You know that before any message arrives, you must read 1 byte, store that somewhere and then read the remaining specified bytes.

A simplified example:

struct mheader {
    int length;
}

// (...)

struct mheader in_h;
read(fd, &in_h, sizeof(struct mheader);

if (in_h.length > 0) {
    read(fd, buffer, in_h.length)
}

In actual use, remember that read doesn't always read the exact amount of bytes you request. Check the return value to find out (which could be negative to indicate errors), and read again if necessary.

Hope this helps. Good luck!


  1. So that the buffer retains the NUL at the end, as extra insurance against string overflows. Reading 256 would allow it to get overwritten.

  2. You would write five bytes. Either write "Cool\0", or write 4 (the length) followed by the 4 characters in "Cool". Read all of it, and figure out the length after.


You look at the return value from read(); it tells you how many bytes were read.

You use the number of bytes read when you want to write the same data.

You don't have to use 255 in the read unless you definitely want to be able to put a NUL at the end - but since you know how many bytes were read, you won't go beyond that anyway. So, the 255 is an insurance policy against carelessness by the programmer.

The memset() is likewise most an insurance policy against carelessness by the programmer - it is not really necessary, unless you want to mask out previous sensitive data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜