Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send length of a package via tcp/ip protocol
    primarykey
    data
    text
    <p>I'm doing this for one of my school projects. I'm trying to design a multi-threaded server that accepts clients for working with a database (adding, deleting records etc). When I connect the client to the server I want to receive all the students in my database.</p> <p>I access the database on the Server Side and store the information in an ArrayList, which I'm trying to send it over the network. I don't have any knowledge on XMLserializing so I'm trying to send each string in the arrayList to the client. When I send the data from the server, I sometimes receive all the data in the same time, sometimes I don't, so my first guess was that I have to split the data I send into packages of some length. I don't see how can I add the length at the beginning of a package. Wouldn't it be the same thing? Maybe I get the correct length maybe I don't. </p> <p>Here is my code; I didn't try sending the length of each package yet, because I have no idea how. I tried sending from the server the length of the arraylist, and read from the network stream that many times, but it doesn't work ( I receive all data in one package). </p> <p>Server side: </p> <pre><code>private void HandleClient(object client) { try { ClientNo++; TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] bytes = new byte[4096]; int i; // Robot r = new Robot(); Protocol p = new Protocol(); ArrayList ListaStudentiResponse = p.ExecuteQueryOnStudents("select * from studenti"); byte[] Length = new byte[4]; Length = System.Text.Encoding.ASCII.GetBytes(ListaStudentiResponse.Count.ToString()); clientStream.Write(Length, 0, Length.Length); foreach ( String s in ListaStudentiResponse) { byte[] data = System.Text.Encoding.ASCII.GetBytes(s); clientStream.Write(data, 0, data.Length); } tcpClient.Close(); ClientNo--; } catch (Exception ex) { Console.WriteLine(ex.Message); } } </code></pre> <p>On Client: </p> <pre><code>private void connectToServerToolStripMenuItem_Click(object sender, EventArgs e) { tcpclient = new TcpClient(); NetworkStream netStream; try { tcpclient.Connect("localhost", 8181); netStream = tcpclient.GetStream(); Byte[] bytes = new Byte[10000]; int readBytes = netStream.Read(bytes, 0, bytes.Length); int Length =Int32.Parse(Encoding.ASCII.GetString(bytes, 0, readBytes)); MessageBox.Show(Length.ToString()); int i = 0; while (i &lt; Length) { i++; Byte[] b = new Byte[10000]; readBytes = netStream.Read(bytes, 0, bytes.Length); String response = Encoding.ASCII.GetString(b, 0, readBytes); MessageBox.Show(response); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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