CyberGlitch Posted April 24, 2006 Posted April 24, 2006 Ok, read the help files and can't get my head wrapped around this so thought I'd ask the all knowing Forums. I'm looking for a simple client/server communication between 2 machines. First the server should sit patiently listening for any data and when it receives something pop-up what data was received. Second one being the client and should connect to the server and send data. I'm talking simple text messages here. I need my server to do something based on what is happening from my other machine. Tried many examples found on the forums and they only say it's connected to the server but can't send any data or have the server listening.
Uten Posted April 24, 2006 Posted April 24, 2006 And you did try the TCPRecv, TCPSend sample from the help file? expandcollapse popup;SERVER!! Start Me First !!!!!!!!!!!!!!! #include <GUIConstants.au3> ; Set Some reusable info ; Set your Public IP address (@IPAddress1) here. Dim $szIPADDRESS = @IPAddress1 Dim $nPORT = 33891 ; 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 ;============================================== Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200) Dim $edit = GUICtrlCreateEdit("",10,10,280,180) GUISetState() ; 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 expandcollapse popup;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!! ; see TCPRecv example #include <GUIConstants.au3> ; Start The TCP Services ;============================================== TCPStartUp() ; Set Some reusable info ;-------------------------- Dim $szServerPC = @ComputerName ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address Dim $szIPADDRESS = TCPNameToIP($szServerPC) Dim $nPORT = 33891 ; Initialize a variable to represent a connection ;============================================== Dim $ConnectedSocket = -1 ;Attempt to connect to SERVER at its IP and PORT 33891 ;======================================================= $ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT) Dim $szData ; 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 ;Loop forever asking for data to send to the SERVER While 1 ; InputBox for data to transmit $szData = InputBox("Data for Server",@LF & @LF & "Enter data to transmit to the SERVER:") ; If they cancel the InputBox or leave it blank we exit our forever loop If @error Or $szData = "" Then ExitLoop ; We should have data in $szData... lets attempt to send it through our connected socket. TCPSend($ConnectedSocket,$szData) ; If the send failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ExitLoop WEnd EndIf Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling
=sinister= Posted April 24, 2006 Posted April 24, 2006 Also, if the machines are not running on the same port, you will have to forward it at http://www.portforward.comOther than that, you can use your local IP (127.0.0.1)
CyberGlitch Posted April 24, 2006 Author Posted April 24, 2006 The 2 examples in the help file wouldn't work using the latest beta.
Uten Posted April 24, 2006 Posted April 24, 2006 (edited) Do you get any error messages? Does it compile? Worked with 118 when I tried it a few weeks ago. EDIT: Just tried it, still works (beta 118) Edited April 24, 2006 by Uten Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling
CyberGlitch Posted April 25, 2006 Author Posted April 25, 2006 I must have an old help file then. That example works great, the one on the help file is different and doesn't work.
Uten Posted April 25, 2006 Posted April 25, 2006 I must have an old help file then. That example works great, the one on the help file is different and doesn't work.There is version information in the help file at the Autoit->introduction page.Happy to know you have got it started Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling
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