通过TCP发送序列化对象

时间:2011-12-31 18:23:56

标签: c# serialization tcp

我正在研究一个非常简单的服务器/客户端程序。我想将服装类从服务器发送到客户端,反之亦然。经过一些研究,我想出了如何发送一个对象并接收它。虽然我发送了一个对象后,我似乎无法再发送了。发送消息不会抛出任何异常,但接收者永远不会收到消息。任何帮助将不胜感激。

public abstract class baseClient
{
    private Player player;

    private Thread chat;
    private NetworkStream messageStream;
    private BinaryFormatter formatter;
    private List<MSG> MSGM;
    private List<String> MSGS;
    private BinaryReader reader;
    private BinaryWriter writer;

    private String ExceptionMessage;

    public baseClient()
    {
        formatter = new BinaryFormatter();
        MSGM = new List<MSG>();
        MSGS = new List<String>();
        chat = new Thread(new ThreadStart(doChat));
        chat.Start();
    }
    public abstract void doChat();


    public void setMessageStream(NetworkStream stream) { messageStream = stream; }
    public void setReader(BinaryReader reader) { this.reader = reader; }
    public void setFormatter(BinaryFormatter formatter) { this.formatter = formatter; }
    public void setPlayer(Player player) { this.player = player; }
    public void setExceptionMessage(String message) { ExceptionMessage = message; }
    public void clearMessageMSG() { MSGM.Clear(); }
    public void clearStringMSG() { MSGS.Clear(); }
    public void setWriter(BinaryWriter writer) { this.writer = writer; }


    public NetworkStream getMessageStream() { return messageStream; }
    public void addMSGM(MSG obj) { MSGM.Add(obj); }
    public void addMSGS(String obj) { MSGS.Add(obj); }
    public BinaryReader getReader() { return reader; }
    public BinaryFormatter getFormatter() { return formatter; }
    public List<MSG> getMessageMSG() { return MSGM; }
    public List<String> getStringMSG() { return MSGS; }
    public BinaryWriter getWriter() { return this.writer; }
    public Player getPlayer() { return player; }

    public void handleInput()
    {
        try
        {
            Object obj = getObject();

            if (obj != null)
            {
                if (obj is Player)
                    setPlayer((Player)obj);
                if (obj is MSG)
                    addMSGM((MSG)obj);
                else if (obj is String)
                    addMSGS((String)obj);
            }

        }
        catch (Exception e)
        {
            setExceptionMessage(e.ToString());
        }
    }
    public Object getObject()
    {
        try
        {


            Stream stream = getReader().BaseStream;

            object obj = formatter.Deserialize(stream);

            stream.Flush();
           // stream.Close();
            return obj;
        }
        catch (Exception e)
        {
            ExceptionMessage= e.ToString();
            return e;
        }
    }
    public String getString()
    {
        try
        {
            string reply = reader.ReadString();
            return reply;
        }
        catch (Exception e)
        {
            ExceptionMessage = e.ToString();
            return null;
        }
    }
    public bool sendMessage(Object msg)
    {
        try
        {
            Byte[] buffer = SerializeMultipleObjects(msg).GetBuffer();
            getMessageStream().Write(buffer, 0, buffer.Length);
            getMessageStream().Flush();
            return true;
        }
        catch (Exception e)
        {
            setExceptionMessage(e.ToString());
            return false;
        }
    }
    public bool sendString(String msg)
    {
        try
        {
            writer.Write(msg);
            writer.Flush();
            return true;
        }
        catch (Exception e)
        {
            setExceptionMessage(e.ToString());
            return false;
        }
    }
    private MemoryStream SerializeMultipleObjects(Object obj)
    {
        MemoryStream stream = new MemoryStream();
        getFormatter().Serialize(stream, obj);
        stream.Flush();
        return stream;
    }

}

1 个答案:

答案 0 :(得分:2)

我建议您使用已建立的消息传递框架,例如WCF。你坚持编写自己的TCP / IP协议,需要message framing