如何判断一个SOCKET端口是可用的?
调用哪些WINSOCK的函数可以判断哪个端口是可用的,又或者如何向WINDOWS查询一个可用的端口? 问题点数:200、回复次数:8Top
1 楼Bind(宁静雪:找到你,绑定你!)(再战江湖)回复于 2002-05-30 11:05:36 得分 50
bind之后,WSAGetLastErrorTop
2 楼xuying()回复于 2002-05-30 11:05:55 得分 150
可能没有现成的函数。但可以用GetTcpTable实现:
GetTcpTable
The GetTcpTable function retrieves the TCP connection table.
DWORD GetTcpTable(
PMIB_TCPTABLE pTcpTable, // buffer for the connection table
PDWORD pdwSize, // size of the buffer
BOOL bOrder // sort the table?
);
Parameters
pTcpTable
[out] Pointer to a buffer that receives the TCP connection table as a MIB_TCPTABLE structure.
pdwSize
[in, out] On input, specifies the size of the buffer pointed to by the pTcpTable parameter.
On output, if the buffer is not large enough to hold the returned connection table, the function sets this parameter equal to the required buffer size.
bOrder
[in] Specifies whether the connection table should be sorted. If this parameter is TRUE, the table is sorted in the order of:
Local IP address
Local port
Remote IP address
Remote port
Return Values
If the function succeeds, the return value is NO_ERROR.
If the function fails, use FormatMessage to obtain the message string for the returned error.
Requirements
Windows NT/2000 or later: Requires Windows NT 4.0 SP4 or later.
Windows 95/98/Me: Requires Windows 98 or later.
Header: Declared in Iphlpapi.h.
Library: Use Iphlpapi.lib.
GetTcpTable可以得到所有已连接或者listen的端口号,相当于netstat。没有出现在里面的端口号就是可用的。Top
3 楼freelybird(阿愚)回复于 2002-05-30 11:30:20 得分 0
agreeTop
4 楼mrlp()回复于 2002-06-04 15:10:59 得分 0
请问上面说得函数和结构是哪个版本支持的?
我用的是vc6+msdn6
甚至找不到函数和结构的任何信息Top
5 楼snsins(初雪草)回复于 2002-06-04 15:31:59 得分 0
端口可以重用
不存在端口可用不可用的问题Top
6 楼snsins(初雪草)回复于 2002-06-04 15:33:31 得分 0
GetTcpTable函数信息可在Platform SDK文档里找到Top
7 楼Yokishiro(狂狼啸月)回复于 2002-06-04 16:07:19 得分 0
在IP Help API里面
Top
8 楼ychener(贫血)回复于 2002-06-04 17:31:55 得分 0
Create One SOcket and Bind Port
看看函数返回以及 GetLastError就知道了Top




