- 论坛徽章:
- 0
|
以下SOCKET的读取代码在以前用一直没有问题,但这次做一个项目时候报错了:read socket error!: Invalid argument
如果不用select()轮询的话就能读取,请各位大虾指导一下是什么原因。
代码:
Int readBySize(Int socket,char *buff, Int size)
{
struct timeval timeout;
struct fd_set rmask;
Int status =0, pos = 0,n;
while(1)
{
/* Set timeout interval.*/
timeout.tv_sec = atoi(TvSec); /*seconds*/
timeout.tv_usec = 0; /* microseconds*/
FD_ZERO(&rmask);
FD_SET(socket, &rmask);
status = select(socket + 1, &rmask, NULL, NULL, &timeout);
switch (status)
{
case -1:
perror("read socket error!" ;
return -1;
case 0:
perror("read socket timeout!" ;
return -1;
default:
n = read(socket, buff + pos , size - pos);
if(n<=0) return -1;
pos += n;
if(pos == size)return 0;
}
}
} |
|