2011-12-31 242 views
0

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

} 
+0

为什么不使用WCF?它可以将对象序列化为TCP上的二进制文件。非常高效,它为你做这一切工作。 – 2011-12-31 22:38:03

回答

2

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

+0

+ +1为较少理解的消息框架 – 2012-01-01 03:01:00

相关问题