Windows Phone平台的TCP客户端开发包 SocketEx.TcpClient
openkk
12年前
SocketEx.TcpClient 是 Windows Phone 平台上的一个 TCP 客户端开发包。方便开发人员轻松实现Socket编程。SocketEx.TcpClient采用同步阻塞模式,这就意味着您如果直接从UI线程使用TcpClien,UI的更新将会被阻止。
连接服务器代码:
var serverAddress = "www.google.fi"; var serverPort = 80; var connection = new TcpClient(serverAddress, serverPort);从服务器接收消息:
var connection = CreateConnection(); var stream = connection.GetStream(); var reader = new StreamReader(stream); string message; while ((message = reader.ReadLine()) != null) { Debug.WriteLine(message); }发送消息到服务器:
var connection = CreateConnection(); var stream = connection.GetStream(); using (var writer = new StreamWriter(stream)) { var request = "GET / HTTP/1.1\r\nHost: " + serverAddress + "\r\nConnection: Close\r\n\r\n"; writer.WriteLine(request); }