本文整理汇总了C#中NetMQ.Sockets.DealerSocket.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# DealerSocket.Connect方法的具体用法?C# DealerSocket.Connect怎么用?C# DealerSocket.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQ.Sockets.DealerSocket
的用法示例。
在下文中一共展示了DealerSocket.Connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindRandom
public void BindRandom()
{
using (var randomDealer = new DealerSocket())
using (var connectingDealer = new DealerSocket())
{
int port = randomDealer.BindRandomPort("tcp://*");
connectingDealer.Connect("tcp://127.0.0.1:" + port);
randomDealer.SendFrame("test");
Assert.AreEqual("test", connectingDealer.ReceiveFrameString());
}
}
示例2: CheckTrySendFails
public void CheckTrySendFails()
{
using (var dealer = new DealerSocket())
{
dealer.Options.SendHighWatermark = 1;
dealer.Options.Linger = TimeSpan.Zero;
dealer.Connect("tcp://127.0.0.1:55555");
Thread.Sleep(100);
var success = dealer.TrySendFrame("1");
Assert.IsTrue(success); // because the SendHighWatermark allows it into the buffers
success = dealer.TrySendFrame("2");
Assert.IsFalse(success);
}
}
示例3: CheckTrySendSucceeds
public void CheckTrySendSucceeds()
{
using (var router = new RouterSocket())
using (var dealer = new DealerSocket())
{
var port = router.BindRandomPort("tcp://127.0.0.1");
router.Options.Linger = TimeSpan.Zero;
dealer.Options.SendHighWatermark = 1;
dealer.Options.Linger = TimeSpan.Zero;
dealer.Connect("tcp://127.0.0.1:" + port);
Thread.Sleep(100);
Assert.IsTrue(dealer.TrySendFrame("1"));
}
}
示例4: CheckSendAgainException
public void CheckSendAgainException()
{
using (var router = new RouterSocket())
using (var dealer = new DealerSocket())
{
var port = router.BindRandomPort("tcp://127.0.0.1");
router.Options.Linger = TimeSpan.Zero;
dealer.Options.SendHighWatermark = 1;
dealer.Options.Linger = TimeSpan.Zero;
dealer.Connect("tcp://127.0.0.1:" + port);
#pragma warning disable 618
dealer.Send("1", dontWait: true, sendMore: false);
dealer.Send("2", dontWait: true, sendMore: false);
#pragma warning restore 618
}
}
示例5: TwoMessagesFromRouterToDealer
public void TwoMessagesFromRouterToDealer()
{
using (var server = new RouterSocket())
using (var client = new DealerSocket())
using (var poller = new NetMQPoller { client })
{
var port = server.BindRandomPort("tcp://*");
client.Connect("tcp://127.0.0.1:" + port);
var cnt = 0;
client.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine(str);
}
cnt++;
if (cnt == 2)
{
poller.Stop();
}
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
client.Options.Identity = clientId;
const string request = "GET /\r\n";
const string response = "HTTP/1.0 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Hello, World!";
client.SendFrame(request);
byte[] serverId = server.ReceiveFrameBytes();
Assert.AreEqual(request, server.ReceiveFrameString());
// two messages in a row, not frames
server.SendMoreFrame(serverId).SendFrame(response);
server.SendMoreFrame(serverId).SendFrame(response);
poller.Run();
}
}
示例6: BindToLocal
public void BindToLocal()
{
var validAliasesForLocalHost = new[] { "127.0.0.1", "localhost", Dns.GetHostName() };
foreach (var alias in validAliasesForLocalHost)
{
using (var localDealer = new DealerSocket())
using (var connectingDealer = new DealerSocket())
{
var port = localDealer.BindRandomPort("tcp://*");
connectingDealer.Connect(string.Format("tcp://{0}:{1}", alias, port));
localDealer.SendFrame("test");
Assert.AreEqual("test", connectingDealer.ReceiveFrameString());
Console.WriteLine(alias + " connected ");
}
}
}
示例7: Mandatory
public void Mandatory()
{
using (var router = new RouterSocket())
{
router.Options.RouterMandatory = true;
router.Bind("tcp://127.0.0.1:5555");
using (var dealer = new DealerSocket())
{
dealer.Options.Identity = Encoding.ASCII.GetBytes("1");
dealer.Connect("tcp://127.0.0.1:5555");
dealer.SendFrame("Hello");
Assert.AreEqual("1", router.ReceiveFrameString());
Assert.AreEqual("Hello", router.ReceiveFrameString());
}
Thread.Sleep(100);
Assert.Throws<HostUnreachableException>(() => router.SendMoreFrame("1").SendFrame("Hello"));
}
}
示例8: Handover
public void Handover()
{
using (var router = new RouterSocket())
using (var dealer1 = new DealerSocket())
{
router.Options.RouterHandover = true;
router.Bind("inproc://127.0.0.1:5555");
dealer1.Options.Identity = Encoding.ASCII.GetBytes("ID");
dealer1.Connect("inproc://127.0.0.1:5555");
dealer1.SendMoreFrame("Hello").SendFrame("World");
var identity = router.ReceiveFrameString();
Assert.AreEqual("ID", identity);
using (var dealer2 = new DealerSocket())
{
dealer2.Options.Identity = Encoding.ASCII.GetBytes("ID");
dealer2.Connect("inproc://127.0.0.1:5555");
// We have new peer which should take over, however we are still reading a message
var message = router.ReceiveFrameString();
Assert.AreEqual("Hello", message);
message = router.ReceiveFrameString();
Assert.AreEqual("World", message);
dealer2.SendMoreFrame("Hello").SendFrame("World");
identity = router.ReceiveFrameString();
Assert.AreEqual("ID", identity);
message = router.ReceiveFrameString();
Assert.AreEqual("Hello", message);
message = router.ReceiveFrameString();
Assert.AreEqual("World", message);
}
}
}
示例9: AddSocketDuringWork
public void AddSocketDuringWork()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var poller = new NetMQPoller { router1 })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
bool router1Arrived = false;
bool router2Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1.SkipFrame();
router1.SkipFrame();
router1Arrived = true;
poller.Add(router2);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2.SkipFrame();
router2.SkipFrame();
router2Arrived = true;
signal2.Set();
};
poller.RunAsync();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal2.WaitOne(300));
poller.Stop();
Assert.IsTrue(router1Arrived);
Assert.IsTrue(router2Arrived);
}
}
示例10: JoinTest
public void JoinTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.Join;
m.Join.Sequence = 123;
m.Join.Group = "Life is short but Now lasts for ever";
m.Join.Status = 123;
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Join));
Assert.That(m.Join.Sequence, Is.EqualTo(123));
Assert.That(m.Join.Group, Is.EqualTo("Life is short but Now lasts for ever"));
Assert.That(m.Join.Status, Is.EqualTo(123));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
server.Bind("inproc://zprototestJoin");
client.Connect("inproc://zprototestJoin");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}
示例11: LeaveTest
public void LeaveTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.Leave;
m.Leave.Sequence = 123;
m.Leave.Group = "Life is short but Now lasts for ever";
m.Leave.Status = 123;
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Leave));
Assert.That(m.Leave.Sequence, Is.EqualTo(123));
Assert.That(m.Leave.Group, Is.EqualTo("Life is short but Now lasts for ever"));
Assert.That(m.Leave.Status, Is.EqualTo(123));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
// Cannot bind address ( inproc://zprototest ) - already in use.
// But only when I run all these tests at the same time.
server.Bind("inproc://zprototestLeave");
client.Connect("inproc://zprototestLeave");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}
示例12: AddSocketDuringWork
public void AddSocketDuringWork()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var poller = new Poller(router1) { PollTimeout = TestPollTimeoutMillis })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
bool router1Arrived = false;
bool router2Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1.SkipFrame();
router1.SkipFrame();
router1Arrived = true;
poller.AddSocket(router2);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2.SkipFrame();
router2.SkipFrame();
router2Arrived = true;
signal2.Set();
};
poller.PollTillCancelledNonBlocking();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal1.WaitOne(300));
poller.CancelAndJoin();
Assert.IsTrue(router1Arrived);
Assert.IsTrue(router2Arrived);
}
}
示例13: Ipv6ToIpv6
public void Ipv6ToIpv6()
{
using (var localDealer = new DealerSocket())
using (var connectingDealer = new DealerSocket())
{
localDealer.Options.IPv4Only = false;
var port = localDealer.BindRandomPort("tcp://*");
connectingDealer.Options.IPv4Only = false;
connectingDealer.Connect(string.Format("tcp://{0}:{1}", IPAddress.IPv6Loopback, port));
connectingDealer.SendFrame("test");
Assert.AreEqual("test", localDealer.ReceiveFrameString());
}
}
示例14: Router_Dealer_Demonstrating_Messages_From_Publisher_To_Subscribers
public void Router_Dealer_Demonstrating_Messages_From_Publisher_To_Subscribers()
{
// NOTES
// 1. Use ThreadLocal<DealerSocket> where each thread has
// its own client DealerSocket to talk to server
// 2. Each thread can send using it own socket
// 3. Each thread socket is added to poller
const int delay = 500; // millis
var clientSocketPerThread = new ThreadLocal<DealerSocket>();
string endpoint;
using (var server = new RouterSocket("@tcp://127.0.0.1:0")) // If we specify 0, it will choose a random port for us.
{
endpoint = server.Options.LastEndpoint; // Lets us know which port was chosen.
Console.Write("Last endpoint, including port: {0}\n", server.Options.LastEndpoint);
using (var poller = new NetMQPoller())
{
// Start some threads, each with its own DealerSocket
// to talk to the server socket. Creates lots of sockets,
// but no nasty race conditions no shared state, each
// thread has its own socket, happy days.
for (int i = 0; i < 4; i++)
{
Task.Factory.StartNew(state =>
{
DealerSocket client = null;
if (!clientSocketPerThread.IsValueCreated)
{
client = new DealerSocket();
client.Options.Identity =
Encoding.Unicode.GetBytes(state.ToString());
client.Connect(endpoint);
//client.ReceiveReady += Client_ReceiveReady;
clientSocketPerThread.Value = client;
poller.Add(client);
}
else
{
client = clientSocketPerThread.Value;
}
Thread.Sleep(3000); // Wait until server is up.
client.SendFrame("Ping");
while (true)
{
Console.Write("Client {0}: Waiting for ping...\n", i);
// Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it
// sends at least one message which makes it necessary to open the connection. I believe this is a
// low-level feature of the TCP/IP transport.
var clientMessage = client.ReceiveMultipartMessage();
Console.WriteLine("======================================");
Console.WriteLine(" INCOMING CLIENT MESSAGE FROM SERVER");
Console.WriteLine("======================================");
PrintFrames("Server receiving", clientMessage);
}
},
string.Format("client {0}", i),
TaskCreationOptions.LongRunning);
}
// start the poller
poller.RunAsync();
// server loop
int sequenceNo = 0;
for (int i=0;i<10;i++)
{
NetMQMessage messageToServer = new NetMQMessage();
messageToServer.AppendEmptyFrame();
messageToServer.Append(sequenceNo.ToString());
sequenceNo++;
Console.WriteLine("======================================");
Console.WriteLine(" OUTGOING MESSAGE {0} TO CLIENTS ", sequenceNo);
Console.WriteLine("======================================");
PrintFrames("Client Sending", messageToServer);
server.SendMultipartMessage(messageToServer);
Thread.Sleep(delay);
}
Console.WriteLine("Finished.");
}
}
}
示例15: Messages_From_Router_To_Dealer_With_Subscription
public void Messages_From_Router_To_Dealer_With_Subscription()
{
Console.Write("Test sending message from publisher(router) to subscribers (dealer).\n");
int maxMessage = 5;
CountdownEvent cd = new CountdownEvent(maxMessage);
string endpoint;
using (var publisher = new RouterSocket())
using (var subscriber = new DealerSocket())
using (var poller = new NetMQPoller { subscriber })
{
publisher.Bind("tcp://127.0.0.1:0");
endpoint = publisher.Options.LastEndpoint;
subscriber.Connect(endpoint);
subscriber.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine("Subscribe: " + str);
}
cd.Signal();
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientIdTheIsLongerThen32BytesForSureAbsolutelySure");
subscriber.Options.Identity = clientId;
const string request = "Ping";
// Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it
// sends at least one message which makes it necessary to open the connection. I believe this is a
// low-level feature of the TCP/IP transport.
subscriber.SendFrame(request); // Ping.
byte[] serverId = publisher.ReceiveFrameBytes();
//Assert.AreEqual(request, publisher.ReceiveFrameString());
for (int i = 0; i < maxMessage; i++)
{
string msg = string.Format("[message: {0}]", i);
Console.Write("Publish: {0}\n", msg);
publisher.SendMoreFrame(serverId).SendFrame(msg);
//publisher.SendMoreFrame("").SendFrame(msg);
}
poller.RunAsync();
if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
{
Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
}
}
}