本文整理汇总了Java中org.zeromq.ZMQ.Socket.bindToRandomPort方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.bindToRandomPort方法的具体用法?Java Socket.bindToRandomPort怎么用?Java Socket.bindToRandomPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.zeromq.ZMQ.Socket
的用法示例。
在下文中一共展示了Socket.bindToRandomPort方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newAgent
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
protected static Agent newAgent (ZContext ctx, Socket pipe)
{
Socket inbox = ctx.createSocket (ZMQ.ROUTER);
if (inbox == null) // Interrupted
return null;
ZreUdp udp = new ZreUdp (PING_PORT_NUMBER);
int port = inbox.bindToRandomPort ("tcp://*", 0xc000, 0xffff);
if (port < 0) { // Interrupted
System.err.println ("Failed to bind a random port");
udp.destroy ();
return null;
}
return new Agent (ctx, pipe, inbox, udp, port);
}
示例2: main
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main (String[] args)
{
ZContext ctx = new ZContext ();
// Use the Zyre UDP class to make sure we listen on the same
// network interface as our peers
ZreUdp udp = new ZreUdp (ZreInterface.PING_PORT_NUMBER);
String host = udp.host ();
Socket collector = ctx.createSocket (ZMQ.SUB);
// Bind to an ephemeral port
int port = collector.bindToRandomPort (String.format ("tcp://%s", host));
// Announce this to all peers we connect to
ZreInterface inf = new ZreInterface ();
inf.setHeader ("X-ZRELOG", "tcp://%s:%d", host, port);
// Get all log messages (don't filter)
collector.subscribe ("".getBytes ());
Poller items = ctx.getContext ().poller ();
items.register (collector, Poller.POLLIN);
items.register (inf.handle (), Poller.POLLIN);
while (!Thread.currentThread ().isInterrupted ()) {
if (items.poll (1000) == -1)
break; // Interrupted
// Handle input on collector
if (items.pollin (0))
printLogMsg (collector);
// Handle event from interface (ignore it)
if (items.pollin (1)) {
ZMsg msg = inf.recv ();
if (msg == null)
break; // Interrupted
msg.destroy ();
}
}
inf.destroy ();
udp.destroy ();
ctx.destroy ();
}