37 lines
1006 B
C#
37 lines
1006 B
C#
|
using System;
|
|||
|
using System.IO;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Windows.Networking;
|
|||
|
using Windows.Networking.Sockets;
|
|||
|
|
|||
|
namespace Plane.Communication
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 提供 TCP 通信的能力。
|
|||
|
/// </summary>
|
|||
|
public class TcpConnection : StreamSocketConnection
|
|||
|
{
|
|||
|
private HostName _remoteHostName;
|
|||
|
private string _remoteServiceName;
|
|||
|
|
|||
|
public TcpConnection(string remoteHostName, int remotePort = 5250)
|
|||
|
{
|
|||
|
_remoteHostName = new HostName(remoteHostName);
|
|||
|
_remoteServiceName = remotePort.ToString();
|
|||
|
}
|
|||
|
|
|||
|
public override async Task OpenAsync()
|
|||
|
{
|
|||
|
if (IsOpen)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
_socket = new StreamSocket();
|
|||
|
await _socket.ConnectAsync(_remoteHostName, _remoteServiceName);
|
|||
|
_inputStream = _socket.InputStream;
|
|||
|
_outputStream = _socket.OutputStream.AsStreamForWrite();
|
|||
|
IsOpen = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|