套接字编程异常 XML 文档中有错误 (170, 46)

发布于 2024-12-15 03:08:10 字数 1688 浏览 0 评论 0原文

我想通过套接字连接发送对象列表。我知道 WCF 服务将是一个更好的选择,但它不适合我。

我正在使用此代码发送数据

    private void tm_Tick(object sender, EventArgs e)
    {

        XmlSerializer formatter = new XmlSerializer(typeof(List<Objects.PIP>));

        MemoryStream stream = new MemoryStream(1024);

        formatter.Serialize(stream, Repository.GlobalRepository.PIPInformation);

        byte[] bt = stream.ToArray();
        foreach (Communication.Client Client in server.ClientList)
        {
            Client.SendMessage(bt);

        }
        stream.Flush();
    }

    public void SendMessage(Byte[] bytesSent)
    {
        SocketAsyncEventArgs writeEventArgs = new SocketAsyncEventArgs();
        writeEventArgs.SetBuffer(bytesSent, 0, bytesSent.Length);
        socket.SendAsync(writeEventArgs);
    }

似乎工作正常。

为了接收数据,我在线程上使用此代码。

void ReceiveData()
    {
        try
        {

            while (true)
            {
                if (disposed == true)
                    return;
                data = new byte[socket.ReceiveBufferSize];
                var recv = socket.Receive(data);

                XmlSerializer formatter = new XmlSerializer(typeof(List<Object.PIP>));
                MemoryStream stream = new MemoryStream(data);
                **Classes.TickerInformation.PIPList= (List<Object.PIP>)formatter.Deserialize(stream);**

            }
            socket.Close();
            return;
        }
        catch (Exception ex)
        {

        }

    }

我收到异常 XML 文档中存在错误 (170, 46)。在这一行中: Classes.TickerInformation.PIPList= (List)formatter.Deserialize(stream);

我假设没有收到所有数据,所以这种情况正在发生。

I want to send a list of objects through a socket connection. And i know WCF service would be a better option but its not a option for me.

I am using this code to send the data

    private void tm_Tick(object sender, EventArgs e)
    {

        XmlSerializer formatter = new XmlSerializer(typeof(List<Objects.PIP>));

        MemoryStream stream = new MemoryStream(1024);

        formatter.Serialize(stream, Repository.GlobalRepository.PIPInformation);

        byte[] bt = stream.ToArray();
        foreach (Communication.Client Client in server.ClientList)
        {
            Client.SendMessage(bt);

        }
        stream.Flush();
    }

    public void SendMessage(Byte[] bytesSent)
    {
        SocketAsyncEventArgs writeEventArgs = new SocketAsyncEventArgs();
        writeEventArgs.SetBuffer(bytesSent, 0, bytesSent.Length);
        socket.SendAsync(writeEventArgs);
    }

Seems to work fine.

to receive data i am using this code on a thread.

void ReceiveData()
    {
        try
        {

            while (true)
            {
                if (disposed == true)
                    return;
                data = new byte[socket.ReceiveBufferSize];
                var recv = socket.Receive(data);

                XmlSerializer formatter = new XmlSerializer(typeof(List<Object.PIP>));
                MemoryStream stream = new MemoryStream(data);
                **Classes.TickerInformation.PIPList= (List<Object.PIP>)formatter.Deserialize(stream);**

            }
            socket.Close();
            return;
        }
        catch (Exception ex)
        {

        }

    }

i am getting an Exception There is an error in XML document (170, 46). in this line:
Classes.TickerInformation.PIPList= (List)formatter.Deserialize(stream);

I assume the all the data is not being recieved so this is happening.

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

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

发布评论

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

评论(1

只有影子陪我不离不弃 2024-12-22 03:08:10

问题是您直到最后都没有收到整个 XML - 因此您只序列化了 XML 的初始部分。您必须从套接字读取数据,直到没有数据为止。

我建议设置一个由客户端发送的终止符字符串,以便您查找它以知道您已完全收到消息。

请记住,套接字不是基于请求-响应的。您打开套接字并继续阅读。

The problem is you have not received the whole of the XML till the end - so you are serialising only the initial part of the XML. You have to read from the socket until there is no data.

I suggest to setup a terminator string sent by the client so you look for it to know you have fully received the message.

Bear in mind, sockets are not based on request-response. you open the socket and keep reading.

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