什么是正确的方式来等待连接? [英] What is the proper way to wait for connections?

查看:411
本文介绍了什么是正确的方式来等待连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个简单的消息使用NetMQ(下面我想达到什么是一个稍微更详尽的描述)。结果
位试验和错误我后两个应用程序之间传递已经发现我不能只是发送或接收邮件马上a连接/绑定来电之后,因为它们是非阻塞,实际上即使连接尚未建立回报。结果
。对于现在的我与Thread.sleep()方法解决了这个,但是这有一个不好的味道吧,绝对无去生产系统。

I am trying to implement a simple message passing between two applications using NetMQ (a slightly more elaborate description of what I am trying to achieve is below).
After a bit of trial and error I've found that I can't just send or receive messages right away after a Connect/Bind calls, since they are non blocking and actually return even if the connection hasn't been established yet.
For now I solved this with Thread.Sleep(), but this has a bad taste to it and definitely a no-go for a production system.

所以,问题是,怎样的One应该做它NetMQ / ZeroMQ

So the question is, how's one supposed to do it in NetMQ/ZeroMQ?

客户端呢?例如:

        using (NetMQContext ctx = NetMQContext.Create())
        {
            using (var client = ctx.CreatePushSocket())
            {
                client.Connect("tcp://127.0.0.1:5555");
                Thread.Sleep(100); // wait for connection

                for (int i = 0; i < 5; i++) 
                {
                    client.Send("test " + i , true);
                }
            }
        }
    }



服务器例如:

Server example:

    using (NetMQContext ctx = NetMQContext.Create())
    {
        using (var server = ctx.CreatePullSocket())
        {
            server.Bind("tcp://127.0.0.1:5555");
            Thread.Sleep(100); // wait for connection
            while (true)
            {
                var str = server.ReceiveString();
                Console.Out.WriteLine(str);
                Thread.Sleep(60*1000); // do msg processing
            }
        }
    }



说明什么,我想实现:

Description of what I am trying to achieve:

客户的 - 将消息发送到一台服务器。客户不应该阻止,当服务器不可用不应该丢弃消息。客户可以随时联机离线/。

Client - Sends messages to a single server. The client should not block and should not discard messages when server is not available. The client can come offline/online at any time.

服务器的 - 从一个单一的客户端接收邮件。服务器阻塞,直到消息被接收。服务器需要做消息的冗长的加工和处理,而不应丢失任何其他消息。服务器可以在任何时间来离线/在线。

Server - Receives messages from a single client. The server blocks until a message is received. Server needs to do lengthy processing of the message and should not loose any other messages while processing. The server can come offline/online at any time.

推荐答案

你的睡眠的最佳解决方案,在服务器端是创建一个插座轮询和poll拉插座直到消息上被接收。这样就避免了浪费睡觉,并为一般更紧凑的代码。

The best solution to your sleep on the server side is to create a socket poller and poll on the pull socket until a message is received. This avoids wasteful sleeps, and makes for generally tighter code.

在客户端的最佳解决方案可能是创建两个插座(一个用于发送邮件,一个用于接收) ,并有服务器宣布其存在准备好客户端发送消息。由于ZeroMQ是在处理多个连接效率,该解决方案将很好地工作。

On the client side the best solution is probably to create two sockets (one for sending messages, one for receiving), and have the server announce its presence ready for the client to send the message. Because ZeroMQ is efficient at handling multiple connections, this solution will work very well.

这篇关于什么是正确的方式来等待连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆