ServerSocket Best way to handle persistent connections

Hello guys,

i`m trying to have an app that will listen to a TCP port and save the fetched data to the database and so far i managed to have something but it seems that after a while the server dies , so i have no idea what is wrong but it seems that the webapp just closes the connections and it generate crashes.

Now what is the best way to handle this ? either to have a console app that will just listen to the port and process the data and save it in the database , and an webapp that will do the processing and displaying of the data or one mixed as now but i will have to fix the issue with the timeout or whatever it is the problem

If in case i have to go with 2 separate apps how should i handle the console app and how do i make sure that will never hang or crash ?

I have some devices that push data from time to time, and the whole app should fetch the data and process it and display it to the customer.

Thanks again.


If your apps push data “from time to time,” why do you need persistent connections?

The reason I ask is that if you’re going to use an always on connection, your program still needs to deal with dropped connections. There’s no guarantee that everything will continue working from point A to point B for all eternity. Besides that, a persistent connection requires persistent system resources.

Thanks Greg, well the issue is that i`m having a device, like a gps tracker that has a very bad software , so i asked them to give me the device and i will try to figure it out the data processing and do the rest .

So i ended up using that ServerSocket from the examples, adapting it for the webapp, and it seems that i did something but i don`t get the proper data.

After an hour of chatting with those guys they told me that i need the protocol for that :smiley: things that i asked from the beginning.

So they give me this .

Now they say in the configurator that its a TCP connection and that i have to set the port and the server address and thats it. now what i`m getting as data is just the IMEI so i assume that i have to do some other magic to be able to fetch that data or not ?

Thanks again. it`s new for me but honestly i want to learn and to doit the proper way .


That document tells you exactly what it wants in section 2.2. You have to write data back to the socket telling the device whether or not your app can handle the data.

Hello Greg,

I saw that as well but the question here is how to do that ? so far i put an ServerSocket that starts to listen and in AddSocket event i put a tcp socket that checks the data, i get the IMEI on it and normally it says that to start the data transfer i have to send back to the device 01 in Binary data or 00 to stop, so i in the SendComplete Event of the TCP socket the Self.Write(Bin(01)) command but so far no luck no extra data is coming back, then there is another thing , “After receiving IMEI, server should determine if it would accept data from this module. If yes server will reply to module 01 if not 00. Note that confirmation should be sent as binary packet.
Then module starts to send first AVL data packet. After server receives packet and parses it, server must report to module number of data received as integer (four bytes).
If sent data number and reported by server doesn’t match module resends sent data.”

So where and how do i handle that ? as well on the DataAvailable event or in SendComplete Event ?

Maybe i`m doing this wrong, as far as i guessed is that once the socket is initiated it holds that connection so it means if i put that Write and send the data back it should be received on the other end for that certain socket. i will have to see if i have any timeouts that i have to handle.

Should i use the ServerSocket or not ? so far it is the only option i guess but the thing is that after a while the app crashes or it does not respond anymore so i have no idea why, i will have to put some deep debug logging to see what it closes the app or stops the app from responding anymore.

Thanks again .

Aurelian .

Bin() returns a string representation of the binary format of a number. That’s not what you want. You want the numeric value in a single character. Use Char() for that.


And move it to DataAvailable.

[quote=322268:@Tim Hare]Bin() returns a string representation of the binary format of a number. That’s not what you want. You want the numeric value in a single character. Use Char() for that.


And move it to DataAvailable.[/quote]

Thanks Tim, so i guess i should do like a parse method that will fetch all that data and fire it in the DataAvailable Event so every time there is something i just parse it and send the needed results back to the device.

I will try that and update asap.

Thanks again,


Hi Tim,

As update, i tried to put that Self.Write(char(01)) and i get an error “char - This item does not exist” so i`ll have to do some reading to see how i can get that fixed. So far it does not work .


Its Chr(), not Char().

Yep. Sorry for the typo.

Oh, and as a personal preference, I would use ME instead of SELF. Me always means the current object instance. Self changes meaning depending on context - it could mean the object instance, or it could be the window containing the object. To avoid having to think about context, I just always use ME to refer to the object, and only use Self when it means the window. Again, just personal preference that I believe has made my life easier.