views:

759

answers:

5

I use this code for reading from socket :

int n ;
char buffer[256];
n = read(newsockfd, buffer, 255);
    if (n < 0)
    {
        error("ERROR reading from socket");
    }

if the data that must be read bigger than 255 byte (for example 1000) which change must be occured ?

I know change char buffer[1000], I need different solution .

+5  A: 

Just read several times from the socket, until you got all the data you want to receive.

For example to receive 1000 bytes, it could look like this (on success read returns the number of bytes read):

int received = 0;
while (received < 1000) {
  n = read(newsockfd, buffer, 255);
  // error checking...
  // do something with the partial data in "buffer"...
  received += n;
}
sth
in a loop, perhaps?
bmargulies
How to use loop what is loop's variable
SjB
I added an example. What exactly to do with the data in `buffer` in each iteration depends on how you want to process further what you are receiving.
sth
+1  A: 

Have you checked errno? http://linux.die.net/man/2/read

If you need more data, use a loop, no?

jldupont
+2  A: 

Use something like:

char buffer[256];
memset(buffer, 0, sizeof(buffer));
char* pBuf = buffer;
int bytesLeft = sizeof(buffer) - sizeof(char);

while(bytesLeft > 0)
{
    int n = read(newsockfd, pBuf, bytesLeft);
    if (n < 0)
    {
        error("ERROR reading from socket");
        break;
    }
    if(n == 0)
    {
        error("peer shutted down");
        break;
    }
    pBuf += n;
    bytesLeft -= n;
}
elder_george
+1 but...it would be better if there were a 'sizeof' in sight - 255 ==> sizeof(buffer)-1, I assume (consistent with the question). I'm not clear why there's a byte left unused; presumably for a terminal null in a string. Also, the Q asked about 'more than 255 bytes' but the answer doesn't do that. The 'while(true)' condition would be better as 'while (bytesLeft > 0)' and you can avoid the last condition in the loop. Should you worry about 'n == 0' (EOF - other end of socket gone away)?
Jonathan Leffler
How will this read more than 255 bytes? This code will read a maximum of 255 bytes; just that that it will read it in chunks. May be I am missing something. Please blast me if I am :)
Vaibhav
`char pBuf = buffer;` should be `char *pBuf = buffer;` or I don't think it would even compile.
billyswong
@Jonathan Leffler You're right on all accounts. I'll correct code.@billyswong yes, that was a typo. thx
elder_george
+1  A: 

Does changing the third parameter of read to 1000 work or not? i.e.

char buffer[1001];
read(newsockfd, buffer, 1000);

If the read function does not accept a count that large, you may read it multiple times. In the loop, keep track of how much characters you have read so far and use read(newsockfd, buffer+m, 1000-m);

billyswong
A: 

Adding to the solutions above, you can even read any number of bytes (assuming you don't have an idea beforehand how many Bytes you are expecting to be sent on the socket) by allocating some more memory to your buffer in which you are reading whenever you reach the maximum limit of the same.

Vaibhav
you mean with a realloc()?
Suppressingfire
Yes , can reallocate the same buffer array.
Vaibhav