d0n Posted April 25, 2007 Posted April 25, 2007 So i am new to this packet stuff and wanted to see what happens with the autoit help example expandcollapse popup#include <GUIConstants.au3> ; Set Some reusable info ; Set your Public IP address (@IPAddress1) here. ;============================================== $IP=@IPAddress1 $port=4099 ; Create a GUI for messages ;============================================== Dim $GOOEY = GUICreate("My Server (IP: " & $IP & ")",300,200) Dim $edit = GUICtrlCreateEdit("",10,10,280,180) GUISetState() ; Start The TCP Services ;============================================== TCPStartup ( ) ; Create a Listening "SOCKET". ; Using your IP Address and Port 33891. ;============================================== $MainSocket=TCPListen($IP, $port) ; If the Socket creation fails, exit. If $MainSocket = -1 Then Exit ; Initialize a variable to represent a connection ;============================================== Dim $ConnectedSocket = -1 ;Wait for and Accept a connection ;============================================== Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 ; Get IP of client connecting Dim $szIP_Accepted = SocketToIP($ConnectedSocket) Dim $msg, $recv ; GUI Message Loop ;============================================== While 1 $msg = GUIGetMsg() ; GUI Closed ;-------------------- If $msg = $GUI_EVENT_CLOSE Then ExitLoop ; Try to receive (up to) 2048 bytes ;---------------------------------------------------------------- $recv = TCPRecv( $ConnectedSocket, 2048 ) ; If the receive failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ExitLoop ; Update the edit control with what we have received ;---------------------------------------------------------------- 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 so this is pretty much what is there on the example i just changed the port but nothing really happens when i do stuff :S whats am i doing wrong here, sorry this is only my 1st time trying out packet with autoit :S
Mellowz Posted April 25, 2007 Posted April 25, 2007 (edited) Do you have a client to connect to it? Try this. Run the server then run this. expandcollapse popup#include <GUIConstants.au3> Dim $IsConnected = 0 Dim $ConnectedSocket = -1 Opt("GUIOnEventMode", 1) GUICreate("Client", 314, 202, 193, 115) GUICtrlCreateGroup(" Connection Information ", 8, 8, 297, 113) GUICtrlCreateLabel("Address:", 16, 32, 53, 17) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") $Address = GUICtrlCreateInput(@IPAddress1, 72, 32, 97, 21) GUICtrlCreateLabel(" :", 172, 32, 12, 17) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") $Port = GUICtrlCreateInput("6900", 184, 32, 33, 21) $Connect = GUICtrlCreateButton("Connect", 224, 32, 73, 25, 0) GUICtrlSetOnEvent(-1, "ConnectToServer") GUICtrlCreateGroup("", 16, 64, 281, 41) $Status = GUICtrlCreateLabel("Not Connected", 24, 80, 268, 17, $SS_CENTER) GUICtrlCreateGroup(" Send Data ", 8, 128, 297, 65) $Cmd = GUICtrlCreateInput("", 16, 152, 209, 21) GUICtrlSetState(-1, $GUI_DISABLE) $Send = GUICtrlCreateButton("Send", 232, 152, 65, 25, 0) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlSetOnEvent(-1, "SendToServer") GUISetState(@SW_SHOW) While 1 Sleep(100) WEnd Func ConnectToServer() If $IsConnected = 0 Then TCPStartUp() GUICtrlSetData($Status, "Connecting to " & GUICtrlRead($Address) & ":" & GUICtrlRead($Port) & "...") $ConnectedSocket = TCPConnect(GUICtrlRead($Address), GUICtrlRead($Port)) If @error Then GUICtrlSetData($Status, "Failed to connect to " & GUICtrlRead($Address) & ":" & GUICtrlRead($Port) & ".") Return 1 EndIf GUICtrlSetState($Address, $GUI_DISABLE) GUICtrlSetState($Port, $GUI_DISABLE) GUICtrlSetState($Cmd, $GUI_ENABLE) GUICtrlSetState($Send, $GUI_ENABLE) GUICtrlSetData($Connect, "Disconnect") GUICtrlSetData($Status, "Connected") $IsConnected = 1 Else GUICtrlSetState($Address, $GUI_ENABLE) GUICtrlSetState($Port, $GUI_ENABLE) GUICtrlSetState($Cmd, $GUI_DISABLE) GUICtrlSetState($Send, $GUI_DISABLE) GUICtrlSetData($Status, "Disconnecting...") TCPCloseSocket($ConnectedSocket) TCPShutdown() GUICtrlSetData($Status, "Not Connected") GUICtrlSetData($Connect, "Connect") $ConnectedSocket = -1 $IsConnected = 0 EndIf Return 0 EndFunc Func SendToServer() If $Cmd = "" Then MsgBox(0, "Error", "You didn't enter the command to send!") Return 1 EndIf TCPSend($ConnectedSocket, GUICtrlRead($Cmd)) If @error Then MsgBox(0, "Error", "Failed to send command to server.") Return 2 EndIf Return 0 EndFunc Func ExitProgram() If $IsConnected = 1 Then MsgBox(0, "Error", "Please disconnect before exitting this program.") Return EndIf Exit EndFunc If I send 'aaa' through the client, my result is '192.168.1.101 > aaa'. Edited April 25, 2007 by Mellowz
d0n Posted April 25, 2007 Author Posted April 25, 2007 i see, so on the example thing where do i put my client info?? Thanks for your help btw!
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