我正在尝试将对象列表发送到另一台计算机。我是套接字等的新手,但尝试了很多东西。最后,我总是以错误告终,无法超越。
我发送的对象列表是具有函数、变量和属性的类。我已将 [Serializable] 放在此类的顶部。
每次客户端执行某个操作时,它会将 San_Change 更改为 true 并启动客户端线程。
我在下面附上了我的代码,任何帮助将不胜感激。
public void Create_Network_Thread(string Host_Or_Client)
{
//This creates a network thread and starts it.
//Host
if (Host_Or_Client == "Host")
{
Host_networkThread = new Thread(new ThreadStart(Host_Network_Thread_Start));
Host_networkThread.Start();
}
//Client
else
{
Client_networkThread = new Thread(new ThreadStart(Client_Network_Thread_Start));
Client_networkThread.Start();
}
}
private void Host_Network_Thread_Start()
{
try
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
clientSocket = serverSocket.AcceptTcpClient();
while (true)
{
object San_Object;
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[clientSocket.Available];
networkStream.Read(bytesFrom, 0, clientSocket.Available);
San_Object = ByteArrayToObject(bytesFrom);
San_List = (List<San>)San_Object;
byte[] sendBytes = ObjectToByteArray(San_Object);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private void Client_Network_Thread_Start()
{
while (true)
{
if (San_Change == true)
{
San_Change = false;
try
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
object San_Object = San_List;
string cur_ip = "";
clientSocket.Connect(cur_ip, 8888);
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = ObjectToByteArray(San_Object);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[(int)clientSocket.ReceiveBufferSize];
San_Object = ByteArrayToObject(inStream);
San_List = (List<San>)San_Object;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
private byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
private Object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object)binForm.Deserialize(memStream);
return obj;
}
}
运行时会发生这种情况:
忘了添加这个,但是当我运行它时,代码失败了。这是信息,希望对您有所帮助。再次感谢。
e {System.Runtime.Serialization.SerializationException: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at San_Player_V2.Game.ByteArrayToObject(Byte[] arrBytes) in San_Player_V2\Game.cs:line 573
at San_Player_V2.Game.Client_Network_Thread_Start() in San_Player_V2\Game.cs:line 477} System.Exception {System.Runtime.Serialization.SerializationException}