2011-12-22 30 views
0

我想投IAsyncResult.AsyncState類StateObject。但是由於AsyncState是Socket類型,所以它給出了一個轉換錯誤。我需要從結果中獲取字節數據。我剛開始一個服務器項目,我不熟悉這一點。無法施展IAsyncResult.AsyncState

public class StateObject 
    { 
     // Client socket. 
     public Socket workSocket = null; 
     // Size of receive buffer. 
     public const int BufferSize = 256; 
     // Receive buffer. 
     public byte[] buffer = new byte[BufferSize]; 
     // Received data string. 
     public StringBuilder sb = new StringBuilder(); 
    } 

這是預先OnReceiveHandlerClass

private void Receive(IAsyncResult result) 
    { 

     StateObject ss = (StateObject)result.AsyncState; 
     ...... 
     } 

謝謝..

+0

朋友的問題是固定的... :-) – Sunil 2011-12-22 04:52:13

+1

後的修復作爲一個答案,並接受它,所以程序員的後代可能難以倖免同樣的命運。 :) – Amy 2011-12-22 04:57:04

+0

實際上這是我的錯誤,因爲我甚至不知道基礎知識。無論如何,我會發布錯誤。你說它可能有助於某人.... :-) – Sunil 2012-01-06 09:30:33

回答

0

我正在該錯誤,因爲我在BeginReceive方法的情況下,發送發送StateObject

的一個不同的對象
Correct method: 
Socket.BeginReceive(StateObject.DataBuffer, 0, (int)StateObject.DataLength, 
         SocketFlags.None, new AsyncCallback(this.Receive),StateObject); 

最後一個參數應該是類型StateObje ct和我發送一個對象,因爲我不知道該發送什麼參數。

相關問題