CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  硬件/系统

关于端口释放的问题,高分求教~~

楼主zover(zover)2004-04-02 12:51:20 在 VC/MFC / 硬件/系统 提问

socket编程中,把一个socket与一个端口绑定,发送完数据后,再把该socket关闭,用的是   close语句。但是下次再运行该程序时,提示该端口被占用,每次只能换个端口重新运行,如何解决。  
  问题点数:0、回复次数:1Top

1 楼masterz(www.fruitfruit.com)回复于 2004-04-02 18:24:58 得分 0

bool   ShutdownConnection(SOCKET   sd)  
  {  
          //   Disallow   any   further   data   sends.     This   will   tell   the   other   side  
          //   that   we   want   to   go   away   now.     If   we   skip   this   step,   we   don't  
          //   shut   the   connection   down   nicely.  
          if   (shutdown(sd,   SD_SEND)   ==   SOCKET_ERROR)   {  
                  return   false;  
          }  
  const   int   kBufferSize   =   1024;  
   
          //   Receive   any   extra   data   still   sitting   on   the   socket.     After   all  
          //   data   is   received,   this   call   will   block   until   the   remote   host  
          //   acknowledges   the   TCP   control   packet   sent   by   the   shutdown   above.  
          //   Then   we'll   get   a   0   back   from   recv,   signalling   that   the   remote  
          //   host   has   closed   its   side   of   the   connection.  
          char   acReadBuffer[kBufferSize];  
  int   maxtry   =   100;  
          while   (maxtry>0)  
  {  
  maxtry--;  
                  int   nNewBytes   =   recv(sd,   acReadBuffer,   kBufferSize,   0);  
                  if   (nNewBytes   ==   SOCKET_ERROR)    
  {  
                          return   false;  
                  }  
                  else   if   (nNewBytes   >   0)    
  {  
  /*  
  char   msgbuf[1024];  
  wsprintf(msgbuf,"%s:%d   FYI,   received   %d   unexpected   bytes   during   shutdown.",  
  __FILE__,__LINE__,nNewBytes);  
  OutputDebugString(msgbuf);  
  */  
  //break;  
                  }  
                  else    
  {  
                          //   Okay,   we're   done!  
                          break;  
                  }  
          }  
   
          //   Close   the   socket.  
          if   (closesocket(sd)   ==   SOCKET_ERROR)   {  
                  return   false;  
          }  
   
          return   true;  
  }Top

相关问题

  • 请教高手:FreeBSD下什么命令能释放系统的端口
  • 关于端口资源没有释放的问题。望高手赐教!
  • 【100高分】什么是端口欺骗?
  • SQL SERVER 2000 端口问题。高分。。。。。。
  • 如何释放一个端口?
  • 关于clientsocket控件释放端口的问题,各位高手有什么好的解决办法没有?
  • 高分求解:用java怎么扫描端口?一定给分
  • 高分!!硬件320h端口资料谁有呀??
  • 高分求解!怎样编程关闭一个端口?
  • 怎样得到端口的地址。高分呀。

关键词

  • 端口
  • socket
  • nnewbytes
  • kbuffersize
  • maxtry
  • msgbuf
  • sd
  • shutdown
  • received
  • close

得分解答快速导航

  • 帖主:zover

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo