2011-05-31 22 views
2

我正在處理一般服務器和客戶端程序。我面臨的問題是當我在我的客戶端和服務器的OnDataReceived中時,我不知道如何處理數據。理想情況下,它應該將接收到的數據輸出到窗口中,但我不知道它是否是Form或Console應用程序。所以問題是我如何創建一個可以處理兩者的通用方法,或者如果這是不可能的,我應該怎麼做?試圖創建一個可寫入控制檯或表單應用程序的方法

我正在使用的代碼:

  SocketPacket theSockId = (SocketPacket)asyn.AsyncState; 

      int iRx = theSockId.m_currentSocket.EndReceive(asyn); 
      char[] chars = new char[iRx + 1]; 
      Decoder decode = Encoding.Default.GetDecoder(); 
      int charLength = decode.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); 
      String szData = new String(chars); 

      //Handle Message here 

      WaitForData(); 

和插座包類:

class SocketPacket 
    { 
    public Socket m_currentSocket; 
    public byte[] dataBuffer = new byte[1024];//Buffer to store the data by the client 

    public SocketPacket(Socket socket) 
    { 
     m_currentSocket = socket; 
    } 
    } 

PS。 我不知道是否需要知道,但我正在做異步客戶端/服務器。

+0

你想創建一個程序來執行一個特定的功能,或者一個可重用的庫嗎?不確定'一般服務器和客戶端程序'是什麼意思。 – mdm 2011-05-31 15:37:40

+0

你可以看看MVP模式。演示者將只使用視圖的方法來顯示數據,視圖將關注是否顯示在控制檯或表單或html標籤中。 – 2011-05-31 15:39:24

回答

3

你的溝通類不應該關心他們收到的數據會發生什麼。相反,他們應該將數據提供給需要它的課程。一種方法是提供一個getData()方法,它接收數據然後將其返回給調用者。更好的辦法是提供一個DataArrived事件,當你收到數據時就會觸發它。這樣,任何數量的消費者都可以收聽數據,但是您的通信代碼不需要知道哪些類正在收聽,或者他們計劃如何處理數據。

編輯

一個簡單的例子:

public class MyClassWithEvent 
{ 
    public delegate void DataArrivedDelegate(string data); 
    public event DataArrivedDelegate DataArrived; 

    public void GetSomeData() 
    { 
     // Communication code goes here; stringData has the data 

     DataArrivedDelegate handler = DataArrived; 
     if (handler != null) 
     { 
      // If you want to raise the event on this thread, this is fine 
      handler(stringData); 
     } 
    } 
} 

在監聽器類:

public MyListener 
{ 

    public MyListener(MyServer server) 
    { 
     // Sets MyListenerMethod to be called when DataArrived is raised 
     server.DataArrived += MyListenerMethod; 
    } 

    public void MyListenerMethod(string data) 
    { 
     // Do something with the data 
     Console.WriteLine(data); 
    } 
} 
+0

好的,我不確定我將如何創建這樣的事件。不要以爲我使用過(自定義)事件,所以任何(模擬)例子都會很棒。 – Siemsen 2011-05-31 16:08:43

+0

我不太清楚這是如何工作的,也就是說,這會成爲我的客戶端和服務器類的一部分嗎?如果是的話,GetSomeData將被OnDataReceived調用?這可能是我完全誤解了這一點。 – Siemsen 2011-05-31 16:26:16

+0

上面的類可能是(或兩者)客戶端和服務器類。您只需像往常一樣查看通信代碼,然後當您收到數據時,就會引發事件(如上所示),以便聆聽者可以對數據執行某些操作。我將更新示例以顯示一位聽衆。 – dlev 2011-05-31 16:32:58

1

我會創建一個接口,我可以寫入事件信息,如IEventSink。因此,OnDataReceived與IEventSink的一個實例一起工作,並在其上調用Write方法。 然後我將有2個接口的實現:一個寫在控制檯上,一個寫在窗體上。

1

你可以嘗試實施戰略

interface IOutputStrategy 
{ 
    void Output(string message); 
} 

class ConsoleOutput:IOutputStrategy 
{ 
    public void Output(string message) 
    { 
     Console.Writeline(message); 
    } 
} 

class FormOutput:IOutputStrategy 
{ 
    public void Output(string message) 
    { 
     // output where you want 
    } 
} 

和服務器/客戶端,您將媒體資源相關聯型IOutputStrategy

//Server 
IOutputStrategy instance = new ConsoleOutput(); 

//Client 
IOutputStrategy instance = new FormOutput(); 

然後在OnDataReceived回調,你可以使用目前的IOutputStrategy實例輸出消息

instance.Output(szData); 

希望它可以幫助

2

火災事件用字符串?如果數據緩衝區字符串是套接字對象的成員,那麼我會提供幫助 - 那麼您可以使用套接字對象&來觸發事件處理程序,無論結果如何,都可能需要決定如何處理用數據做。

RGDS, 馬丁

+0

好吧,我應該包括SocketPacket類(我現在有),確實有DataBuffer。但是,你能否舉一個例子來說明事件處理程序的外觀以及我如何創建一個事件。從未使用過(據我所知)。 – Siemsen 2011-05-31 15:55:38

0

這是依賴注入的好位置。

  1. 創建一個界面,有一個名爲「ShowTheResult」
  2. 現在的字符串屬性,在任何你想要的目的是能夠顯示結果(形式,控制檯應用程序(我們叫它ISiemsen現在) ,web表單...)實現ISiemsen接口
  3. 在爲「ShowTheResult」設置的屬性內,可以將值分配給滿足該對象需要的任何值。 (如果它是一個控制檯,執行一個console.WriteLine(值),如果它是一個表單,則將該值賦給一個文本框)
  4. 更改您的工作進程以接受對工作函數的引用並讓它發送反應何時需要去。

    protected void getResults(ISiemsen siemens ...) 
    { 
        SocketPacket theSockId = (SocketPacket)asyn.AsyncState; 
        int iRx = theSockId.m_currentSocket.EndReceive(asyn); 
        char[] chars = new char[iRx + 1]; 
        Decoder decode = Encoding.Default.GetDecoder(); 
        int charLength = decode.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); 
        String szData = new String(chars); 
    
    
        WaitForData(); 
    //Handle Message here 
        siemsen.ShowTheResult(theResult); 
    } 
    

使用這種技術的UI元素不知道或不關心其中的數據來源和工作進程不知道或不關心的地方去......這是「鬆散耦合」。

相關問題