2011-01-27 36 views
1

我想建立一個聊天!現在我的目標是接收來自用戶的輸入(將被輸入到一個類中的函數中),保存並通過網絡將該對象發送給用戶。你如何通過網絡發送一個序列化的對象?

這裏是我到目前爲止的代碼:

namespace ConsoleApplication1 
{ 
    class Program 
    {  
     static void Main(string[] args) 
     { 
      TcpListener server = new TcpListener(IPAddress.Any, 5000); 
      server.Start(); 
      Console.WriteLine("Server started"); 
      int a = 0; 

      while (true) 
      { 
       TcpClient connection = server.AcceptTcpClient(); 
       Console.WriteLine("connection accepted"); 

       ThreadPool.QueueUserWorkItem(ProssecClient, connection); 
      } 
     } 

     public static void ProssecClient(object o) 
     { 
      TcpClient connection = o as TcpClient; 
      if (connection == null) 
       return; 
      StreamReader sr = new StreamReader(connection.GetStream()); 
      StreamWriter sw = new StreamWriter(connection.GetStream()); 
      string word = ""; 
      savedObject saved = new savedObject(); 

      try 
      { 
       while (true) 
       { 
        sw.WriteLine(sr.ReadLine()); 
        sw.Flush(); 

        // here the server should read and retrieve, 
        // everything that it gets to every user that logs in. 
       } 
      } 
      catch 
      { 
       Console.WriteLine("client left"); 
      } 
     }   
    } 
} 

我已經保存在BinaryFormatter的一切,我如何將其發送到用戶接收?

客戶端代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
using System.IO; 
using System.Threading; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      TcpClient connection = new TcpClient("127.0.0.1", 5000); 
      StreamReader sr = new StreamReader(connection.GetStream()); 
      StreamWriter sw = new StreamWriter(connection.GetStream()); 
      savedObject saved = new savedObject(); 
      Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create); 
      BinaryFormatter bformatter = new BinaryFormatter(); 
      string word = ""; 
      string allwords = ""; 

      Thread Listen = new Thread(deserialise); 
      Listen.Start(); 

      while (true) 
      { 
       word = Console.ReadLine(); 
       allwords = saved.AllWords(word); 
       sw.WriteLine(allwords); 
       sw.Flush(); 
       Console.WriteLine(sr.ReadLine()); 

       //Serialize 
       //bformatter.Serialize(stream, saved); 
       //stream.Close(); 

       //sw.WriteLine(saved); 
      } 
     } 
    } 

    public static void deserialise() 
    { 
     //Deserialize 
     //if (File.Exists("EmployeeInfo.osl")) 
     //{ 
     // stream = File.Open("EmployeeInfo.osl", FileMode.Open); 
     // bformatter = new BinaryFormatter(); 

     // saved = (savedObject)bformatter.Deserialize(stream); 
     // stream.Close(); 
     //} 

    } 
} 

[Serializable()] 
class savedObject : ISerializable 
{ 
    public string allwords; 

    public string AllWords(string words) 
    { 
     allwords += words + " "; 
     return allwords; 
    } 

    public void Words(SerializationInfo info, StreamingContext ctxt) 
    { 
     info.AddValue("RetrievedWord", allwords); 
    } 


    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     allwords = (String)info.GetValue("RetrievedWord", typeof(string)); 
    } 
} 

回答

1

在ProcessClient方法:

TcpClient client = (TcpClient) connection; 

using(StreamWriter streamWriter = new StreamWriter(tcpClient.GetStream())) 
{ 
    BinaryFormatter binaryFormatter = new BinaryFormatter(); 
    binaryFormatter.Serialize(streamWriter, savedObject); 
} 
+0

我已經改變了我的代碼位。我依靠客戶端來持有序列化對象並將其傳遞給服務器,然後服務器應將其傳遞迴客戶端。 – 2011-01-27 11:45:03

+0

以下是客戶端類型。這轉到一個對象,對象被序列化----------->去服務器(服務器將序列化的對象傳遞給每個登錄的用戶)----->客戶端將序列化對象並讀取它的內容...這裏是客戶端代碼.... – 2011-01-27 11:47:01

0

你可以使用SOAP Arhitecture(使用XML序列化不Bynary序列化)這將是實現多easyer。或者如果您需要點對點聊天code here

2

考慮WCF。

它從一致的高層次角度處理包括安全性,不同協議等在內的所有通信問題。

它幾乎是.Net中的通信標準,包含並取代了較老的更低級別的技術。

對於如何建立使用WCF聊天服務一個很好的教程看到WCF/WPF Chat Application

相關問題