加载中
TcpClient is a class in .NET Framework which “provides simple methods for connecting, sending, and receiving stream data over a network”. TcpClient hides the details of working with sockets and it’s a simple way to open connections and work with TCP. Unfortunately, TcpClient is not available in Windows Phone version of .NET Framework.

TcpClient 是一个能通过网络接收或发送数据的.net类,它将 sockets底层复杂的工作细节隐藏起来,让开发者更加简单的利用Tcp套接字来开发程序。但悲剧的是在Windows Phone专用的.net framework下是无法使用它的。

SocketEx.TcpClient – Unofficial, only little tested TcpClient for Windows Phone

SocketEx.TcpClient is a MIT-licensed TcpClient for Windows Phone which aims to make working with Windows Phone sockets easy. Compared to the TcpClient in full .NET Framework, SocketEx.TcpClient isn’t 100% compatible and some of the features aren’t implemented at all.

Please note that the library works in a synchronous blocking mode. This means that if you use the TcpClient directly from the UI-thread, you will block the UI from updating.

The library hasn’t gone through an exhaustive testing so there may be issues. The code is based on the “Crystalbyte Networking for Silverlight” project, available from the CodePlex. Almost all of the code is from that neat library, but I adjusted it a little to get it working with Windows Phone and fixed out some threading issues.

SocketEx.TcpClient -- 一个让Windows Phone能用的非官方没有测试保证的TcpClient
SocketEx.TcpClient 是一个致力于让Windows Phone也能用上sockets的类库,基于MIT授权。当然与完全版.net framework的TcpClient肯定是没法比的,无论是功能多少还是稳定性方面。

忘了说,这个类库是完全运行在同步模式上的(这一点与WP7的标准开发模式恰好相反)。这也就意味着使用它会 阻止前端UI的运行。而且本类库也没经过大量的测试,这些代码是基于codeplex上的“ Crystalbyte Networking for Silverlight” 项目的。

The usage

With SocketEx.TcpClient you don’t work with the low-level Socket and SocketAsyncEventArgs-classes. Instead you create a new TcpClient and then operate its stream using either a StreamReader or a StreamWriter. This is easier than it sounds.

I’ve previously written about how to operate with the built-in Socket and SocketAsyncEventArgs classes.

Now, let’s do those same examples with SocketEx.TcpClient.

SocketEx.TcpClient – How To Open a Connection

We can open the connection by passing the server address and server port as parameters to TcpClient.

var serverAddress = "www.google.fi";
var serverPort = 80;
 
var connection = new TcpClient(serverAddress, serverPort);

如何使用:
通过SocketEx.TcpClient你完全不需要涉及那些很底层的Socket以及SocketAsyncEventArgs类。你只需要创建一个TcpClient然后通过StreamReader或者StreamWriter来操作它的流。
这里要谈到我以前写过的几篇文章谈到如何操作内建的Socket以及SocketAsyncEventArgs类。
好了,让我们开始吧。
SocketEx.TcpClient -- 如何打开连接
这里只需要传递服务器地址以及服务器端口给TcpClient作为参数就行了。
var serverAddress = "www.google.fi";
var serverPort = 80;
 
var connection = new TcpClient(serverAddress, serverPort);


SocketEx.TcpClient – How To Receive a Message

To read a message we need a StreamReader.

var connection = CreateConnection();
var stream = connection.GetStream();
 
var reader = new StreamReader(stream);
 
string message;
while ((message = reader.ReadLine()) != null)
{
    Debug.WriteLine(message);
}

SocketEx.TcpClient -- 如何接受消息
这里使用StreamReader来接受消息。
var connection = CreateConnection();
var stream = connection.GetStream();
 
var reader = new StreamReader(stream);
 
string message;
while ((message = reader.ReadLine()) != null)
{
    Debug.WriteLine(message);
}


SocketEx.TcpClient – How To Send a Message

To write a message we need a StreamWriter.

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);
}

SocketEx.TcpClient -- 如何发送信息
这里需要StreamWriter来发送消息
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);
}

返回顶部
顶部