Note that there are some explanatory texts on larger screens.

plurals
  1. PORandom serialization exceptions using Socket
    primarykey
    data
    text
    <p>I'm trying to send data back and forth between only two computers using a Socket. The data is in the form of serialized Packet objects.</p> <p>When testing the program on another computer on my local network, I'm getting random SerializationExceptions so that no data goes through. </p> <p>The program consistently sends different data, so when it makes another pass at sending it again, it will sometimes go through and sometimes hit the same SerializationException again. If I catch the exception and leave it running, all data eventually makes it through, but it takes several tries.</p> <p>The exception says: "The input stream is not a valid binary format. The starting contents (in bytes) are [byte data]"</p> <p>Not sure exactly where my problem lies. The larger amounts of data I send (~100kb max) <strong>always</strong> go through. The smaller ones (50-70 bytes) have trouble. Here's everything to do with my serialization and reading/writing data.</p> <p>Socket defined as such:</p> <pre><code>SocketMain = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); </code></pre> <p>Send &amp; Read methods. I'm aware this is probably a horrible way to do so and might end up being my issue. Suggestions?:</p> <pre><code> public void SendPacket(Packet P) { using (MemoryStream MS = new MemoryStream()) { BinaryFormatter BF = new BinaryFormatter(); BF.Serialize(MS, P); SocketMain.Send(MS.ToArray()); } } public void ReadPacket() { byte[] BufferArray = new byte[131072]; int BytesReceived = SocketMain.Receive(BufferArray); byte[] ActualData = new byte[BytesReceived]; Buffer.BlockCopy(BufferArray, 0, ActualData, 0, BytesReceived); using (MemoryStream MS = new MemoryStream(ActualData)) { BinaryFormatter BF = new BinaryFormatter(); HandlePacket((Packet)BF.Deserialize(MS)); } } </code></pre> <p>Example Packet object. This is one of my smaller ones. I <em>think</em> this might be the one that is causing the issue, but I don't know how I could tell.</p> <pre><code>[Serializable()] public class Packet4BlockVerify : Packet, ISerializable { public byte Index; public string MD5Hash; public Packet4BlockVerify(int Index, string MD5Hash): base(4) { this.Index = (byte)Index; this.MD5Hash = MD5Hash; } protected Packet4BlockVerify(SerializationInfo info, StreamingContext context) { this.ID = info.GetByte("ID"); this.Index = info.GetByte("Index"); this.MD5Hash = info.GetString("MD5Hash"); } public override void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("ID", this.ID); info.AddValue("Index", this.Index); info.AddValue("MD5Hash", this.MD5Hash); } } </code></pre> <p>Does anyone see anything wrong?</p>
    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