Autox Posted February 1, 2007 Posted February 1, 2007 (edited) Hey gang, I have been banging my head on this for awhile now and have gotten no where. I have been reading the help file and looking over the examples included there and an example Valery sent the last time I inquired about this stuff but I just don't get it obviously because it doesn't work like I would expect it. I am trying to write a client/server application that works basically like this... I want a central server that just sits waiting for incoming connections. When it receives one it receives the data. In this case it will be a one word request such as 'report' or 'update'. Once it gets this message I want it to send back a response (basically "I heard you"). Then I want it to go back to listening to that port or process any further pending incoming connections. I have managed to get it to start up and listen on the port. It detects the connections and receives the data packet. It sees the packet, reads it properly and sends the response. But then it just loops. It doesn't respond to anymore incoming connections. This is the code I am using for the server side: expandcollapse popup$StartCheck = TCPStartup() If $StartCheck < 1 Then _FileWriteLog($LogFile, "-- Error starting TCP Services..." & @error & " *****") Exit EndIf _FileWriteLog($LogFile, "-- TCP Services Online...") _FileWriteLog($LogFile, "--- Creating Listening port") $nMainSocket = TCPListen($MasterIP, $MasterPort) While 1 Detect() Rcv() WEnd Func Detect() _FileWriteLog($LogFile, "--- Monitoring for incoming connections-" & $nSocketRecv) While $nSocketRecv = -1 ToolTip("waiting for incoming file...", 0, 0) $nSocketRecv = TCPAccept($nMainSocket) WEnd TCPCloseSocket($nSocketRecv) EndFunc ;==>Detect ;MsgBox(0,"test",$nSocketRecv) Func Rcv() While 1 _FileWriteLog($LogFile, "--- Receiving data...-" & $nSocketRecv & " - " & $szRecvBuffer & "-=> " & @error) $szRecvBuffer &= TCPRecv($nSocketRecv, 2048) ; MsgBox(0,"test",$szRecvBuffer) If @error Then ExitLoop _FileWriteLog($LogFile, "---- Buffer Length=" & StringLen($szRecvBuffer)) If StringLen($szRecvBuffer) Then If $szRecvBuffer = "report" Then _FileWriteLog($LogFile, "---- Detected Communication Request...") Ohura() ExitLoop ElseIf $szRecvBuffer = "done" Then _FileWriteLog($LogFile, "----- End Communication requested...") TCPCloseSocket($nSocketRecv) _FileWriteLog($LogFile, "------ Socket closed -" & @error & " -=> " & $nSocketRecv) $szRecvBuffer = "" Return EndIf EndIf WEnd EndFunc ;==>Rcv Exit Func Ohura() ; _ArrayDisplay($AvailPorts,"test") ; MsgBox(0,"test","Spawn Listener") _FileWriteLog($LogFile, "---- Responding to communication request") TCPSend($nSocketRecv, "ohura-" & _ArrayPop($AvailPorts)) ; TCPCloseSocket($nSocketRecv) $szRecvBuffer = "" ; $nSocketRecv = -1 EndFunc ;==>Ohura TCPSend($nSocketRecv, "go away") Edited February 1, 2007 by Larry
Snarg Posted February 4, 2007 Posted February 4, 2007 Your Rcv() function never exits so your server will never go back to the Detect() function. A little reading goes a long way. Post count means nothing.
Shevilie Posted February 4, 2007 Posted February 4, 2007 The Exit between the two bottom functions have no effect... except that TCPSend($nSocketRecv, "go away") will never be send Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit
Autox Posted February 6, 2007 Author Posted February 6, 2007 Thanks for pointing that out. I think I put that in while banging my head on it. Perhaps someone can help outline the flow of how TCP commands should be used? Basically something like this is how I understand it: Start TCP Services =>Listen on an IP/port -=>Attempt to accept an incoming connection --=>If no incoming continue listening ^^^ If yes incoming continue on vvv ---=>Receive data Here is where my understanding runs aground. I thought I understood how this worked but apparently not. How do I detect that the incoming connection is disconnected? In my example I have the sending app send a 'done' command and I close the socket. That doesn't seem right to me but I haven't had much luck elsewise. I get pretty much the same reaction if I don't close the socket and go back to listening. My logs show this: CODE2007-02-06 07:19:36 : - Initializing Logs 2007-02-06 07:19:36 : 2007-02-06 07:19:36 : - Initializing Variables.... 2007-02-06 07:19:36 : -- Logfile is set to H:\Code\IMP - Information Management Platform\DR - Data Router\tcplog.txt 2007-02-06 07:19:36 : -- Listener processes are limited to 100 2007-02-06 07:19:36 : -- Router IP Address is set to 172.19.10.31 2007-02-06 07:19:36 : -- Router Port is set to 33891 2007-02-06 07:19:36 : - Initializing TCP Services... 2007-02-06 07:19:36 : -- TCP Services Online... 2007-02-06 07:19:36 : --- Creating Listening port 2007-02-06 07:19:36 : --- Monitoring for incoming connections--1 The app will stop here and wait for that incoming connection. Then it begins processing that: CODE2007-02-06 07:19:41 : --- Receiving data...-500 - -=> 0 2007-02-06 07:19:41 : ---- Buffer Length=6 2007-02-06 07:19:41 : ---- Detected Communication Request... 2007-02-06 07:19:41 : ---- Responding to communication request The data sent to from the client app said 'report' which the if-then checks for and when it finds it then it sends the app into the 'ohura' function which transmits back some information which is recorded in the log as the 'Responding to communication...' entry. At this point it returns to the rvc function. The next line in that function is exitloop which sends it out of the while-wend code which should then have it process the WEnd stop which would send it back to the main loop. Here the loop would send it back to the detect function. The detect would find the client is still connected and goes back into the receive. Since the client app pauses with a message box for me to click the receive loops spins with no data. CODE2007-02-06 07:29:44 : --- Receiving data...-500 - report-=> 0 2007-02-06 07:29:44 : ---- Buffer Length=0 2007-02-06 07:29:44 : --- Receiving data...-500 - -=> 0 2007-02-06 07:29:44 : ---- Buffer Length=0 As soon as I click the button on the client it sends the 'done' message which the receive detects and processes. CODE... 2007-02-06 07:29:45 : --- Receiving data...-500 - -=> 0 2007-02-06 07:29:45 : ---- Buffer Length=4 2007-02-06 07:29:45 : ----- End Communication requested... 2007-02-06 07:29:45 : ------ Socket closed -0 -=> 500 2007-02-06 07:29:45 : --- Monitoring for incoming connections-500 2007-02-06 07:29:45 : --- Receiving data...-500 - done-=> 0 The socket closed in the above log is just text. I disabled the actual line of text that closes the socket. But here is my problem... The client app closes its socket and exitx but the server apps goes into a loop. You can see it goes to 'monitoring for new...' which is the detect routine. The detect routine looks like this: Func Detect() _FileWriteLog($LogFile,"--- Monitoring for incoming connections-" & $nSocketRecv) While $nSocketRecv = -1 ToolTip( "waiting for incoming file...", 0, 0 ) $nSocketRecv = TCPAccept( $nMainSocket ) WEnd The log entry above that says 'Monitoring for incoming connections-500' is the filewritelog from the detect function. As you can see the '500' in that line is the value of the last TCPAccept. This causes the while loop to be skipped apparently because $nSocketRecv is not equal to -1 it equals 500. This has led me to small break through on my app but I don't understand the logic of it. If I add in a line just above the 'While $nSocketRecv = -1' that says '$nSocketRecv = -1' then the receive loop processes properly and stops and waits for the next connection. My question at this point is... Is this the best/proper method of handling the communications? Resetting the value of the tcpaccept command to -1? Here is what I need my app to do: Daemon (on central server): Listen on designated port for incoming connections Agent (on client system): Connect to daemon on designated port Daemon: accept connection Agent: Tell daemon it wants to report in Daemon: assign a secondary port for the agent to report on. Daemon: Spawn an independent process to listen on that port Daemon: Tell agent which port to report on. Daemon: monitor for and process any other incoming connections Agent: Connect to listener process on the specified port Listener: Accept connection Agent: Send data Listener: Accept data Agent: Wait for verification Listener: Send verify Agent: Send done Agent: Exit Listener: Insert received date into queue Listener: Exit Thanks to all
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