当前位置: 技术问答>linux和unix
阻塞方式,read不返回!!!
来源: 互联网 发布时间:2015-12-10
本文导语: 建立了一个很基本的socket client,write了一个Get请求到网站的80端口,能够获取到服务器的请求,但是在利用read循环读取数据的时候,读取一些数据后read被阻塞,但是不返回? 而且,我换一个网站get,则能get到数据,...
建立了一个很基本的socket client,write了一个Get请求到网站的80端口,能够获取到服务器的请求,但是在利用read循环读取数据的时候,读取一些数据后read被阻塞,但是不返回?
而且,我换一个网站get,则能get到数据,read也能返回。
while ((rc = read(s, buf, BUF_LEN)) > 0)
{
printf("rc = %dn", rc);
writen(fd, ptr, rc);
}
其中s为一socket 描述符,fd为一文件句柄。
执行的时候输出:
rc = 1448
rc = 1448
rc = 1448
rc = 1448
rc = 1448
rc = 630
...
程序停在...处,read函数不返回!
请教为什么呢?
而且,我换一个网站get,则能get到数据,read也能返回。
while ((rc = read(s, buf, BUF_LEN)) > 0)
{
printf("rc = %dn", rc);
writen(fd, ptr, rc);
}
其中s为一socket 描述符,fd为一文件句柄。
执行的时候输出:
rc = 1448
rc = 1448
rc = 1448
rc = 1448
rc = 1448
rc = 630
...
程序停在...处,read函数不返回!
请教为什么呢?
|
阻塞模式下:read一定要读到数据才返回的,你这里停止是正常的
|
解释的很通。多写程序多试试。
阻塞方式下三种情况会返回:
1.读到了足够的字节
2.遇到EOF
3.收到signal
It is not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are actu-
ally available right now (maybe because we were close to end-of-file,
or because we are reading from a pipe, or from a terminal), or
because read() was interrupted by a signal.
非阻塞方式多一种:
4.无数据可读
EAGAIN Non-blocking I/O has been selected using O_NONBLOCK and no
data was immediately available for reading.
阻塞方式下三种情况会返回:
1.读到了足够的字节
2.遇到EOF
3.收到signal
It is not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are actu-
ally available right now (maybe because we were close to end-of-file,
or because we are reading from a pipe, or from a terminal), or
because read() was interrupted by a signal.
非阻塞方式多一种:
4.无数据可读
EAGAIN Non-blocking I/O has been selected using O_NONBLOCK and no
data was immediately available for reading.