返回7 就是正常的连接
我应该怎么得到连接的状态呢???
h_Accept // 用户连接成功后的句柄
h_Socket // socket的句柄
查了一下资料 用 getsockopt 这个API可以得到
但是这个返回值一直是-1
连接成功后为0
然后用户下线后值依旧是0
'sckClosed 0 关闭状态
'sckOpen 1 打开状态
'sckListening 2 侦听状态
'sckConnectionPending 3 连接挂起
'sckResolvingHost 4 解析域名
'sckHostResolved 5 已识别主机
'sckConnecting 6 正在连接
'sckConnected 7 已连接
'sckClosing 8 同级人员正在关闭连接
'sckError 9 错误
我应该怎么得到这些种种状态呢
5 个解决方案
#1
getsockopt
The getsockopt function retrieves a socket option.
int getsockopt(
SOCKET s,
int level,
int optname,
char FAR *optval,
int FAR *optlen
);
--------------------------
Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening.
SO_BROADCAST BOOL Socket is configured for the transmission of broadcast messages.
SO_CONDITIONAL_ACCEPT BOOL Returns current socket state, either from a previous call to setsockopt or the system default.
SO_DEBUG BOOL Debugging is enabled.
SO_DONTLINGER BOOL If TRUE, the SO_LINGER option is disabled.
SO_DONTROUTE BOOL Routing is disabled. Not supported on ATM sockets.
SO_ERROR int Retrieves error status and clear.
SO_GROUP_ID GROUP Reserved.
SO_GROUP_PRIORITY int Reserved.
SO_KEEPALIVE BOOL Keep-alives are being sent. Not supported on ATM sockets.
SO_LINGER LINGER structure Returns the current linger options.
SO_MAX_MSG_SIZE unsigned int Maximum size of a message for message-oriented socket types (for example, SOCK_DGRAM). Has no meaning for stream oriented sockets.
SO_OOBINLINE BOOL OOB data is being received in the normal data stream. (See section Windows Sockets 1.1 Blocking Routines and EINPROGRESS for a discussion of this topic.)
SO_PROTOCOL_INFO WSAPROTOCOL_INFO Description of protocol information for protocol that is bound to this socket.
SO_RCVBUF int Buffer size for receives.
SO_REUSEADDR BOOL The socket can be bound to an address which is already in use. Not applicable for ATM sockets.
SO_SNDBUF int Buffer size for sends.
SO_TYPE int The type of the socket (for example, SOCK_STREAM).
PVD_CONFIG Service Provider Dependent An opaque data structure object from the service provider associated with socket s. This object stores the current configuration information of the service provider. The exact format of this data structure is service provider
The getsockopt function retrieves a socket option.
int getsockopt(
SOCKET s,
int level,
int optname,
char FAR *optval,
int FAR *optlen
);
--------------------------
Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening.
SO_BROADCAST BOOL Socket is configured for the transmission of broadcast messages.
SO_CONDITIONAL_ACCEPT BOOL Returns current socket state, either from a previous call to setsockopt or the system default.
SO_DEBUG BOOL Debugging is enabled.
SO_DONTLINGER BOOL If TRUE, the SO_LINGER option is disabled.
SO_DONTROUTE BOOL Routing is disabled. Not supported on ATM sockets.
SO_ERROR int Retrieves error status and clear.
SO_GROUP_ID GROUP Reserved.
SO_GROUP_PRIORITY int Reserved.
SO_KEEPALIVE BOOL Keep-alives are being sent. Not supported on ATM sockets.
SO_LINGER LINGER structure Returns the current linger options.
SO_MAX_MSG_SIZE unsigned int Maximum size of a message for message-oriented socket types (for example, SOCK_DGRAM). Has no meaning for stream oriented sockets.
SO_OOBINLINE BOOL OOB data is being received in the normal data stream. (See section Windows Sockets 1.1 Blocking Routines and EINPROGRESS for a discussion of this topic.)
SO_PROTOCOL_INFO WSAPROTOCOL_INFO Description of protocol information for protocol that is bound to this socket.
SO_RCVBUF int Buffer size for receives.
SO_REUSEADDR BOOL The socket can be bound to an address which is already in use. Not applicable for ATM sockets.
SO_SNDBUF int Buffer size for sends.
SO_TYPE int The type of the socket (for example, SOCK_STREAM).
PVD_CONFIG Service Provider Dependent An opaque data structure object from the service provider associated with socket s. This object stores the current configuration information of the service provider. The exact format of this data structure is service provider
#2
谢谢楼s回答
也就是说我想得到那些状态 只改第三个参数 查看一下返回值就可以了吗??是这样吗 谢谢您的回答!!!
也就是说我想得到那些状态 只改第三个参数 查看一下返回值就可以了吗??是这样吗 谢谢您的回答!!!
#3
Windows socket中没有得到连接状态的函数,应用程序只需要按要求调用相关的函数即可.关于socket选项你可以参考《WinSock网络编程经络》第20章,有详细的说明,这里有一个得到选项的程序,解压后找GetSockOpt文件夹,下载地址:http://download.csdn.net/detail/geoff08zhang/4571358
#4
你列出来的那些状态,是vb扩展的,并不是标准的。换个地方就不一定有了。比如vc下面的MFC封装的socket,或者windows的socket API,都没有这些状态。
linux下面也一样没有(它更接近标准的socket,winsock不太标准,比如重叠IO,只能在windows下才有)。
linux下面也一样没有(它更接近标准的socket,winsock不太标准,比如重叠IO,只能在windows下才有)。
#5
可以通过调用函数返回值获得状态,只不过这种状态没有精确到什么侦听中。。接受中之类的状态
比如发送Send。。。返回TRUE(1)就成功了,如果返回FALSE(0)就表示网络有故障,通过GetLastError获得具体是什么原因即可
比如发送Send。。。返回TRUE(1)就成功了,如果返回FALSE(0)就表示网络有故障,通过GetLastError获得具体是什么原因即可
#1
getsockopt
The getsockopt function retrieves a socket option.
int getsockopt(
SOCKET s,
int level,
int optname,
char FAR *optval,
int FAR *optlen
);
--------------------------
Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening.
SO_BROADCAST BOOL Socket is configured for the transmission of broadcast messages.
SO_CONDITIONAL_ACCEPT BOOL Returns current socket state, either from a previous call to setsockopt or the system default.
SO_DEBUG BOOL Debugging is enabled.
SO_DONTLINGER BOOL If TRUE, the SO_LINGER option is disabled.
SO_DONTROUTE BOOL Routing is disabled. Not supported on ATM sockets.
SO_ERROR int Retrieves error status and clear.
SO_GROUP_ID GROUP Reserved.
SO_GROUP_PRIORITY int Reserved.
SO_KEEPALIVE BOOL Keep-alives are being sent. Not supported on ATM sockets.
SO_LINGER LINGER structure Returns the current linger options.
SO_MAX_MSG_SIZE unsigned int Maximum size of a message for message-oriented socket types (for example, SOCK_DGRAM). Has no meaning for stream oriented sockets.
SO_OOBINLINE BOOL OOB data is being received in the normal data stream. (See section Windows Sockets 1.1 Blocking Routines and EINPROGRESS for a discussion of this topic.)
SO_PROTOCOL_INFO WSAPROTOCOL_INFO Description of protocol information for protocol that is bound to this socket.
SO_RCVBUF int Buffer size for receives.
SO_REUSEADDR BOOL The socket can be bound to an address which is already in use. Not applicable for ATM sockets.
SO_SNDBUF int Buffer size for sends.
SO_TYPE int The type of the socket (for example, SOCK_STREAM).
PVD_CONFIG Service Provider Dependent An opaque data structure object from the service provider associated with socket s. This object stores the current configuration information of the service provider. The exact format of this data structure is service provider
The getsockopt function retrieves a socket option.
int getsockopt(
SOCKET s,
int level,
int optname,
char FAR *optval,
int FAR *optlen
);
--------------------------
Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening.
SO_BROADCAST BOOL Socket is configured for the transmission of broadcast messages.
SO_CONDITIONAL_ACCEPT BOOL Returns current socket state, either from a previous call to setsockopt or the system default.
SO_DEBUG BOOL Debugging is enabled.
SO_DONTLINGER BOOL If TRUE, the SO_LINGER option is disabled.
SO_DONTROUTE BOOL Routing is disabled. Not supported on ATM sockets.
SO_ERROR int Retrieves error status and clear.
SO_GROUP_ID GROUP Reserved.
SO_GROUP_PRIORITY int Reserved.
SO_KEEPALIVE BOOL Keep-alives are being sent. Not supported on ATM sockets.
SO_LINGER LINGER structure Returns the current linger options.
SO_MAX_MSG_SIZE unsigned int Maximum size of a message for message-oriented socket types (for example, SOCK_DGRAM). Has no meaning for stream oriented sockets.
SO_OOBINLINE BOOL OOB data is being received in the normal data stream. (See section Windows Sockets 1.1 Blocking Routines and EINPROGRESS for a discussion of this topic.)
SO_PROTOCOL_INFO WSAPROTOCOL_INFO Description of protocol information for protocol that is bound to this socket.
SO_RCVBUF int Buffer size for receives.
SO_REUSEADDR BOOL The socket can be bound to an address which is already in use. Not applicable for ATM sockets.
SO_SNDBUF int Buffer size for sends.
SO_TYPE int The type of the socket (for example, SOCK_STREAM).
PVD_CONFIG Service Provider Dependent An opaque data structure object from the service provider associated with socket s. This object stores the current configuration information of the service provider. The exact format of this data structure is service provider
#2
谢谢楼s回答
也就是说我想得到那些状态 只改第三个参数 查看一下返回值就可以了吗??是这样吗 谢谢您的回答!!!
也就是说我想得到那些状态 只改第三个参数 查看一下返回值就可以了吗??是这样吗 谢谢您的回答!!!
#3
Windows socket中没有得到连接状态的函数,应用程序只需要按要求调用相关的函数即可.关于socket选项你可以参考《WinSock网络编程经络》第20章,有详细的说明,这里有一个得到选项的程序,解压后找GetSockOpt文件夹,下载地址:http://download.csdn.net/detail/geoff08zhang/4571358
#4
你列出来的那些状态,是vb扩展的,并不是标准的。换个地方就不一定有了。比如vc下面的MFC封装的socket,或者windows的socket API,都没有这些状态。
linux下面也一样没有(它更接近标准的socket,winsock不太标准,比如重叠IO,只能在windows下才有)。
linux下面也一样没有(它更接近标准的socket,winsock不太标准,比如重叠IO,只能在windows下才有)。
#5
可以通过调用函数返回值获得状态,只不过这种状态没有精确到什么侦听中。。接受中之类的状态
比如发送Send。。。返回TRUE(1)就成功了,如果返回FALSE(0)就表示网络有故障,通过GetLastError获得具体是什么原因即可
比如发送Send。。。返回TRUE(1)就成功了,如果返回FALSE(0)就表示网络有故障,通过GetLastError获得具体是什么原因即可