简体   繁体   中英

Sending Serialized Objects over TCP

I am working on a very simply server/client program. I am wanting to send a costume class from the server to the client and vice versa. After some research, I figured out how to send an object and receive it. Although after I send an object, I can't seem to send any more. The send message doesn't throw any exceptions but the receiver never gets the message. Any help would be appreciated.

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;
    }

}

I recommend you use an established messaging framework such as WCF. It you insist on writing your own TCP/IP protocol, you will need message framing .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM