如何通过tcp/ip协议发送包的长度

发布于 2024-11-02 12:31:03 字数 2475 浏览 3 评论 0原文

我正在为我的一个学校项目这样做。我正在尝试设计一个多线程服务器,它接受客户端使用数据库(添加、删除记录等)。当我将客户端连接到服务器时,我想接收数据库中的所有学生。

我访问服务器端的数据库并将信息存储在 ArrayList 中,我试图通过网络发送它。我对 XMLserializing 没有任何了解,所以我尝试将 arrayList 中的每个字符串发送到客户端。当我从服务器发送数据时,我有时会同时收到所有数据,有时则不会,所以我的第一个猜测是我必须将发送的数据分成一定长度的包。我不知道如何在包的开头添加长度。难道不是同一件事吗?也许我得到了正确的长度,也许我没有。

这是我的代码;我还没有尝试发送每个包裹的长度,因为我不知道如何发送。我尝试从服务器发送数组列表的长度,并从网络流中读取多次,但它不起作用(我在一个包中接收所有数据)。

服务器端:

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);
        }

    }

客户端:

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 < 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);
        }

    }
}

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.

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.

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).

Server side:

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);
        }

    }

On Client:

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 < 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);
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-11-09 12:31:03

您可以使用 StateObject 来跟踪数据有多大,然后在 ReadCallback 期间进行测试以查看是否拥有“全部”消息。如果您没有收到所有消息,则可以使用当前的 StateObject 再次调用 BeginRecieve。

这是一个不错的例子:http://www.csharphelp。 com/2007/02/asynchronous-server-socket-using-c/

You can use a StateObject to keep track of how large your data is, and then test during ReadCallback to see if you have "all" of the message. If you don't have all of your message, you call BeginRecieve again with the current StateObject.

Here is a decent example: http://www.csharphelp.com/2007/02/asynchronous-server-socket-using-c/

单身狗的梦 2024-11-09 12:31:03

这就是我一直在使用的:

如何在 SocketAsyncEventArgs 对象上使用缓冲区< /a>

查看已接受的答案。首先,这是使用比异步更高效的调用完成端口。其次,我发现通过查看e.SocketError来找出失败的确切原因非常容易排除故障。

其工作原理是,要发送消息,请在消息中附加标头和标尾。
因此,当它收到消息时,它会检查是否收到预告片。如果未收到预告片,它将继续为该客户端接收消息,并将收到的消息附加到 stringBuilder 对象。收到预告片后,只需调用 stringbuilder.toString() 即可获取全部内容。

This is what I been using:

How to use the buffer on SocketAsyncEventArgs object

Look at the accepted answer. 1st off, this is using something call completion port which is highly efficient than async. Secondly, I find that it is very easy to troubleshoot by looking at e.SocketError to find out the exact cause for failure.

How it works is that for your message to send, append the message with a header and trailer.
So when it receive the message, it will check if the trailer is received. If trailer not received, it will continue receive for that client, and append the received message to the stringBuilder object. Once the trailer is received, just call stringbuilder.toString() to get the whole content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文