套接字编程异常 XML 文档中有错误 (170, 46)
我想通过套接字连接发送对象列表。我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您直到最后都没有收到整个 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.