10

Using C / C++ socket programming, and the "read(socket, buffer, BUFSIZE)" method. What exactly is the "buffer" I know that char and byte are the same thing, but does it matter how many elements the byte array has in it? Does the buffer need to be able to hold the entire message until the null character?

4
  • Is this null character sent by the sender of Your mesasage? Commented Sep 27, 2008 at 7:49
  • No, unfortunately I must append it at the end of the expected payload Commented Oct 21, 2008 at 19:31
  • Do You know the size of the message, or the end marked somehow? Commented Oct 23, 2008 at 16:16
  • That is a good point, I should know it because it is sent as part of the HTTP header Commented Oct 25, 2008 at 20:03

3 Answers 3

15

BUFSIZE should be equal to the size of your buffer in bytes. read() will stop reading when the buffer is full. Here is an example:

#define MY_BUFFER_SIZE 1024

char mybuffer[MY_BUFFER_SIZE];
int nBytes = read(sck, mybuffer, MY_BUFFER_SIZE);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but that only partially answers my question. I guess what I am after is what happens if the stream to be read is larger than the buffer?
Sorry, that wasn't very well phrased. I meant: you have to do your reads in a loop. Each time through the loop, you process what's in the buffer (even if only to append its content to a string). The loop finishes when read() returns 0.
CJY is right, and if you're reading from a stream socket you have to do it in a loop even if the buffer is big enough for the message, because you won't necessarily get all the data in one go. Reading a datagram of known size might be an exception, I don't remember.
7

As always, use sizeof when you have the chance. Using the built-in operator sizeof, you ask the compiler to compute the size of a variable, rather than specify it yourself. This reduces the risk of introducing bugs when the size of the actual variable is different from what you think.

So, instead of doing

#define BUFSIZE 1500
char buffer[BUFSIZE];
int n = read(sock, buffer, BUFSIZE);

you really should use

char buffer[1500];
int n = read(sock, buffer, sizeof buffer);

Notice how you don't need parenthesis around the argument to sizeof, unless the argument is the name of a type.

2 Comments

unwind: is that true in C or only C++? (Sorry, only familiar w/ C).
hey there, sorry to bother you on a very old question. I'm facing a similar problem and would really be happy if you could give me your opinion on what could be causing it.thks in advance!Hope you don't mind. In case, this is the question!
0

Your sockets implementation doesn't require the buffer to be big enough to hold the entire message for sure, but it might be convenient depending on what you are doing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.