阻塞套接字 [英] blocking sockets

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

问题描述

我不确定这是否适合此问题。我是UDP通信的新手。我正在使用客户端服务器C ++应用程序,该应用程序使用UDP在Windows和Linux环境之间进行通信。 Windows是客户端。客户端和服务器
能够发送/接收数据包,但稍后服务器和客户端recvfrom()都会调用阻塞。我做了一些调查并使用select call修改了接听电话,然后才收到下面的数据,只有在数据时才读取数据 是可用的。下面是
客户端调用,服务器代码类似。我想在这种情况下有数据可供阅读但是调用块。

I am not sure if this is the appropriate forum for this question. I am new to UDP communication. I am working on a client server C++ app that communicates between a Windows and Linux environment using UDP. Windows is the client. The client and server are able to send/receive packets but later both the server and client recvfrom() calls block. I did some investigation and modified the receive call using select call before receiving like below to read data only when data  is available. Below is the client call and the server code is similar. I guess in this case there is data available for reading but the call blocks.

int result = 0;

fd_set  readfds;
do
{
FD_ZERO(& read_flags);
FD_SET(套接字,& read_flags);
result = select(socket + 1,& read_flags,NULL,NULL,NULL);

} while(结果== -1&& errno == EINTR);
if(result> 0)
{
if(FD_ISSET(socket,& read_flags))
{

fd_set  readfds; do { FD_ZERO ( &read_flags ); FD_SET ( socket, &read_flags ); result = select ( socket + 1, &read_flags, NULL, NULL, NULL ); } while ( result == -1 && errno == EINTR ); if( result > 0 ) { if(FD_ISSET( socket,&read_flags )) {

nbytes = recvfrom(socket,buff ,bufflen,0,(struct sockaddr *)& si_other,& slen);

}
}

nbytes = recvfrom ( socket, buff, bufflen, 0, (struct sockaddr*)&si_other, &slen ); } }






但即便如此这个修改recvfrom()调用有时不会返回和阻塞。还要注意,由于select调用有时不返回,因此不调用recvfrom()调用。我也尝试过为select设置一个时间,但是没有
解决问题。

But even with this modification the recvfrom() call does not return sometimes and blocks. It is also noted that since the select call does not return sometimes, the recvfrom() call is not invoked. I also tried by setting a time out for select but that doesn't solve the issue.

这背后的原因是什么?

What could be the reason behind this?

还有什么相当于  fcntl()linux调用和常量之类的  Windows中的O_NONBLOCK和F_GETFL?

Also what is the equivalent of  fcntl() linux call and constants like  O_NONBLOCK and F_GETFL in Windows?

推荐答案

您好,

  UDP通常在您的应用请求信息时使用,并且不需要

 UDP is normally used when your app requests information and does not require

进一步的通信,即登录/密码,补丁信息等。 当应用程序需要与服务器进行持续通信时,TCP使用

further communications, ie login/password, patch Information, etc.  TCP is used

。 downLoads,

when the app needs continuous communications with the Server, ie. downLoads,

在线游戏等。

 请查看以下信息,链接如下,并检查arcticle中提供的示例

 Please review the following information, link below, and examine the examples

  https://msdn.microsoft.com/en-us/library/c19ex43h%28v=vs.110%29。 aspx?f = 255& MSPPError = -2147217396

 https://msdn.microsoft.com/en-us/library/c19ex43h%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

 谢谢:)


这篇关于阻塞套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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