socket为send和recv设置超时时间

linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别  


UDP的socket在某些情况:如对方关闭时,本地可能sendto不出去数据,然后recvfrom就会被阻塞,这时就需要设置 这两个参数的值提高程序质量。

linux:

    struct timeval timeout={3,0};//3s
    int ret=setsockopt(sock_fd,SOL_SOCKET,SO_SNDTIMEO,(const char*)&timeout,sizeof(timeout));
    int ret=setsockopt(sock_fd,SOL_SOCKET,SO_RCVTIMEO,(const char*)&timeout,sizeof(timeout));

    如果ret==0 则为成功,-1为失败,这时可以查看errno来判断失败原因
    int recvd=recv(sock_fd,buf,1024,0);
    if(recvd==-1&&errno==EAGAIN)
   {
        printf("timeout\n");
   }


windows:
int timeout = 3000; //3s
   int ret=setsockopt(sock_fd,SOL_SOCKET,SO_SNDTIMEO,&timeout,sizeof(timeout));

   int ret=setsockopt(sock_fd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));


而solaris,则不支持。


from:  http://blog.csdn.net/aa2650/article/details/17417385

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
delphi socket call php socket 例子,可根据需要扩展写成聊天室、网站助理类似淘宝助理,有订单提醒。 <?php //确保在连接客户端时不会超时 set_time_limit(0); $port = 10081 ; $ip = '192.168.1.102'; // create a streaming socket, of type TCP/IP $sock = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP ); // set the option to reuse the port socket_set_option ( $sock , SOL_SOCKET , SO_REUSEADDR , 1 ); // "bind" the socket to the address to "localhost", on port $port // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc.. socket_bind ( $sock , $ip , $port ); // start listen for connections socket_listen ( $sock ); // create a list of all the clients that will be connected to us.. // add the listening socket to this list $clients = array( $sock ); while ( true ) { // create a copy, so $clients doesn't get modified by socket_select() $read = $clients ; // get a list of all the clients that have data to be read from // if there are no clients with data, go to next iteration if ( socket_select ( $read , $write = NULL , $except = NULL , 0 ) < 1 ) continue; // check if there is a client trying to connect if ( in_array ( $sock , $read )) { // accept the client, and add him to the $clients array $clients [] = $newsock = socket_accept ( $sock ); // send the client a welcome message socket_write ( $newsock , "这是一个delphi(客户端) socket 与 PHP_socket(服务器) 通信的例子 测试,交流QQ:410578660。 but ill make an exception :)\n" . "There are " .( count ( $clients ) - 1 ). " client(s) connected to the server\n" ); socket_getpeername ( $newsock , $ip ); echo "New client connected: { $ip } \n" ; // remove the listening socket from the clients-with-data array $key = array_search ( $sock , $read ); unset( $read [ $key ]); } // loop through all the clients that have data to read from foreach ( $read as $read_sock ) { // read until newline or 1024 bytes // socket_read while show errors when the client is disconnected, so silence the error messages $data = @ socket_read ( $read_sock , 1024 , PHP_NORMAL_READ ); // check if the client is disconnected if ( $data === false ) { // remove client for $clients array $key = array_search ( $read_sock , $clients ); unset( $clients [ $key ]); echo "client disconnected.\n" ; // continue to the next client to read from, if any continue; }
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值