Skip to content

Instantly share code, notes, and snippets.

@robertpi robertpi/hwclient.cs
Created Jul 24, 2015

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
public static void Main(string[] args)
{
//
// Hello World client
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} HWClient [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Endpoint Where HWClient should connect to.");
Console.WriteLine(" Default is tcp://127.0.0.1:5555");
Console.WriteLine();
args = new string[] { "tcp://127.0.0.1:5555" };
}
string endpoint = args[0];
// Create
using (var context = new ZContext())
using (var requester = new ZSocket(context, ZSocketType.REQ))
{
requester.CurveServer = true;
requester.CurveServerKey = Encoding.ASCII.GetBytes(Z85.Decode(@"rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7"));
//requester.CurveServerKey = Encoding.ASCII.GetBytes(Z85.Decode("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
requester.CurveSecretKey = Encoding.ASCII.GetBytes(Z85.Decode("D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs"));
requester.CurvePublicKey = Encoding.ASCII.GetBytes(Z85.Decode("Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID"));
// Connect
requester.Connect(endpoint);
for (int n = 0; n < 10; ++n)
{
string requestText = "Hello";
Console.Write("Sending {0}...", requestText);
// Send
requester.Send(new ZFrame(requestText));
// Receive
using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine(" Received: {0} {1}!", requestText, reply.ReadString());
}
}
}
Console.WriteLine("Done.");
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
private static void ZapLoop(ZContext context)
{
using (var socket = ZSocket.Create(context, ZSocketType.REP))
{
socket.ReceiveTimeout = TimeSpan.FromSeconds(1);
socket.Bind("inproc://zeromq.zap.01");
while (true)
{
ZError error;
using (ZFrame msg = socket.ReceiveFrame(ZSocketFlags.None, out error))
{
// ZError.EAGAIN occurs when socket timeout occurs.
if (msg == null && error.Number == ZError.EAGAIN)
{
return;
}
// if the error code isn't empty throw an exception
if (error != null)
{
throw new Exception(error.ToString());
}
Console.WriteLine(msg.ReadString());
}
}
}
}
public static void Main(string[] args)
{
//
// Hello World server
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} HWServer [Name]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Name Your name. Default: World");
Console.WriteLine();
args = new string[] { "World" };
}
string name = args[0];
// Create
using (var context = new ZContext())
{
var thread = new Thread(() => ZapLoop(context));
thread.Start();
using (var responder = new ZSocket(context, ZSocketType.REP))
{
responder.CurveServer = true;
responder.CurveSecretKey =
Encoding.ASCII.GetBytes(Z85.Decode("JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6"));
// Bind
responder.Bind("tcp://*:5555");
while (true)
{
// Receive
using (ZFrame request = responder.ReceiveFrame())
{
Console.WriteLine("Received {0}", request.ReadString());
// Do some work
Thread.Sleep(1);
// Send
responder.Send(new ZFrame(name));
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.