C
C#2y ago
Lasek

Hi, I can't get to deserialize objects while sending them over a socket while using BinaryFormatter

that's the code for a server which receives data from client:
C#
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using Data;
using System.Text;

var addr= await Dns.GetHostAddressesAsync("localhost");
IPEndPoint ipEndPoint = new IPEndPoint(addr[0], 2156);
using Socket serverSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(100);
Socket socket = await serverSocket.AcceptAsync();

using (var networkStream = new NetworkStream(socket))
{
int byt=0;
while (true)
{

BinaryFormatter binaryFormatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
DataPacket dataPacket= (DataPacket)binaryFormatter.Deserialize(networkStream);
Console.WriteLine(dataPacket.message);
#pragma warning restore SYSLIB0011
break;
}
}
C#
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using Data;
using System.Text;

var addr= await Dns.GetHostAddressesAsync("localhost");
IPEndPoint ipEndPoint = new IPEndPoint(addr[0], 2156);
using Socket serverSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(100);
Socket socket = await serverSocket.AcceptAsync();

using (var networkStream = new NetworkStream(socket))
{
int byt=0;
while (true)
{

BinaryFormatter binaryFormatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
DataPacket dataPacket= (DataPacket)binaryFormatter.Deserialize(networkStream);
Console.WriteLine(dataPacket.message);
#pragma warning restore SYSLIB0011
break;
}
}
16 Replies
Lasek
LasekOP2y ago
and this is the code for a client which sends data
C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using Data;
namespace SocketFileTransfer
{
internal class Program
{
static void Main(string[] args)
{
Connect();
}

[Obsolete("Obsolete")]
public static async void Connect()
{
IPEndPoint ipEndPoint = new IPEndPoint(Dns.GetHostAddresses("localhost")[0], 2156);
using Socket client = new(
ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
await client.ConnectAsync(ipEndPoint);
using (var myStream = new NetworkStream(client)) {
BinaryFormatter binaryFormatter = new BinaryFormatter();
DataPacket dataPacket = new DataPacket(0, "yeeee");
binaryFormatter.Serialize(myStream,dataPacket);
}


client.Shutdown(SocketShutdown.Both);
}
}
}
C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using Data;
namespace SocketFileTransfer
{
internal class Program
{
static void Main(string[] args)
{
Connect();
}

[Obsolete("Obsolete")]
public static async void Connect()
{
IPEndPoint ipEndPoint = new IPEndPoint(Dns.GetHostAddresses("localhost")[0], 2156);
using Socket client = new(
ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
await client.ConnectAsync(ipEndPoint);
using (var myStream = new NetworkStream(client)) {
BinaryFormatter binaryFormatter = new BinaryFormatter();
DataPacket dataPacket = new DataPacket(0, "yeeee");
binaryFormatter.Serialize(myStream,dataPacket);
}


client.Shutdown(SocketShutdown.Both);
}
}
}
also a data class:
C#
namespace Data;
[Serializable]
public class DataPacket
{
public int id;
public string message;

public DataPacket(int id, string message)
{
this.id = id;
this.message = message;
}
}
C#
namespace Data;
[Serializable]
public class DataPacket
{
public int id;
public string message;

public DataPacket(int id, string message)
{
this.id = id;
this.message = message;
}
}
I get:
/home/xenu/RiderProjects/SocketFileTransfer/ServerSocketFileTransfer/bin/Debug/net7.0/ServerSocketFileTransfer
Unhandled exception. System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.
at System.Runtime.Serialization.Formatters.Binary.BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(BinaryParser serParser)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at Program.<Main>$(String[] args) in /home/xenu/RiderProjects/SocketFileTransfer/ServerSocketFileTransfer/Program.cs:line 25
at Program.<Main>(String[] args)
/home/xenu/RiderProjects/SocketFileTransfer/ServerSocketFileTransfer/bin/Debug/net7.0/ServerSocketFileTransfer
Unhandled exception. System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.
at System.Runtime.Serialization.Formatters.Binary.BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(BinaryParser serParser)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at Program.<Main>$(String[] args) in /home/xenu/RiderProjects/SocketFileTransfer/ServerSocketFileTransfer/Program.cs:line 25
at Program.<Main>(String[] args)
Buddy
Buddy2y ago
Just note, BinaryFormatter is insecure and cannot be made secure. Prefer other APIs and/or libraries, such as MessagePack
Lasek
LasekOP2y ago
one question why sockets are so godamn ducked up whyyyyyyyy …
Buddy
Buddy2y ago
Huh?
Lasek
LasekOP2y ago
idk I just spent like a good 3h trying to make this "thing" work
Buddy
Buddy2y ago
Because networking is complicated
Lasek
LasekOP2y ago
I know, I did this kind of stuff in java
Buddy
Buddy2y ago
hard to understand as a beginner. Practice makes perfect.
Lasek
LasekOP2y ago
but back then … it just clicked
Buddy
Buddy2y ago
Sockets is pretty much the same for all languages Well, basics at least.
Lasek
LasekOP2y ago
although now… its like wtf is going on ye
Buddy
Buddy2y ago
Whenever you send a packet, include the length of the packet with it as well as the type of the packet Packet Type | Packet Length | Payload 1. Allocate the bytes required for header. 2. Receive bytes equal to header size (which contains the header IE; Type and Length) 3. After that receive bytes equal to the size of packet length. 4. Check packet type, deserialize based on the type of packet.
Lasek
LasekOP2y ago
alrighty, thanks
Buddy
Buddy2y ago
Just note, also validate everything received. Such as if you receive an invalid packet, it will not try and deserialize it. Usually you just kick the user if they send an invalid packet
Lasek
LasekOP2y ago
oke doke thanks man
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server