如何使用keep alive选项关闭和打开客户端套接字 [英] How do I close and open the client sockets using keep alive options

查看:185
本文介绍了如何使用keep alive选项关闭和打开客户端套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有MFC VC ++的TCP / IP套接字程序,它使用keep alive套接字选项与服务器通信。

我的客户端PC程序有以下几个过程:



1.制作客户端插座

- 使用WSAStartup

- 用ip地址/端口号创建客户端套接字



ClientSocket = socket(AF_INET,SOCK_STREAM,0);



2.尝试连接:

connect(ClientSocket,(struct sockaddr *)& ToServer,sizeof(ToServer));



3.下一步设置必要的选项

iMode = 1; //设置非阻塞模式

int iresult = ioctlsocket( ClientSocket,FIONBIO和& iMode);



4.添加另一个选项

if(setsockopt(ClientSocket,SOL_SOCKET,SO_REUSEADDR,(char) *)& bEnable,sizeof(bEnable))== SOCKET_ERROR)

...



5.此套接字不会出错我没有抓住或终止它。



如果我发现问题,如果我改变客户端插座 - 关闭或重新打开它。

1)如果我想要克制se clientsocket,使用closesocket()后我找不到结果成功。

2)我无法打开任何其他具有相同IP地址和端口号的客户端套接字选项RE_Use

3)在Winshark的监控日志中,

服务器每隔30秒重复向客户端发送一条消息(=此程序是我的电脑),我的客户端程序回复服务器。

4)即使终止我的客户端程序,并且在运行相同的程序之后,服务器也会向我发送相同的保持活动消息。我的客户端程序除现有套接字外没有其他套接字。



看起来好像服务器强迫我使用不使用额外的套接字而只使用相同的套接字。



问题:

哪个对等方有错误使用套接字选项?它是什么?



我想要做的一件事就是服务器发送和其他消息,我想关闭现有的客户端套接字,并创建一个新的客户端套接字。



谢谢。



我的尝试:



这个问题浪费了更多1周。

解决方案

请参阅<如何使用这些选项的说明< a href =https://msdn.microsoft.com/en-gb/library/windows/desktop/ms740621(v=vs.85).aspx>使用SO_REUSEADDR和SO_EXCLUSIVEADDRUSE(Windows) [ ^ ]。

I am coding a TCP/IP socket program with MFC VC++ that communicate with a server using keep alive socket options.
My client PC program have some processes as follows:

1. Making a client socket
- Use WSAStartup
- create client socket with ip address/ port number

ClientSocket = socket(AF_INET, SOCK_STREAM, 0);

2. trying to connect:
connect(ClientSocket, (struct sockaddr *)&ToServer, sizeof(ToServer));

3. Next setting necessary options
iMode = 1;// to set non-blocking mode
int iresult= ioctlsocket(ClientSocket, FIONBIO, &iMode);

4. Add another option
if (setsockopt(ClientSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&bEnable, sizeof(bEnable)) == SOCKET_ERROR)
...

5. This socket makes no error if I do not clise or terminate it.

After the problems are found if I change the client socket - close or re-open it.
1) If I want to close clientsocket, I cannot find the result success after use closesocket().
2) And I cannot open any other client sockets with same ip address and port number with option RE_Use
3) In the monitoring log of Winshark,
The server push keep alive messages to client(= this program is my pc) repeatedly every 30 seconds,and my client program reply to server.
4) Even though terminat my client program and after I run the same program, the server send same keep alive messages to me. My client program make no other sockets except existing socket.

It looks like as if server force to me to use not make additional socket but use only same used socket.

Question:
Which peer has mistake to use socket options? And what is it?

Only one thing I want to do is even server send and other messages, I want to close existing client socket, and make a new client socket.

Thank you.

What I have tried:

More 1 week wasted by this problem.

解决方案

See the explanation of how to use these options at Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE (Windows)[^].


这篇关于如何使用keep alive选项关闭和打开客户端套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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