Click here to Skip to main content
15,891,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
https://www.codeproject.com/script/Articles/Download.aspx?file=/KB/cs/488668/TcpServer-src.zip&rp=https://www.codeproject.com/


I am using this server socket application and found it is working very good.

I am facing one isssue which is, when client send lengthy strings continuously , then it is showing as splitted lines on text box.3/4 strings as one packet and other part is coming in the next packet.

How can I solve ths?

Pls help

What I have tried:

For intIndex = offset To offset + count - 1
           If Bytes(intIndex) = 13 Then
               RaiseEvent LineReceived(Me, mobjText.ToString)
               mobjText = New StringBuilder()
           Else
               mobjText.Append(ChrW(Bytes(intIndex)))
           End If
       Next
Posted
Updated 5-Mar-20 4:46am
Comments
Richard Deeming 4-Mar-20 10:11am    
If you have a question about an article, then post it in the message forum at the bottom of that article.

The chances of the author stumbling across this question here and realising it's for them are minuscule.

I'm not sure I understand the problem. It looks like the code is creating a new string whenever it sees a "13" (carriage return). But as far as the packets go, TCP is stream-oriented, which means that a packet does not have to end at a "message" boundary. The packets that contain client strings can be split arbitrarily. Each packet could contain part of a string or several strings, and it is up to the receiver to reassemble them and divide them at whatever points make sense, which is what this code appears to be doing.
 
Share this answer
 
Thank u so much.


I solvedmyissue like below

C#
<pre>  private void tcpServer1_OnDataAvailable(tcpServer.TcpServerConnection connection)
        {
            byte[] data = readStream(connection.Socket);
            int intIndex;
            int startindex=0;

            for (intIndex = 0; intIndex < data.Length; intIndex++)
            {             

                if (data[intIndex] == 13)
                {
                    if (data != null)
                    {

                        //string splitstring=data.
                        string dataStr = Encoding.ASCII.GetString(data).Substring(startindex,(intIndex-startindex));
                        //dataStr = dataStr + dataStr.Replace("\r", "").Replace("\n", "") + "\r\n";
                        invokeDelegate del = () =>
                        {
                            logData(false, dataStr);
                        };
                        Invoke(del);

                        startindex = intIndex;
                    }
                }
                else
                {
                }
            }          
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900