我必须使用仅暴露同步套接字接口的套接字库。我有几个问题将此同步转换为异步。我从msdn示例开始(库接口类似于microsoft socket库,除了库使用不同的通信算法)。
这是我目前的进展
private async void Start_Click(object sender, RoutedEventArgs e)
{
Log("Start server");
var port = int.Parse(Port.Text);
var task = Task.Run(() =>
{
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
Log("Hostname " + Dns.GetHostName());
IPAddress[] ipv4Addresses = Array.FindAll(
Dns.GetHostEntry(string.Empty).AddressList,
a => a.AddressFamily == AddressFamily.InterNetwork);
IPAddress[] ipv6Addresses = Array.FindAll(
Dns.GetHostEntry(Dns.GetHostName()).AddressList,
a => a.AddressFamily == AddressFamily.InterNetworkV6);
foreach (var ip in ipv4Addresses)
{
Log("Ipv4 list " + ip);
}
foreach (var ip in ipv6Addresses)
{
Log("Ipv6 list " + ip);
}
var localEndPoint = new IPEndPoint(IPAddress.Any, port);
Log("Ip " + IPAddress.Any);
Log("Port " + port);
// Create a TCP/IP socket.
var listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen((int)SocketOptionName.MaxConnections);
Log("Max connection " + (int)SocketOptionName.MaxConnections);
// Start listening for connections.
bool isContinue = true;
while (isContinue)
{
Log("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
var handler = listener.Accept();
// handle connection
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
string data;
Log("Connection accepted");
Log("Local endpoint " + handler.LocalEndPoint);
Log("Remote endpoint " + handler.RemoteEndPoint);
data = null;
// An incoming connection needs to be processed.
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
}
// Show the data on the console.
Log("Text received " + data);
// Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
Log("Message sent");
handler.Shutdown(SocketShutdown.Both);
Log("Shutdown");
handler.Close();
Log("Close");
isContinue = true;
}
}
catch (Exception ex)
{
Log(ex.ToString());
}
});
await task;
}
我正在考虑使用Task,async,等待,但我似乎无法在我脑海中构建它。
我想通过添加socket来改进这个库。
感谢。
答案 0 :(得分:3)
如果库仅公开同步实现,则意味着它正在执行阻塞I / O.由于您无法更改它,因为它是库实现的一部分,因此您必须使用ThreadPool线程来委派工作。我写了几篇博文,其中提供了相关内容(1,2)。
异步处理此工作只会在您想要特别避免阻塞特定线程(例如UI线程)时出现问题。在这种情况下,您可以简单地执行对套接字库的调用:
var task = Task.Run(() =>
{
// run socket code
});
var result = await task;
// rest of your code
对Task.Run
队列的调用对ThreadPool起作用,等待避免在单独的线程中执行该工作时阻止UI线程。