ServerSocket-Desktop example question

Hello,
I am trying to write a windows app to have an indicator I have be able to pull items from a database. To test the ServerSocket example Xojo has, I am using the old Hyperterminal program on another computer to act like a client. I setup the Xojo example to listen to the port I want and when I run it and connect to it with Hyperterminal, I see the “connected” status on the window.

Problem I am having is when I type something in Hyperterminal and press enter, I don’t see it in the Xojo example. However, if I type something in the Xojo example and I press SEND, I see it in Hyperterminal.

I thought the ReceiveMessage event was going to read what came in and display it in the window but I only get that when I connect and it gives me the client’s IP

Any help will be appreciated
Allen

See the DataAvailable event in the docs for TCPSocket. Once a connection has been established, commands sent to your application will arrive here. You will want to use .LookAhead and .ReadAll methods to pull and parse the data.

Example Code (uses a defined separator for the data but will be similar in implementation in most cases)

//Define our DataSeparator
Const DataSeparator = chr(0) + endofline

//See if Data is available, and enough data has sent to obtain a full desired "packet"; also checking to see if more than one "packet" is available for processing.
Var data as String = me.Lookahead
Var tCount as integer = data.CountFields(DataSeparator)

Var termIDX as integer = 0

//Loop thru our available "packets" of "complete data"
For x as integer = 1 to tcount
  
  termIDX = data.IndexOfBytes(termIDX,DataSeparator)
  //Only read a complete "data packet"
  Var line as String = me.Read(termIDX)
  
//Separate commands from data to be processed (in the instance of this example)
  Var Command as String = line.NthField("::",1).NthField("<{[",2).Trim
//Data does not need to be compressed/decompressed, it merely was in this example to speed up transmissions of data across the network and reduce bandwidth.
  Var theData as String = DecompressGZip(line.NthField(command + "::",2))
 
//Feed the commands and data to a function that appends them to an array to be processed by our timer and popped off the array one-by-one as they're processed
    ProcessData( Command, theData )
 
  
next

//No actual processing occurs within the DataAvailable event, data is only buffered for processing in this case above.

Thank You Matthew,
The example you provide me guided me to accomplished what I was looking for.

1 Like

Hello everyone. Going back to this question, I got the “client” test program to send to the “server”. The server then grabs what came in and I call a method to process the data. In that method, I copy the result in a global variable called sMessage. How can I send sMessage back to the client? I tried to send it in the SendComplete event but I get a continuous loop of the sMessage.

See updated demo at

**Demo contains both a working server and a client and demonstrates passing information between the server and client, as well as handling the incoming data and responding. I tried to keep it as simple as possible, also using the snippet above to show how it integrates. Let me know if you have any questions please!

Thank You