C# - 解析完成之前遇到的流结束

时间:2016-10-30 13:09:05

标签: c# networking

我正在尝试通过网络发送文件。为此,我将文件转换为字符串并通过网络将其与我的Packet类一起发送,但当我想发送它时,我的服务器崩溃在Packet p = (Packet)bf.Deserialize(ms); 与SerializationException在解析完成之前遇到的End of Stream,但为什么呢? 这是我的包类:

    [Serializable]
public class Packet
{
    public List<String> gData;
    public int packetInt;
    public bool packetBool;
    public string senderID;
    public PacketType packetType;
    public string packetID;

    public Packet(PacketType packetType, string senderID)
    {
        gData = new List<String>();
        this.senderID = senderID;
        this.packetType = packetType;
        packetID = Guid.NewGuid().ToString();
    }

    public Packet(byte[] bytes)
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream(bytes);

        ms.Position = 0;

        Packet p = (Packet)bf.Deserialize(ms);
        ms.Close();

        gData = p.gData;
        packetBool = p.packetBool;
        packetInt = p.packetInt;
        packetType = p.packetType;
        senderID = p.senderID;
        packetID = Guid.NewGuid().ToString();
    }

    public byte[] ToBytes()
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();

        ms.Position = 0;

        bf.Serialize(ms, this);
        byte[] bytes = ms.ToArray();
        ms.Close();
        return bytes;
    }

    public static string GetIP4Address()
    {
        IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());

        foreach(IPAddress ip in ips)
        {
            if(ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        return "127.0.0.1";
    }
}

我发送数据包并将文件转换为字符串:

private void OpenButton_Sound_Click(object sender, EventArgs e)
    {
        openFileDialog.Filter = "MP3 Dateien | *.mp3";
        DialogResult r = openFileDialog.ShowDialog();
        if(r == DialogResult.OK)
        {
            String path = openFileDialog.FileName;
            File.Copy(path, path.Replace("mp3", "wav"));
            code = Convert.ToBase64String(File.ReadAllBytes(path));
            SoundPathText_Sound.Text = path;
        }
    }

    private void ErstenButton_Sound_Click(object sender, EventArgs e)
    {
        Packet p = new Packet(PacketType.Window, "server");
        p.gData.Add(SoundForm.SERIALAZIE_ID);
        p.gData.Add(code);
        Server.SendPacketToAll(p);
    }

接收代码:

        public static Socket master;
    public string username;
    public static string id;
    private String ip;
    private int port;

    public Client(String username, String ipPortString)
    {
        InitializeComponent();
        this.username = username;
        ip = ipPortString.Split(':')[0];
        port = Convert.ToInt32(ipPortString.Split(':')[1]);
    }
    private void Client_Load(object sender, EventArgs e){}

    public void Start()
    {
        master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);
        try
        {
            master.Connect(point);
        }
        catch
        {
            MessageBox.Show("Fehler beim Verbinden! Code: 0x156f237" + "\n" + "Der Server konnte nicht gefunden werden!");
            Thread.Sleep(1000);
            Start();
        }
        Thread t = new Thread(Data_IN);
        t.Start();
    }

    void Data_IN()
    {
        byte[] buffer;
        int readBytes;

        for (;;)
        {
            buffer = new byte[master.SendBufferSize];
            readBytes = master.Receive(buffer);

            if(readBytes > 0)
            {
                DataManager(new Packet(buffer));
            }
        }
    }

0 个答案:

没有答案