erezlevi Posted January 26, 2008 Share Posted January 26, 2008 (edited) hi, can someone explain how to convert IP address + port to a AU3 "socket" and the oposite. here is the code from the help file: expandcollapse popup#include <GUIConstants.au3> Dim $szIPADDRESS = @IPAddress1 Dim $nPORT = 33891 TCPStartUp() $MainSocket = TCPListen($szIPADDRESS, $nPORT) If $MainSocket = -1 Then Exit Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200) Dim $edit = GUICtrlCreateEdit("",10,10,280,180) GUISetState() Dim $ConnectedSocket = -1 Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 Dim $szIP_Accepted = SocketToIP($ConnectedSocket) Dim $msg, $recv While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop $recv = TCPRecv( $ConnectedSocket, 2048 ) If @error Then ExitLoop b If $recv <> "" Then GUICtrlSetData($edit, _ $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit)) WEnd If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket ) TCPShutDown() ; Function to return IP Address from a connected socket. ;---------------------------------------------------------------------- Func SocketToIP($SHOCKET) Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]") Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _ "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr)) If Not @error And $aRet[0] = 0 Then $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3)) If Not @error Then $aRet = $aRet[0] Else $aRet = 0 EndIf $sockaddr = 0 Return $aRet EndFunc Edited January 26, 2008 by erezlevi Link to comment Share on other sites More sharing options...
erezlevi Posted January 27, 2008 Author Share Posted January 27, 2008 bump. no hackers logged on ha.... Link to comment Share on other sites More sharing options...
martin Posted January 27, 2008 Share Posted January 27, 2008 (edited) hi, can someone explain how to convert IP address + port to a AU3 "socket" and the oposite. here is the code from the help file: expandcollapse popup#include <GUIConstants.au3> Dim $szIPADDRESS = @IPAddress1 Dim $nPORT = 33891 TCPStartUp() $MainSocket = TCPListen($szIPADDRESS, $nPORT) If $MainSocket = -1 Then Exit Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200) Dim $edit = GUICtrlCreateEdit("",10,10,280,180) GUISetState() Dim $ConnectedSocket = -1 Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 Dim $szIP_Accepted = SocketToIP($ConnectedSocket) Dim $msg, $recv While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop $recv = TCPRecv( $ConnectedSocket, 2048 ) If @error Then ExitLoop b If $recv <> "" Then GUICtrlSetData($edit, _ $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit)) WEnd If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket ) TCPShutDown() ; Function to return IP Address from a connected socket. ;---------------------------------------------------------------------- Func SocketToIP($SHOCKET) Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]") Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _ "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr)) If Not @error And $aRet[0] = 0 Then $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3)) If Not @error Then $aRet = $aRet[0] Else $aRet = 0 EndIf $sockaddr = 0 Return $aRet EndFunc I can't tell you how to use those functions but I have had a quick look at the msdn info. It looks to me like there are some error with your function. I think the structure you have created is wrong. See here. The structure is a union but you have made it as a structure with separate elements. I think you just need $sockaddr = DLLStructCreate("char[8]") getpeername should be passed a pointer to the length of the name parameter but you are passing it the length. int getpeername( __in SOCKET s, __out struct sockaddr* name, __inout int* namelen ); Parameters s A descriptor identifying a connected socket. name The SOCKADDR structure that receives the address of the peer. namelen A pointer to the size, in bytes, of the name parameter. Return Value If no error occurs, getpeername returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. For the function inet_ntoa you are using int_ptr which should now be int* unless you are using an old versio of AutoIt. Then you are passing DLLStructGetData($sockaddr,3) instead of the structure. I am not sure what is required when you pass a structure. I would guess that you need to pass a pointer to the structure - DllstructGetptr($sockaddr). The return from inet_ntoa is a character pointer, so to get the string you need to say $sName = DllStructCreate("char[" & $slen + 1 & "]", Ret[0]) $name = DllStructGetData($sName,1) Whether or not any of this will help, or even whether I'm right, I have no idea. EDIT:spelling Edited January 27, 2008 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
erezlevi Posted January 28, 2008 Author Share Posted January 28, 2008 I can't tell you how to use those functions but I have had a quick look at the msdn info. It looks to me like there are some error with your function. I think the structure you have created is wrong. See here. The structure is a union but you have made it as a structure with separate elements. I think you just need $sockaddr = DLLStructCreate("char[8]") getpeername should be passed a pointer to the length of the name parameter but you are passing it the length. For the function inet_ntoa you are using int_ptr which should now be int* unless you are using an old versio of AutoIt. Then you are passing DLLStructGetData($sockaddr,3) instead of the structure. I am not sure what is required when you pass a structure. I would guess that you need to pass a pointer to the structure - DllstructGetptr($sockaddr). The return from inet_ntoa is a character pointer, so to get the string you need to say $sName = DllStructCreate("char[" & $slen + 1 & "]", Ret[0]) $name = DllStructGetData($sName,1) Whether or not any of this will help, or even whether I'm right, I have no idea. EDIT:spelling martin first thanks, I will check it after work today. but the script is not my own, it is from the help file... I am using it to try to understand how a socket that autoit is using can help me connect via TCPsend to an existing session allready opened by a different telnet application. (so I can put the user/pass and the rest of the commands via TCPsend and not via "send" or controlsend that you can't oporate if windows OP is locked.) 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