本帖最后由 ceoguang 于 2011-11-27 01:35 编辑
有这个问题,暂时无解。。
itljl 发表于 2011-11-21 19:42
有可能无解吗?
只是高手们不肯出招而已,设置为非阻塞方式连接就会解决你的问题.
看代码!不解释
#AutoIt3Wrapper_UseX64=n
Global Const $SOCKET_ERROR = -1
Global Const $WSADESCRIPTION_LEN = 256
Global Const $WSASYS_STATUS_LEN = 128
Global Const $FIONBIO = 2147772030
Global Const $SD_RECEIVE = 0, $SD_SEND = 1, $SD_BOTH = 2
Global Const $AF_UNSPEC = 0, $AF_INET = 2, $AF_IPX = 6, $AF_APPLETALK = 16, $AF_NETBIOS = 17, $AF_INET6 = 23, $AF_IRDA = 26, $AF_BTH = 32
Global Const $SOCK_STREAM = 1, $SOCK_DGRAM = 2, $SOCK_RAW = 3, $SOCK_RDM = 4, $SOCK_SEQPACKET = 5
Global Const $IPPROTO_ICMP = 1, $IPPROTO_IGMP = 2, $BTHPROTO_RFCOMM = 3, $IPPROTO_TCP = 6, $IPPROTO_UDP = 17, $IPPROTO_ICMPV6 = 58, $IPPROTO_RM = 113
Global Const $tagMIB_IFROW = _
'WCHAR wszName[256];' & _
'DWORD dwIndex;' & _
'DWORD dwType;' & _
'DWORD dwMtu;' & _
'DWORD dwSpeed;' & _
'DWORD dwPhysAddrLen;' & _
'BYTE bPhysAddr[8];' & _
'DWORD dwAdminStatus;' & _
'DWORD dwOperStatus;' & _
'DWORD dwLastChange;' & _
'DWORD dwInOctets;' & _
'DWORD dwInUcastPkts;' & _
'DWORD dwInNUcastPkts;' & _
'DWORD dwInDiscards;' & _
'DWORD dwInErrors;' & _
'DWORD dwInUnknownProtos;' & _
'DWORD dwOutOctets;' & _
'DWORD dwOutUcastPkts;' & _
'DWORD dwOutNUcastPkts;' & _
'DWORD dwOutDiscards;' & _
'DWORD dwOutErrors;' & _
'DWORD dwOutQLen;' & _
'DWORD dwDescrLen;' & _
'CHAR bDescr[256];'
Global Const $tagSockaddr_in = 'ushort family;ushort port;long address;char zero[8];'
Global Const $tagWSADATA = 'ushort wVersion;' & _
'ushort wHighVersion;' & _
'char szDescription[' & $WSADESCRIPTION_LEN + 1 & '];' & _
'char szSystemStatus[' & $WSASYS_STATUS_LEN + 1 & '];' & _
'ushort iMaxSockets;' & _
'ushort iMaxUdpDg;' & _
'ptr lpVendorInfo;'
Global Const $tagtimeval = 'int tv_sec;int tv_usec;'
Global Const $tagFD_SET = 'uint;uint[64];'
Global $Ws2_32_dll = -1
Local $mSocket, $iResult, $tSockaddr_in, $tTime, $tFD_SET
Local $iTime
WSAStartup()
If @error Then Exit (MsgBox(16, '错误', '网络初始化失败'))
$mSocket = socket()
If $mSocket = -1 Then Exit (MsgBox(16, '错误', '套字节创建失败'))
$iResult = ioctlsocket($mSocket, $FIONBIO, 1);设置为非阻塞模式
If $iResult <> 0 Then MsgBox(16, '错误', '设置非阻塞模式失败')
$tSockaddr_in = DllStructCreate($tagSockaddr_in)
DllStructSetData($tSockaddr_in, 1, $AF_INET)
DllStructSetData($tSockaddr_in, 2, htons(3456))
DllStructSetData($tSockaddr_in, 3, inet_addr('192.168.188.2'))
connect($mSocket, DllStructGetPtr($tSockaddr_in), DllStructGetSize($tSockaddr_in));此处WSAGetLastError()会产生10035错误,表示一个非阻塞的连接正在进行.这是一个正常的错误,不必理会
$tTime = DllStructCreate($tagtimeval)
DllStructSetData($tTime, 1, 5);5秒 ==============================>>>>单位秒
;DllStructSetData($tTime, 2, 0)0 =============================>>>>>单位微秒
$tFD_SET = DllStructCreate($tagFD_SET)
$iTime = TimerInit()
DllStructSetData($tFD_SET, 1, 1)
DllStructSetData($tFD_SET, 2, $mSocket, 1)
$iResult = _select(0, DllStructGetPtr($tFD_SET), 0, DllStructGetPtr($tTime))
If $iResult <= 0 Then Exit(MsgBox(0, '', '连接超时,程序退出'))
MsgBox(64,'连接成功','耗时:' & TimerDiff($iTime))
ioctlsocket($mSocket, $FIONBIO, 1);设置回阻塞模式
Func MAKEWORD($LoByte, $HiByte)
Return BitOR($LoByte, 0x100 * $HiByte)
EndFunc ;==>MAKEWORD
Func MakeLong($LoWord, $HiWord)
Return BitOR($HiWord * 0x10000, BitAND($LoWord, 0xFFFF))
EndFunc ;==>MakeLong
Func HiWord($Long)
Return BitShift($Long, 16)
EndFunc ;==>HiWord
Func WSAGetLastError()
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $Ret = DllCall($Ws2_32_dll, 'int', 'WSAGetLastError')
If @error Then SetError(@error)
Return $Ret[0]
EndFunc ;==>WSAGetLastError
Func ioctlsocket($hSocket, $iCmd, $pArgp)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $Ret = DllCall($Ws2_32_dll, 'int', 'ioctlsocket', 'uint', $hSocket, 'long', $iCmd, 'ulong*', $pArgp)
If @error Then Return SetError(@error, 0, -1)
Return $Ret[0]
EndFunc ;==>ioctlsocket
Func _select($preadfds, $pWritefds, $pexceptfds, $pTimeout)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'select', 'int', 0, 'ptr', $preadfds, 'ptr', $pWritefds, 'ptr', $pexceptfds, 'ptr', $pTimeout)
If @error Then Return SetError(@error)
Return $aResult[0]
EndFunc ;==>_select
Func WSAStartup()
$Ws2_32_dll = DllOpen('Ws2_32.dll')
If $Ws2_32_dll = -1 Then
Return SetError(-2, 0, 0)
EndIf
Local $WSADATA = DllStructCreate($tagWSADATA), $Ret
$Ret = DllCall($Ws2_32_dll, 'int', 'WSAStartup', 'short', MAKEWORD(2, 2), 'ptr', DllStructGetPtr($WSADATA))
Return SetError($Ret[0], $WSADATA = 0, $Ret[0] = 0)
EndFunc ;==>WSAStartup
Func socket($iAddressFamily = $AF_INET, $iType = $SOCK_STREAM, $iProtocol = $IPPROTO_TCP)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $hSocket = DllCall($Ws2_32_dll, 'uint', 'socket', 'int', $iAddressFamily, 'int', $iType, 'int', $iProtocol)
If @error Then Return SetError(@error, 1, -1)
Return SetError(WSAGetLastError(), 0, $hSocket[0])
EndFunc ;==>socket
Func connect($hSocket, $lpsockaddr, $iLen)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'connect', 'uint', $hSocket, 'ptr', $lpsockaddr, 'int', $iLen)
If @error Then Return SetError(@error, 1, -1)
Return SetError(WSAGetLastError(), 0, $aResult[0])
EndFunc ;==>connect
Func bind($hSocket, $lpsockaddr, $iLen)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'bind', 'uint', $hSocket, 'ptr', $lpsockaddr, 'int', $iLen)
If @error Then Return SetError(@error, 1, False)
Return SetError(WSAGetLastError(), 0, $aResult[0])
EndFunc ;==>bind
Func listen($hSocket, $ibacklog = 1)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'listen', 'uint', $hSocket, 'int', $ibacklog)
If @error Then Return SetError(@error, 1, False)
Return SetError(WSAGetLastError(), 0, $aResult[0])
EndFunc ;==>listen
Func accept($hSocket)
Local $aResult, $tBuffer = DllStructCreate($tagSockaddr_in)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
$aResult = DllCall($Ws2_32_dll, 'int', 'accept', 'uint', $hSocket, 'ptr', DllStructGetPtr($tBuffer), 'int*', DllStructGetSize($tBuffer))
If @error Then Return SetError(-1, 0, -1)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>accept
Func _send($hSocket, $lpBuf, $iLen, $iFlags = 0)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'send', 'uint', $hSocket, 'ptr', $lpBuf, 'int', $iLen, 'int', $iFlags)
If @error Then Return SetError(-1, 0, @error)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>_send
Func recv($hSocket, $lpBuf, $iLen, $iFlags = 0)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'recv', 'uint', $hSocket, 'ptr', $lpBuf, 'int', $iLen, 'int', $iFlags)
If @error Then Return SetError(-1, 0, @error)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>recv
Func WSACleanup()
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'WSACleanup')
If @error Then Return SetError(-1, 0, @error)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>WSACleanup
Func closesocket($hSocket)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'int', 'closesocket', 'uint', $hSocket)
If @error Then Return SetError(-1, 0, @error)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>closesocket
Func _TCPShutdown($hSocket, $iHow = $SD_BOTH)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, "int", "shutdown", "uint", $hSocket, "int", $iHow)
If @error Then Return SetError(1, @error, False)
If $Ws2_32_dll[0] <> 0 Then Return SetError(2, WSAGetLastError(), False)
Return True
EndFunc ;==>_TCPShutdown
Func htons($iPort)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'ushort', 'htons', 'ushort', $iPort)
If @error Then Return SetError(-1, 0, @error)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>htons
Func htonl($hostlong)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'ulong', 'htonl', 'ulong', $hostlong)
If @error Then Return SetError(-1, 0, @error)
Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc ;==>htonl
Func inet_addr($sIp)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'uint', 'inet_addr', 'str', $sIp)
If @error Then Return SetError(-1, 0, @error)
Return SetError(@error, @extended, $aResult[0])
EndFunc ;==>inet_addr
Func inet_ntoa($iLong)
If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
Local $aResult = DllCall($Ws2_32_dll, 'str', 'inet_ntoa', 'int', $iLong)
If @error Then Return SetError(-1, 0, @error)
Return SetError(@error, @extended, $aResult[0])
EndFunc ;==>inet_ntoa
|