免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5029 | 回复: 2
打印 上一主题 下一主题

socket编程中的select模型问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-24 12:51 |只看该作者 |倒序浏览
在socket编程时使用select模型,如何判断对方主机已经关闭?



  1. // sockselect.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"

  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <tchar.h>  //for UNICODE
  7. #include <io.h>

  8. #define SERVER_PORT 2020

  9. void ReadSocket(SOCKET s)
  10. {
  11.         char recvbuff[1024];
  12.         int ret,
  13.                 nLeft,
  14.                 idx;
  15.         nLeft = 30;
  16.         idx = 0;
  17.         while(nLeft>0)
  18.         {
  19.                 ret = recv(s, &recvbuff[idx], nLeft, 0);
  20.                 if(ret==SOCKET_ERROR)
  21.                 {
  22.                 }
  23.                 idx += ret;
  24.                 nLeft -= ret;
  25.         }
  26. }

  27. int main(int argc, char* argv[])
  28. {

  29.         SOCKET   s = INVALID_SOCKET;
  30.         struct sockaddr_in ClientSAddr;
  31.         WSADATA  WSStartData;
  32.         fd_set fdread, fdexcept;
  33.         int ret;
  34.         char     DefaultIPAddr[] = "127.0.0.1";

  35.         // time inteval to check sockets
  36.         struct timeval tv;

  37.         // create a socket and connect it to server
  38.         WSAStartup(MAKEWORD(1,1), &WSStartData);
  39.        
  40.         // Init Socket
  41.         s = socket(AF_INET,SOCK_STREAM,0);
  42.        
  43.         // Init Address
  44.         memset (&ClientSAddr, sizeof(ClientSAddr),0);
  45.         ClientSAddr.sin_family = AF_INET;
  46.         ClientSAddr.sin_addr.S_un.S_addr = inet_addr(DefaultIPAddr);
  47.         ClientSAddr.sin_port = htons(SERVER_PORT);
  48.        
  49.         // Connect
  50.         connect(s, (struct sockaddr *)&ClientSAddr, sizeof(ClientSAddr));

  51.         //
  52.         tv.tv_sec = 5;
  53.         tv.tv_usec = 0;

  54.         while(TRUE)
  55.         {
  56.                 FD_ZERO(&fdread);
  57.                 FD_ZERO(&fdexcept);

  58.                 FD_SET(s, &fdread);
  59.                 FD_SET(s, &fdexcept);

  60.                 ret=select(0, &fdread, NULL, &fdexcept, &tv);

  61.                 if(ret==SOCKET_ERROR)
  62.                 {
  63.                         printf("Error!");
  64.                 }

  65.                 if(ret > 0)
  66.                 {
  67.                         if(FD_ISSET(s, &fdexcept))
  68.                         {
  69.                                 printf("s in exception, quit...");
  70.                                 break;
  71.                         }
  72.                         if(FD_ISSET(s, &fdread))
  73.                         {
  74.                                 //
  75.                                 printf("There are %d socket(s) ready, we read it from socket...\n", ret);
  76.                                 ReadSocket(s);
  77.                                 //printf("FD_SET");
  78.                         }
  79.                 }


  80.         }
  81.         return 0;
  82. }

复制代码


像这样一段代码,如果服务器正常发送数据,那么一切都很正常,一旦服务器socket关闭,客户机就会进入假死状态,CPU占用率达100%,如何判断对方关闭这一事件的发生呢?谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-09-24 13:04 |只看该作者
听说有个half close什么的

论坛徽章:
0
3 [报告]
发表于 2006-09-24 13:23 |只看该作者
若对端sock关闭,接收会读到0,发送会引发SIGPIPE
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP