2011-01-27 5 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に保存されているすべてのものを持っていますか?

クライアント側のコード:ProcessClient方法で

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

TcpClient client = (TcpClient) connection; 

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

を参照してくださいWCFを使用したチャットサービスを構築する方法の良いチュートリアルについて

。私は、シリアル化されたオブジェクトを保持し、それをサーバーに渡すためにクライアント側に依存しているので、サーバーはそれをクライアントに戻す必要があります。 –

+0

ここにその方法を示します:クライアントタイプ。これはオブジェクトに行き、オブジェクトはシリアライズされます----------->サーバーに行きます(サーバーはシリアル化されたオブジェクトをログインしたすべてのユーザーに渡します)----->クライアントはオブジェクトがあり、その上にあるものを読み込みます...クライアント側のコードはここにあります。 –

0

あなたはそれを実装するために多くのeasyerだろう(XMLシリアル化Bynaryないシリアライズを使用)SOAP Arhitectureを使用することができます。または、ピアツーピアチャットが必要な場合code here

+0

.netオブジェクトで作業しているのであれば、BinaryFormatterを使用することをお勧めします - マルチパッチサポートが必要な場合 - SOAPまたはXMLシリアル化 –

2

WCFを検討してください。

セキュリティ、異なるプロトコルなど、すべての通信の問題を一貫した高水準の観点から処理します。

これは、.Netでの通信のための標準であり、より古い低レベルの技術を網羅しています。私は私のコードビットを変更したWCF/WPF Chat Application

関連する問題