我在https://netmq.readthedocs.io/上采用了一个简单的接收/请求套接字示例,并希望使其在无限循环中与 parametrizedThread 一起工作。该代码在几个循环中运行良好,之后它会抛出
无法立即完成非阻塞套接字操作
对于我得到的上述内容,应该在第一个循环之后立即发生,而不是随机发生。这里有什么问题?听起来必须清除某些东西才能再次获得干净的连接(不确定)。
class Program
{
public class Connector
{
public String connection { get; set; }
public ResponseSocket server { get; set; }
public Connector(string address, ResponseSocket server_)
{
this.connection = address;
this.server = server_;
}
}
static void Main(string[] args)
{
string connection = "tcp://localhost:5555";
using (var server = new ResponseSocket())
{
while (true)
{
try
{
server.Bind(connection);
}
catch (NetMQException e)
{
Console.WriteLine(e.ErrorCode);
}
Connector c = new Connector(connection, server);
ParameterizedThreadStart parametrizedClientThread = new ParameterizedThreadStart(runClientSide);
Thread t = new Thread(parametrizedClientThread);
t.Start(c);
//runClientSide(connection, server);
}
}
}
private static void runClientSide(object param)
{
Connector conn = (Connector)param;
string connection = conn.connection;
ResponseSocket server = conn.server;
using (var client = new RequestSocket())
{
client.Connect(connection);
client.SendFrame("Hello");
string fromClientMessage = server.ReceiveFrameString();
Console.WriteLine("From Client: {0}", fromClientMessage);
server.SendFrame("Hi Back");
string fromServerMessage = client.ReceiveFrameString();
Console.WriteLine("From Server: {0}", fromServerMessage);
//Console.ReadLine();
}
}