C
Join ServerC#
help
Hi, I can't get to deserialize objects while sending them over a socket while using BinaryFormatter
XXenu1/5/2023
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;
}
}
XXenu1/5/2023
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);
}
}
}
XXenu1/5/2023
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;
}
}
XXenu1/5/2023
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)
BBuddy1/5/2023
Just note, BinaryFormatter is insecure and cannot be made secure.
BBuddy1/5/2023
Prefer other APIs and/or libraries, such as MessagePack
XXenu1/5/2023
one question
XXenu1/5/2023
why sockets are so godamn ducked up
XXenu1/5/2023
whyyyyyyyy …
BBuddy1/5/2023
Huh?
XXenu1/5/2023
idk
XXenu1/5/2023
I just spent like a good 3h trying to make this "thing" work
BBuddy1/5/2023
Because networking is complicated
XXenu1/5/2023
I know, I did this kind of stuff in java
BBuddy1/5/2023
hard to understand as a beginner.
Practice makes perfect.
Practice makes perfect.
XXenu1/5/2023
but back then … it just clicked
BBuddy1/5/2023
Sockets is pretty much the same for all languages
BBuddy1/5/2023
Well, basics at least.
XXenu1/5/2023
although now… its like wtf is going on
XXenu1/5/2023
ye
BBuddy1/5/2023
Whenever you send a packet, include the length of the packet with it
BBuddy1/5/2023
as well as the type of the packet
BBuddy1/5/2023
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.
XXenu1/5/2023
alrighty, thanks
BBuddy1/5/2023
Just note, also validate everything received.
Such as if you receive an invalid packet, it will not try and deserialize it.
Such as if you receive an invalid packet, it will not try and deserialize it.
BBuddy1/5/2023
Usually you just kick the user if they send an invalid packet
XXenu1/5/2023
oke doke
XXenu1/5/2023
thanks man
AAccord1/6/2023
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.