D.Tkalcec (RTC)
|
|
« Reply #3 on: May 25, 2017, 07:35:13 AM » |
|
If you need a LINE exactly as it was sent, then you need to PARSE the raw data you receive from Sender.Read to make sure it does contain the LINE you need and nothing else. The purpose of the example parser I've posted above is to make sure that you have one complete LINE (assuming that #13#10 or <CR><LF> is your line separator) and NOT just a part of a line or multiple lines.
TCP/IP is a streaming protocol, which means that there are no implicit LINE or MESSAGE separators built in. You get a serie of raw bytes in the order they were sent, but it is your responsibility to parse that raw data to combine the bytes which belong together and separate the rest.
The OnDataReceived event will be triggered when there is more RAW DATA ready to be read and NOT when there is a new complete "LINE" ready for processing. You can not simply use the Sender.Read method and expect to get the next "LINE" from the Client. You get raw bytes.
For example, the "LINE" you've posted above has 68 bytes of "raw" data:
6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>
If a Client would send you 3 such LINES of data, the TCP/IP stream would look something like this:
6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>
Depending on the API used by the Client, the Network used to send that data (hubs, gateways, routers, proxies, ...), your Server configuration and the time it takes your Server to read each package of that raw data, using one line here to represent a single OnDataReceived event with one call to Sender.Read, the data you will receive on the Server could look like this ...
6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>
... or this ...
6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF> 6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF> 6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo <CR><LF><LF>
... or this ...
6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF> 6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo <CR><LF><LF>6583750,067-232-1,1-CV,05-22-17 17:49:46,GIANV,gcarlo<CR><LF><LF>
... or this ...
6583750, 067-232-1, 1-CV, 05-22-17 17:49:46, GIANV, gcarlo<CR><LF><LF> 6583750,067-232-1, 1-CV,05-22-17 17:49:46, GIANV,gcarlo <CR><LF><LF>6583750, 067-232-1,1-CV, 05-22-17 17:49:46, GIANV,gcarlo<CR><LF> <LF>
... or any other combination of raw bytes sent by the Client, either split into multiple packages or combined.
This is why, when using raw TCP/IP, you HAVE TO parse the raw data you receive to extract the content you need. If that is NOT the problem, then how exactly does it look like when a LINE gets "lost"? What does the Client send to the Server and what do you receive on the Server? Are you checking Connect and Disconnect events on the Server?
If you really do "lose" a LINE, your problem might be that your connections are being dropped because the Network becomes over-flooded with packages and the Network either does NOT have enough bandwidth to handle all that data, or the Server is unable to process all Clients requests on time.
Anyway ... you should first update your Server to parse the content it receives to make sure the problem isn't caused by data fragmentation. Also, make sure to log clients connect and disconnect events, so you will know if you are losing TCP/IP packets while the connection is active (which should NOT happen if everything is working correctly) - or if you are losing connections (which would be possible if the Network or your Server can NOT handle the load).
Best Regards, Danijel Tkalcec
|