Note that there are some explanatory texts on larger screens.

plurals
  1. POTCP server client issue
    primarykey
    data
    text
    <p>First i am n00b in socket programming. So i decided to write simple data over lan tcp server My server code that handles incomming data is</p> <pre><code> private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); clientStream.ReadTimeout = 10; int size = 4096 * 1000; byte[] message = new byte[size]; byte[] All = new byte[0]; int bytesRead; string error = ""; lock (this) { while (true) { All = new byte[0]; while (true) { bytesRead = 0; try { bytesRead = clientStream.Read(message, 0, size); All = AddBArrays(All, message, bytesRead); } catch { break; } if (bytesRead == 0) { break; } } if (All.Length &gt; 0) { Message m = (Message)Tools.ByteArrayToObject(All); OnRecived(new RecivedArgs("localhost", (Message)Tools.ByteArrayToObject(All))); } } tcpClient.Close(); } } byte[] AddBArrays(byte[] ar1, byte[] ar2, int read) { byte[] concat = new byte[ar1.Length + read]; if (ar1.Length != 0) System.Buffer.BlockCopy(ar1, 0, concat, 0, ar1.Length); System.Buffer.BlockCopy(ar2, 0, concat, ar1.Length, read); return concat; } </code></pre> <p>it works but have some issues. It fales receiving files bigger then 100 mbs or smthng and also if i send data very often interval &lt; 800 then data is lost. how should i improve my code? The large file issue is not so important the primary issue is the data loss in fast data sending. tnx for help</p> <p>Ok i now updated the code by the suggestions</p> <pre><code>private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); clientStream.ReadTimeout = 10; int size = 4096 * 1000; List&lt;byte&gt; Test = new List&lt;byte&gt;(); byte[] message = new byte[size]; byte[] All = new byte[0]; int bytesRead; while (true) { //All = new byte[0]; while (true) { bytesRead = 0; try { bytesRead = clientStream.Read(message, 0, size); for (int i = 0; i &lt; bytesRead; i++) { Test.Add(message[i]); } } catch { break; } if (bytesRead == 0) { break; } } if (Test.Count &gt; 0) { Message m = (Message)Tools.ByteArrayToObject(Test.ToArray()); OnRecived(new RecivedArgs("localhost", m)); Test = new List&lt;byte&gt;(); } } tcpClient.Close(); } </code></pre> <p>but the issues still there</p> <p>Edit--> large file issue fixed it was just a 'System.OutOfMemoryException' but it didn't throw a error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload