0

我试图将它从一个客户端发送到另一个客户端,源、目标和数据接收良好,但字符串和双精度变为空。这是的代码TcpObject

[Serializable]
public class TcpObject
{
    public Command Command { set; get; }
    public Object Data { set; get; }
    public int Source { set; get; }
    public int Destination { set; get; }
    public string IDString { set; get; }
    public double exactTime { set; get; }

    public TcpObject()
    {
       Command = Command.Null;
       Data = null;
    }

这是我发送数据的地方:

TcpObject tcpObject = new TcpObject();
tcpObject.Command = Command.Msg;
tcpObject.Source = 1;
tcpObject.Destination = 2;
byte[] junkMsg = new byte[1000];
tcpObject.Data = junkMsg;
tcpObject.IDString = randomString();
tcpObject.exactTime = exactTime.ExactTime();
WorkClient.Send(tcpObject);
hread.Sleep(20);

这就是我收到的地方:

public void Listen()
{
   try
   {
      while (Connected)
      {
         TcpObject tcpObject = new TcpObject();
         IFormatter formatter = new BinaryFormatter();
         tcpObject = (TcpObject)formatter.Deserialize(ServerSocket);

         MessageBox.Show(tcpObject.IDString);
         //TcpObject succeedTcpObject = new TcpObject();
         //get the remainder of the time that the message got send and when the message got accepted.
      }
   }
      catch (Exception) { }
}

这是发送函数:

public static void Send(TcpObject tcpObject) 
{
   try
   {
      IFormatter formatter = new BinaryFormatter();
      formatter.Serialize(ServerSocket, tcpObject);
   }
   catch (Exception) { }
}

弹出的MessageBox是空的。我在发送消息之前检查过,并且字符串存在。

谢谢。

4

1 回答 1

1

显然,您发送的垃圾从未初始化:

byte[] junkMsg = new byte[1000];
tcpObject.Data = junkMsg;
--SNIP--
WorkClient.Send(tcpObject);
于 2013-04-11T19:39:59.433 回答