使用非阻塞套接字接收数据时出现问题 [英] Problem in receiving data with non blocking socket

查看:71
本文介绍了使用非阻塞套接字接收数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

 

我正在写一个以太网应用程序,我必须从非阻塞中接收数据socket。

I am writing a ethernet application in which I have to receive data from a non blocking socket.

接收信息对阻塞套接字是正确的。

The receive of information is proper with blocking socket.

但是我必须使用非阻塞套接字来接收数据。

But I have to use non blocking socket to receive the data.

当我在添加一些调试打印后创建非阻塞套接字时,我能够获得正确的数据。

When I will create the non blocking socket after adding some debug prints I am able to get proper data.

非阻塞套接字需要一些时序延迟?

Some timing delay is needed with non blocking socket?

我可以使用任何其他功能代替recv吗?

Can I use any other function in place of recv?

 

 

感谢

nadini

 

 

 

推荐答案

问题:

为什么你必须使用非阻塞套接字吗?

Why do you have to use a non-blocking socket?

代码看起来不像预期的那样?

What does the code look like that doesn't behave as expected?

你知道吗?什么是"非阻塞"是吗?

Do you know what "non-blocking" means?

听起来像你有类似的东西:

It sounds to me like you have something like:

int count = recv(socket,buf,bytecount,0)

int count = recv( socket, buf, bytecount, 0 )

然后您继续处理。 显而易见,如果您知道非阻塞套接字是什么,如果在您进行recv()调用时数据尚未到达,则您不会获得任何数据并且调用立即返回(当然返回零)。  ;
这就是非阻塞的定义:如果函数无法完成,则调用不会等待(阻塞);它只是立即返回。

and then you just continue with the processing.  It should be obvious, if you know what a non-blocking socket is that, if the data has not arrived when you make the recv() call, you get no data and the call returns immediately (returning zero, of course).  That's the definition of non-blocking: the call does not wait (block) if the function cannot complete; it simply returns immediately.

Paul T。


这篇关于使用非阻塞套接字接收数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆