closesocket写在while(1)循环外面是关闭exe程序时调用吗?
while(1) { memset(buf,0,sizeof(buf)); printf("send message: "); fgets(buf,sizeof(buf),stdin); if(nret=send(connection,buf,sizeof(buf),0)==SOCKET_ERROR) { printf("send message failed %dn",WSAGetLastError()); closesocket(connection); WSACleanup(); return 0; } printf("the data was send to %s successfullyn",inet_ntoa(seraddr.sin_addr)); if(strncmp(buf,"exit",sizeof("exit"))==0) { break; } memset(buf,0,sizeof(buf)); if(nret=recv(connection,buf,sizeof(buf),0)==SOCKET_ERROR) { printf("recv failed %dn",WSAGetLastError());//why not WSACleanup()...... } printf("the following data was received from %s successfullyn",inet_ntoa(seraddr.sin_addr)); printf(buf); } closesocket(connection); WSACleanup(); return 0; }在socket编程中,最后closesocket()的函数是放在while(1)循环的外面,是在关闭exe程序的时候才调用吗?还是怎么的?求教~谢谢~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个简单的收发数据消息的代码段
如果发送的字符串是"exit"就跳出循环
不过这句
14 if(strncmp(buf,"exit",sizeof("exit"))==0)