kcd-clan Posted May 6, 2006 Posted May 6, 2006 I need a script that will recive inputbox commands that are sent to server. nothing to complicated just basic socket setting and a inpoutbox Just use the help reciver aka expandcollapse popupExample ;SERVER!! Start Me First !!!!!!!!!!!!!!! #include <GUIConstants.au3> ; Initialize a variable to represent a connection ;============================================== Global $MainSocket, $ConnectedSocket = -1 Global $g_IP = @IPADDRESS1 Global $g_port = 33891 ; Start The TCP Services ;============================================== TCPStartUp() ; Create a Listening "SOCKET" ;============================================== $MainSocket = TCPListen($g_IP, $g_port, 100 ) If $MainSocket = -1 Then Exit $RogueSocket = -1 ; Create a GUI for chatting ;============================================== $GOOEY = GUICreate("my server - I am " & @IPADDRESS1,350,200,@DesktopWidth/2+100,@DesktopHeight/2-350) $edit = GUICtrlCreateEdit("",10,40,330,150,$WS_DISABLED) $input = GUICtrlCreateInput("",10,10,250,20) $butt = GUICtrlCreateButton("Send",260,10,80,20,$BS_DEFPUSHBUTTON) GUISetState() ; GUI Message Loop ;============================================== While 1 $msg = GUIGetMsg() ; GUI Closed ;-------------------- If $msg = $GUI_EVENT_CLOSE Then ExitLoop ; User Pressed SEND ;-------------------- If $msg = $butt Then If $ConnectedSocket > -1 Then $ret = TCPSend( $ConnectedSocket, GUICtrlRead($input)) If @ERROR Then ; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1 ;---------------------------------------------------------------- TCPCloseSocket( $ConnectedSocket ) WinSetTitle($GOOEY,"","my server - I am " & @IPADDRESS1) $ConnectedSocket = -1 ElseIf $ret > 0 Then ; UPDATE EDIT CONTROL WITH DATA WE SENT ;---------------------------------------------------------------- GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF ) EndIf EndIf GUICtrlSetData($input,"") EndIf If $RogueSocket > 0 Then $recv = TCPRecv( $RogueSocket, 512 ) If NOT @error Then TCPCloseSocket( $RogueSocket ) $RogueSocket = -1 EndIf EndIf ; If no connection look for one ;-------------------- If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAccept( $MainSocket) If $ConnectedSocket >= 0 Then WinSetTitle($GOOEY,"","my server - Hello " & SOCKET2IP($ConnectedSocket)) EndIf ; If connected try to read some data ;-------------------- Else ; EXECUTE AN UNCONDITIONAL ACCEPT IN CASE ANOTHER CLIENT TRIES TO CONNECT ;---------------------------------------------------------------- $RogueSocket = TCPAccept( $MainSocket) If $RogueSocket > 0 Then TCPSend( $RogueSocket, "~~rejected" ) EndIf $recv = TCPRecv( $ConnectedSocket, 512 ) If $recv <> "" And $recv <> "~~bye" Then ; UPDATE EDIT CONTROL WITH DATA WE RECEIVED ;---------------------------------------------------------------- GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF) ElseIf @error Or $recv = "~~bye" Then ; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1 ;---------------------------------------------------------------- WinSetTitle($GOOEY,"","my server - I am " & @IPADDRESS1) TCPCloseSocket( $ConnectedSocket ) $ConnectedSocket = -1 EndIf EndIf WEnd GUIDelete($GOOEY) Func OnAutoItExit() ;ON SCRIPT EXIT close opened sockets and shutdown TCP service ;---------------------------------------------------------------------- If $ConnectedSocket > -1 Then TCPSend( $ConnectedSocket, "~~bye" ) Sleep(2000) TCPRecv( $ConnectedSocket, 512 ) TCPCloseSocket( $ConnectedSocket ) EndIf TCPCloseSocket( $MainSocket ) TCPShutDown() EndFunc Func SOCKET2IP($SHOCKET) Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]") $a = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET,"ptr",DLLStructGetPtr($sockaddr), _ "int_ptr",DLLStructGetSize($sockaddr)) If Not @error And $a[0] = 0 Then $a = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3)) If Not @error Then $a = $a[0] Else $a = 0 EndIf ; release Struct not really needed as it is a local $sockaddr = 0 Return $a EndFunc Visit mEMy programs made.Iul - IulG-V Console - G-V Console_RandomLetter - _RandomLetter()Saftey Kill - Saftey Killcolorzone() = colorzone()
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