Bonjour,

Dans le cadre de mon programme, j'aimerais envoyer un objet par un socket. Mais pas uniquement une class.

Le but, c'est d'envoyer ceci :
INT32 : Code de l'objet
INT32 : Taille de l'objet
object : L'objet en lui-même

Comment puis-je faire ça ?
Je crois comprendre que mon erreur provient de l'envoi. Je dois avouer que je ne comprends pas tout à fait comment fonctionne les MemoryStream et les BinaryFormatter.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
static public class ProtocolMech
{
    static public ProtocolMessage GetMessage(Socket Src)
    {
        Int32 size;
        Int32 type;
        byte[] msg;
        if (Src.Available > 2 * sizeof(Int32))
        {
            msg = new byte[sizeof(Int32)];
            Src.Receive(msg, sizeof(Int32), SocketFlags.None);
            type = (Int32)BitConverter.ToUInt32(msg, 0);
 
            msg = new byte[sizeof(Int32)];
            Src.Receive(msg, sizeof(Int32), SocketFlags.None);
            size = (Int32)BitConverter.ToUInt32(msg, 0);
 
            msg = new byte[size];
            Src.Receive(msg, size, SocketFlags.None);
 
            return new ProtocolMessage(type, ByteArrayToObject(msg));
        }
        else
            return null;
    }
 
 
    static public void SendMessage(Int32 Code, object Obj, Socket Dest)
    {
 
        MemoryStream _MemoryStream = new MemoryStream();
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
 
        byte[] objbuff = ObjectToByteArray(Obj);
        Int32 SizeOfObj = Convert.ToInt32(objbuff.Length);
 
        _BinaryFormatter.Serialize(_MemoryStream, Code);
        _BinaryFormatter.Serialize(_MemoryStream, SizeOfObj);
        _BinaryFormatter.Serialize(_MemoryStream, Obj);
 
        Dest.Send(_MemoryStream.ToArray(), SocketFlags.None);
    }
 
    static public object ByteArrayToObject(byte[] _ByteArray)
    {
        try
        {
            MemoryStream _MemoryStream = new MemoryStream(_ByteArray);
 
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                        = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            _MemoryStream.Position = 0;
 
            return _BinaryFormatter.Deserialize(_MemoryStream);
        }
        catch (Exception _Exception)
        {
            // Error
            throw (_Exception);
        }
    }
 
    static public byte[] ObjectToByteArray(object Obj)
    {
        try
        {
            MemoryStream _MemoryStream = new MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                        = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            _BinaryFormatter.Serialize(_MemoryStream, Obj);
            return _MemoryStream.ToArray();
        }
        catch (Exception _Exception)
        {
            // Error
            throw (_Exception);
        }
    }
}
Merci d'avance.