FaridAgl Posted January 19, 2014 Share Posted January 19, 2014 (edited) I know it is NOT the first attempt on this, but here is what I came up with: expandcollapse popupFunc TCPConnectEx($sIPAddress, $iPort, $iTimeOut = 1000) Local $iError = 0, $avResuslt Local $hWindowsSocket = DllOpen("ws2_32.dll") Local $iSocket = DllCall($hWindowsSocket, "UINT_PTR", "socket", _ "int", 2, _ ;AF_INET "int", 1, _ ;SOCK_STREAM "int", 6) ;IPPROTO_TCP If ((@error) Or ($iSocket[0] = 0xFFFFFFFF)) Then $iError = 1 Else $iSocket = $iSocket[0] Local $iIpAddress = DllCall($hWindowsSocket, "ULONG", "inet_addr", _ "str", $sIPAddress) If ((@error) Or ($iIpAddress[0] = 0) Or ($iIpAddress[0] = 0xFFFFFFFF)) Then $iError = 2 Else $iIpAddress = $iIpAddress[0] $iPort = DllCall($hWindowsSocket, "USHORT", "htons", _ "USHORT", $iPort) If (@error) Then $iError = 3 Else $iPort = $iPort[0] EndIf EndIf If ($iError = 0) Then Local $tSocketAddress = DllStructCreate("short sin_family;USHORT sin_port; ULONG sin_addr;CHAR sin_zero[8]") DllStructSetData($tSocketAddress, "sin_family", 2) ;AF_INET DllStructSetData($tSocketAddress, "sin_port", $iPort) DllStructSetData($tSocketAddress, "sin_addr", $iIpAddress) DllCall($hWindowsSocket, "int", "ioctlsocket", _ "UINT_PTR", $iSocket, _ "long", 0x8004667E, _ ;FIONBIO "ULONG*", 1) ;Nonblocking $avResuslt = DllCall($hWindowsSocket, "int", "connect", _ "UINT_PTR", $iSocket, _ "ptr", DllStructGetPtr($tSocketAddress), _ "int", DllStructGetSize($tSocketAddress)) If ((@error) Or ($avResuslt[0] <> -1)) Then $iError = 4 Else Local $t = DllStructCreate("uint;int") DllStructSetData($t, 1, 1) DllStructSetData($t, 2, $iSocket) Local $tTimeval = DllStructCreate("long tv_sec;long tv_usec") DllStructSetData($tTimeval, "tv_sec", Floor($iTimeOut / 1000)) DllStructSetData($tTimeval, "tv_usec", Mod($iTimeOut, 1000)) $avResuslt = DllCall($hWindowsSocket, "int", "select", _ "int", $iSocket, _ "ptr", DllStructGetPtr($t), _ "ptr", DllStructGetPtr($t), _ "ptr", 0, _ "ptr", DllStructGetPtr($tTimeval)) If ((@error) Or ($avResuslt[0] <> 0)) Then DllCall($hWindowsSocket, "int", "ioctlsocket", _ "UINT_PTR", $iSocket, _ "long", 0x8004667E, _ ;FIONBIO "ULONG*", 0) ;Blocking Else $avResuslt = DllCall($hWindowsSocket, "int", "WSAGetLastError") If ((@error) Or ($avResuslt[0] <> 0)) Then $iError = 5 Else $iError = 10060 ;WSAETIMEDOUT EndIf EndIf EndIf EndIf EndIf If ($iError) Then DllCall($hWindowsSocket, "int", "closesocket", _ "UINT_PTR", $iSocket) $iSocket = -1 EndIf DllClose($hWindowsSocket) Return SetError($iError, 0, $iSocket) EndFunc The usage is exactly like the native TCPConnect, everything is just the same except that this one accepts an extra parameter for timeout. The reason of why I posted it here is to get help of experts on this. I mean I will be very thankful if anyone check this out and let me know if there is any problem, maybe I did it wrong in some spots. Thanks in advance. Edited January 19, 2014 by D4RKON3 http://faridaghili.ir Link to comment Share on other sites More sharing options...
Celtic88 Posted January 19, 2014 Share Posted January 19, 2014 Excellent! Works Perfectly, Thankyou!! Link to comment Share on other sites More sharing options...
funkey Posted January 19, 2014 Share Posted January 19, 2014 Is this the same like this: ? Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
FaridAgl Posted January 19, 2014 Author Share Posted January 19, 2014 @funkey NO, it is mostly like this: I have modified that to suite my need, made it somehow cleaner and simpler (I think). http://faridaghili.ir Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now