Hello all, I'm having some trouble with the following. Being new I went through the examples and fired up some code. Now I have the following server ( just the basic one as written in helpfiles).
My client and server look like this.
So when I press a button the string will be seen in the server. Whenever I press either button again the string is not seen anymore. It looks like it's losing the connection but after some debugging I cannot find out what is going wrong.
My client and server look like this.
[ autoit ]
#include #include #include #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 275, 75, 192, 124) $Button1 = GUICtrlCreateButton("Test", 32, 16, 75, 25) $Button2 = GUICtrlCreateButton("Test2", 152, 16, 75, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 RunServer("Test") Case $Button2 RunServer("Test2") EndSwitch WEnd Func RunServer($action) TCPStartup() Local $ConnectedSocket, $szData Local $szIPADDRESS = @IPAddress1 Local $nPORT = 33891 ; Initialize a variable to represent a connection ;============================================== $ConnectedSocket = -1 $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT) ; If there is an error... show it If @error Then MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error) ; If there is no error loop an inputbox for data ; to send to the SERVER. Else $szData = $action TCPSend($ConnectedSocket, StringToBinary($action, 4)) EndIf EndFunc ;==>Example ; and the server #include <GUIConstantsEx.au3> RunServer() Func RunServer() ; Set Some reusable info ; Set your Public IP address (@IPAddress1) here. ; Local $szServerPC = @ComputerName ; Local $szIPADDRESS = TCPNameToIP($szServerPC) Local $szIPADDRESS = @IPAddress1 Local $nPORT = 33891 Local $MainSocket, $edit, $ConnectedSocket, $szIP_Accepted Local $msg, $recv ; Start The TCP Services ;============================================== TCPStartup() ; Create a Listening "SOCKET". ; Using your IP Address and Port 33891. ;============================================== $MainSocket = TCPListen($szIPADDRESS, $nPORT) ; If the Socket creation fails, exit. If $MainSocket = -1 Then Exit ; Create a GUI for messages ;============================================== GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200, 100, 100) $edit = GUICtrlCreateEdit("", 10, 10, 280, 180) GUISetState() ; Initialize a variable to represent a connection ;============================================== $ConnectedSocket = -1 ;Wait for and Accept a connection ;============================================== Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 ; Get IP of client connecting $szIP_Accepted = SocketToIP($ConnectedSocket) ; 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 ; convert from UTF-8 to AutoIt native UTF-16 $recv = BinaryToString($recv, 4) ; 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() EndFunc ;==>Example ; Function to return IP Address from a connected socket. ;---------------------------------------------------------------------- Func SocketToIP($SHOCKET) Local $sockaddr, $aRet $sockaddr = DllStructCreate("short;ushort;uint;char[8]") $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _ "ptr", DllStructGetPtr($sockaddr), "int*", 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 ;==>SocketToIP
So when I press a button the string will be seen in the server. Whenever I press either button again the string is not seen anymore. It looks like it's losing the connection but after some debugging I cannot find out what is going wrong.