nitekram Posted February 12, 2015 Share Posted February 12, 2015 I am trying to use the examples, but having issues with passing data between the two windows. I am not going to use it for chat perse, but I do want to be able to send messages to other computers, like NET SEND, or even instant messages. Though, the example says it made contact, it closes completly afterwards. I do not want the window staying open, unless their is data being sent between the server and the client or visaversa. Here is my attemp: expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> ; Start First clicking on "1. Server" ; Then start a second instance of the script selecting "2. Client" Example() Func Example() TCPStartup() ; Start the TCP service. ; Register OnAutoItExit to be called when the script is closed. OnAutoItExitRegister("OnAutoItExit") ; Assign Local variables the loopback IP Address and the Port. Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer. Local $iPort = 65432 ; Port used for the connection. #Region GUI Local $sTitle = "TCP Start" Local $hGUI = GUICreate($sTitle, 250, 70) Local $idBtnServer = GUICtrlCreateButton("1. Server", 65, 10, 130, 22) Local $idBtnClient = GUICtrlCreateButton("2. Client", 65, 40, 130, 22) GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idBtnServer WinSetTitle($sTitle, "", "TCP Server started") GUICtrlSetState($idBtnClient, $GUI_HIDE) GUICtrlSetState($idBtnServer, $GUI_DISABLE) If Not MyTCP_Server($sIPAddress, $iPort) Then MsgBox('','server','exiting loop') ;ExitLoop EndIf Case $idBtnClient WinSetTitle($sTitle, "", "TCP Client started") GUICtrlSetState($idBtnServer, $GUI_HIDE) GUICtrlSetState($idBtnClient, $GUI_DISABLE) If Not MyTCP_Client($sIPAddress, $iPort) Then MsgBox('','client','exiting loop') ExitLoop EndIf EndSwitch Sleep(10) WEnd #EndRegion GUI EndFunc ;==>Example Func MyTCP_Client($sIPAddress, $iPort) Local $iError = 0 ; for error checking ; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified. Local $iSocket = TCPConnect($sIPAddress, $iPort) ; If an error occurred display the error code and return False. If @error Then ; The server is probably offline/port is not opened on the server. $iError = @error MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError) Return False EndIf Local $hGUI_Client = GUICreate('Client', 200, 200) Local $hGUI_ClientCheck = GUICtrlCreateCheckbox('', 50, 100) Local $hGUI_ClientEdit = GUICtrlCreateEdit('', 10, 10, 100, 100) Local $hGUI_ClientButton = GUICtrlCreateButton('Send', 10, 120, 30, 30) GUISetState(@SW_SHOW, $hGUI_Client) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUICtrlDelete($hGUI_Client) ExitLoop Case $hGUI_ClientEdit MsgBox('', 'Sending Data', GUICtrlRead($hGUI_ClientEdit)) ; Send the string "tata" to the server. TCPSend($iSocket, GUICtrlRead($hGUI_ClientEdit)) ; If an error occurred display the error code and return False. If @error Then $iError = @error MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError) Return False EndIf EndSwitch WEnd ; Close the socket. TCPCloseSocket($iSocket) EndFunc ;==>MyTCP_Client Func MyTCP_Server($sIPAddress, $iPort) ; Assign a Local variable the socket and bind to the IP Address and Port specified with a maximum of 100 pending connexions. Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100) Local $iError = 0 If @error Then ; Someone is probably already listening on this IP Address and Port (script already running?). $iError = @error MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not listen, Error code: " & $iError) Return False EndIf Local $hGUI_Server = GUICreate('Server TEST', 200, 200) ;Local $hGUI_ServerCheck = GUICtrlCreateCheckbox(50,100) Local $hGUI_ServerLable = GUICtrlCreateLabel('junk', 10, 10, 100, 100) GUISetState(@SW_SHOW, $hGUI_Server) ; Assign a Local variable to be used by the Client socket. Local $iSocket = 0 Do ; Wait for someone to connect (Unlimited). ; Accept incomming connexions if present (Socket to close when finished; one socket per client). Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUICtrlDelete($hGUI_Server) Return False Case Else $iSocket = TCPAccept($iListenSocket) ; If an error occurred display the error code and return False. If @error Then $iError = @error MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError) Return False EndIf ; Assign a Local variable the data received. Local $sReceived = TCPRecv($iSocket, 4) ;we're waiting for the string "tata" OR "toto" (example script TCPRecv): 4 bytes length. If $sReceived <> '' Then ; Notes: If you don't know how much length will be the data, ; use e.g: 2048 for maxlen parameter and call the function until the it returns nothing/error. ; Display the string received. ;MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "Received: " & $sReceived) GUICtrlSetData($hGUI_ServerLable, $sReceived) Sleep(4000) EndIf EndSwitch Until $iSocket <> -1 ;if different from -1 a client is connected. ; Close the Listening socket to allow afterward binds. TCPCloseSocket($iListenSocket) ; Close the socket. TCPCloseSocket($iSocket) EndFunc ;==>MyTCP_Server Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit It closes after attmeping to send info only two times. . 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow." WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator Link to comment Share on other sites More sharing options...
nitekram Posted February 12, 2015 Author Share Posted February 12, 2015 I have found this >LINK - which does it on different computers, which is what I want. I think maybe the help file needs to maybe explain or at least keep the connection open, or is only showing you how to create the connection then close. 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow." WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator 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