Note that there are some explanatory texts on larger screens.

plurals
  1. POSend a list of class objects through a socket or tcp
    primarykey
    data
    text
    <p>I'm trying to send a list of objects to another computer. I'm new to sockets and such but have tried quite a few things. In the end I always end up with errors and can't get past them. </p> <p>The list of objects I'm sending are classes with functions, variables and properties. I have placed the [Serializable] at the top of this class.</p> <p>Each time a client performs a certain action it will change the San_Change to true and start the client thread. </p> <p>I've attached my code below, and any help would be appreciated.</p> <pre><code> 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&lt;San&gt;)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&lt;San&gt;)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; } } </code></pre> <p>When ran this occurs:</p> <p>Forgot to add this in, but when I run this as it is the code fails. Here is the information, hope this helps. Thanks again.</p> <pre><code>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} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload