问题描述
我正在Windows上开发客户端服务器应用程序.
我正在使用Winsock API.
有什么功能可以检查套接字连接的状态吗?
我程序的流程就像.
1)建立与服务器的连接
2)用插座做些什么
3)保持空闲状态
4)将内容发送到服务器.
在第4步中将任何数据发送到服务器时,有什么方法可以让我检查连接是否仍然有效吗?
Hi,
I am developing a client server application on windows.
I am using winsock api.
Is ther any function by which i can check the status of the socket connection.
flow of my program is like.
1 ) Establish a connection to the server
2 ) Do something with the socket
3 ) Keep idle
4 ) Send something to the server.
While sending any data to server in 4th step, is ther any way for me to check whether the connection is still alive ?
推荐答案
char data;
recv(socket,&data,1, MSG_PEEK);//read one byte
但请确保您处于非阻止模式. (注意-使用PEEK可以确保接收的数据不出队,并且可以通过正确的接收例程进行读取/处理).这是Ulimiate TCP/IP使用的方法.
but make sure you are in non-blocking mode. (Note - using PEEK makes sure that the received data is not de-queued and can be read/processed by proper receive routine). This is the method used by Ulimiate TCP/IP.
这篇关于如何检查套接字连接是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!