Netframework v4套接字中的BeginRead方法的等价物是什么

时间:2013-03-06 00:03:24

标签: windows-phone-8

在netframework v2中,套接字使用Invoke(BeginRead)方法调用方法(示例ReceiveMsg),如下所示:

client = new TcpClient();
client.Connect(SERVERIP,PORTNO);
data = new byte [client.ReceiveBufferSize];
SendMessage消息( “你好\ n”);

client.GetStream()。BeginRead(data,0,System.Convert.ToInt32(client.ReceiveBufferSize),ReceiveMsg,null);

  1. 此方法的ReceiveMsg套接字是否始终处于“自动模式”,以便它将等待接收SocketServer广播的消息?

  2. 或正在使用System.Threading.Thread.Sleep()使ReceiveMsg方法始终处于活动状态,以便它在SocketServer广播消息时处于就绪模式以响应?

  3. 如何在Netframework v4或4.5套接字中为此ReceiveMsg方法执行此操作,因为不再需要BeginRead()。

  4. 感谢。

    以下是netframework v4中socket的代码:http://msdn.microsoft.com/en-us/library/hh202858(v=vs.92).aspx(对于WP mango / Wp8)

    Client.Connect();   Client.Send();   Client.Receive();

     public string Receive()
            {
    
                //-- receive the reply from server 
    
               string response = "Receiving Operation Timeout";
    
                // We are receiving over an established socket connection
                if (_socket != null)
                {
                    // Create SocketAsyncEventArgs context object
                    SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                    socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
    
                    // Setup the buffer to receive the data
                    socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
    
                    // Inline event handler for the Completed event.
                    // Note: This even handler was implemented inline in order to make this method self-contained.
                    socketEventArg.Completed += new EventHandler(delegate(object s, SocketAsyncEventArgs e)
                    {
                        if (e.SocketError == SocketError.Success)
                        {
                            // Retrieve the data from the buffer
    
                            response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
                            response = response.Trim('\0');
                        }
                        else
                        {
                            response = e.SocketError.ToString();
                        }
    
                        _clientDone.Set();
                    });
    
                    // Sets the state of the event to nonsignaled, causing threads to block
                    _clientDone.Reset();
    
                    // Make an asynchronous Receive request over the socket
                    _socket.ReceiveAsync(socketEventArg);
    
                    // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS seconds.
                    // If no response comes back within this time then proceed
                    _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
                }
                else
                {
                    response = "Socket is not initialized";
                }
    
                return response;
            }
    
    

0 个答案:

没有答案