SjB SjB - 22 days ago 8
C Question

Read function in Network Programming in C

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 .

sth sth
Answer

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;
}