如何為 Windows Phone 建立和使用 TCP 通訊端用戶端應用程式【msdn】

來源:互聯網
上載者:User

1.串連到 TCP 通訊端伺服器

使用 System.Net.Sockets API 建立一個通訊端並串連到伺服器。為了清楚起見,System.Net.Sockets API 的調用被封裝在 SocketClient 類中。

定義頭(使用 _socket 變數儲存 Socket 對象。_clientDone 變數是一個 ManualResetEvent,用於協調通過Sockets API 呼叫的非同步呼叫):

// Cached Socket object that will be used by each call for the lifetime of this class        Socket _socket = null;        // Signaling object used to notify when an asynchronous operation is completed        static ManualResetEvent _clientDone = new ManualResetEvent(false);        // Define a timeout in milliseconds for each asynchronous call. If a response is not received within this         // timeout period, the call is aborted.        const int TIMEOUT_MILLISECONDS = 5000;        // The maximum size of the data buffer to use with the asynchronous socket methods        const int MAX_BUFFER_SIZE = 2048;

在 SocketClient.cs 中,添加以下方法,該方法將建立 TCP 通訊端,並向伺服器發送非同步串連請求。此操作的響應將由內聯回調進行處理

  public string Connect(string hostName, int portNumber)        {            string result = string.Empty;            DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                        // Create a SocketAsyncEventArgs object to be used in the connection request            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();            socketEventArg.RemoteEndPoint = hostEntry;            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)            {                result = e.SocketError.ToString();                _clientDone.Set();            });            _clientDone.Reset();            _socket.ConnectAsync(socketEventArg);
_clientDone.WaitOne(TIMEOUT_MILLISECONDS); return result; }

 

2.向 TCP 通訊端伺服器發送資料

public string Send(string data)        {            string response = "Operation Timeout";            // We are re-using the _socket object that was initialized in the Connect method            if (_socket != null)            {                // Create SocketAsyncEventArgs context object                SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();                // Set properties on context object                socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;                socketEventArg.UserToken = null;                // Inline event handler for the Completed event.                // Note: This event handler was implemented inline in order to make this method self-contained.                socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)                {                    response = e.SocketError.ToString();                    // Unblock the UI thread                    _clientDone.Set();                });                // Add the data to be sent into the buffer                byte[] payload = Encoding.UTF8.GetBytes(data);                socketEventArg.SetBuffer(payload, 0, payload.Length);                // Sets the state of the event to nonsignaled, causing threads to block                _clientDone.Reset();                // Make an asynchronous Send request over the socket                _socket.SendAsync(socketEventArg);                // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.                // If no response comes back within this time then proceed                _clientDone.WaitOne(TIMEOUT_MILLISECONDS);            }            else            {                response = "Socket is not initialized";            }            return response;        }

3.接收來自 TCP 通訊端伺服器的資料

   public string Receive()        {            string response = "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<SocketAsyncEventArgs>(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 milliseconds.                // If no response comes back within this time then proceed                _clientDone.WaitOne(TIMEOUT_MILLISECONDS);            }            else            {                response = "Socket is not initialized";            }            return response;        }        /// <summary>        /// Closes the Socket connection and releases all associated resources        /// </summary>        public void Close()        {            if (_socket != null)            {                _socket.Close();            }        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.