1
我真的很陌生,我正在開發一個基於TcpClient的應用程序。NetworkStream BeginRead/EndRead
我想知道如何使用BeginRead & EndRead,我已經閱讀過MSN文檔,但沒有幫助。
我有這樣的:
private void Send() { TcpClient _client = new TcpClient("host", 80); NetworkStream ns = _client.GetStream(); ns.Flush(); /... ns.Write(buffer, 0, buffer.Length); int BUFFER_SIZE = 1024; byte[] received = new byte[BUFFER_SIZE]; ns.BeginRead(received, 0, 0, new AsyncCallback(OnBeginRead), ns); } private void OnBeginRead(IAsyncResult ar) { NetworkStream ns = (NetworkStream)ar.AsyncState; int BUFFER_SIZE = 1024; byte[] received = new byte[BUFFER_SIZE]; string result = String.Empty; ns.EndRead(ar); int read; while (ns.DataAvailable) { read = ns.Read(received, 0, BUFFER_SIZE); result += Encoding.ASCII.GetString(received); received = new byte[BUFFER_SIZE]; } result = result.Trim(new char[] { '\0' }); // Want to update Form here with result }
如何使用結果我更新表單組件?
感謝您的幫助。
這是Winforms嗎? – CodingGorilla 2010-12-09 15:41:44
是的,它是WinForms。 – 2010-12-09 15:54:06