简体   繁体   English

使用命令行mono建立zeromq helloworld示例

[英]Building zeromq helloworld example with command line mono

I am trying to build a helloworld example for zeromq using the C# binding. 我正在尝试使用C#绑定为zeromq构建一个helloworld示例。 I have succesfully built the .Net library (clrzmq.dll). 我成功构建了.Net库(clrzmq.dll)。 I am trying to build the single csharp source, form the command line, using the csharp compiler that comes with mono. 我正在尝试使用mono附带的csharp编译器构建单个csharp源,形成命令行。

I have included the contents of the source file below: 我已经包含了以下源文件的内容:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;

/**
 *  Author: Randy Dryburgh
 *  Email: me@rwd.im
 *  License: This example code licensed under the MIT/X11 license.
 */

namespace Examples {
    class hwserver {
        static void Main(string[] args) {
            // allocate a buffer
            byte[] zmq_buffer = new byte[1024];

            //  Prepare our context and socket
            ZMQ.Context context = new ZMQ.Context(1);
            ZMQ.Socket  socket = context.Socket(ZMQ.REP);
            socket.Bind("tcp://*:5555");

            while (true) {
                try {
                    //  Wait for next request from client
                    socket.Recv(out zmq_buffer);
                    string request = Encoding.ASCII.GetString(zmq_buffer);

                    // log that we got one
                    Console.WriteLine("Received request: [%s]", request);
                    //  Do some 'work'
                    Thread.Sleep(1);

                    //  Send reply back to client
                    socket.Send(Encoding.ASCII.GetBytes("World".ToCharArray()));

                } catch (ZMQ.Exception z) {
                    // report the exception
                    Console.WriteLine("ZMQ Exception occurred : {0}", z.Message);
                }
            }
        }
    }
}

Here is the command line command I use and the error message I get. 这是我使用的命令行命令和我得到的错误消息。

oompah@localhost:~/work/dev/c++/3rdparty/zeromq/zguide/examples/C#$ mcs hwserver.cs.v1 -lib:/home/oompah/work/dev/c++/3rdparty/zeromq/clrzmq2/clrzmq/bin/Release/ -r:clrzmq.dll
hwserver.cs.v1(20,53): error CS0234: The type or namespace name `REP' does not exist in the namespace `ZMQ'. Are you missing an assembly reference?
hwserver.cs.v1(20,42): error CS1502: The best overloaded method match for `ZMQ.Context.Socket(ZMQ.SocketType)' has some invalid arguments
/home/oompah/work/dev/c++/3rdparty/zeromq/clrzmq2/clrzmq/bin/Release/clrzmq.dll (Location of the symbol related to previous error)
hwserver.cs.v1(20,42): error CS1503: Argument `#1' cannot convert `object' expression to type `ZMQ.SocketType'
hwserver.cs.v1(26,28): error CS1502: The best overloaded method match for `ZMQ.Socket.Recv(params ZMQ.SendRecvOpt[])' has some invalid arguments
/home/oompah/work/dev/c++/3rdparty/zeromq/clrzmq2/clrzmq/bin/Release/clrzmq.dll (Location of the symbol related to previous error)
hwserver.cs.v1(26,28): error CS1615: Argument `#1' does not require `out' modifier. Consider removing `out' modifier
Compilation failed: 5 error(s), 0 warnings

I am not sure why there are so many errors when compiling what is ostensibly, a "trivial" hello world example code supplied with the guideline. 我不确定在编译表面上是什么时会出现这么多错误,这是指南提供的“琐碎的”hello world示例代码。

The .Net assembly for zeromq built succesfully, so I don't see why I am getting the errors above (assuming that there is nothing wrong with the code above) - how do I fix this? zeromq的.Net程序集成功构建,所以我不明白为什么我得到上面的错误(假设上面的代码没有任何问题) - 我该如何解决这个问题?

I am running on Ubuntu 10.0.4 LTS, 64 bit. 我正在运行Ubuntu 10.0.4 LTS,64位。

[Edit] [编辑]

Details of my mono build are as follows: 我的单声道版本的详细信息如下:

Mono JIT compiler version 2.10.2 (tarball Wed Jul 20 17:42:26 BST 2011)
Copyright (C) 2002-2011 Novell, Inc and Contributors. www.mono-project.com
    TLS:           __thread
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug 
    LLVM:          supported, not enabled.
    GC:            Included Boehm (with typed GC and Parallel Mark)

Here both parts: 这两个部分:

namespace ZMQGuide
{
    class Program
    {
        static void Main(string[] args)
        {
            // ZMQ Context, server socket
            using (Context context = new Context(1)
            using (Socket socket = context.Socket(SocketType.REP))
            {
                socket.Bind("tcp://*:5555");

                while (true)
                {
                    // Wait for next request from client
                    string message = socket.Recv(Encoding.Unicode);
                    Console.WriteLine("Received request: {0}", message);

                    // Do Some 'work'
                    Thread.Sleep(1000);

                    // Send reply back to client
                    socket.Send("World", Encoding.Unicode);
                }
            }
        }
    }
}

namespace ZMQGuide
{
    class Program
    {
        static void Main(string[] args)
        {
            // ZMQ Context and client socket
            using (Context context = new Context(1))
            using (Socket requester = context.Socket(SocketType.REQ))
            {
                requester.Connect("tcp://localhost:5555");

                string request = "Hello";
                for (int requestNum = 0; requestNum < 10; requestNum++)
                {
                    Console.WriteLine("Sending request {0}...", requestNum);
                    requester.Send(request, Encoding.Unicode);

                    string reply = requester.Recv(Encoding.Unicode);
                    Console.WriteLine("Received reply {0}: {1}", requestNum, reply);
                }
            }
        }
    }
}

Looks like example is a bit outdated as library got better csharped ;) I just downloaded clrzmq to check. 看起来示例有点过时,因为库得到了更好的csharped;)我刚刚下载了clrzmq来检查。 Try this version: 试试这个版本:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;

/**
 *  Author: Randy Dryburgh
 *  Email: me@rwd.im
 *  License: This example code licensed under the MIT/X11 license.
 */

namespace Examples {
    class hwserver {
        static void Main(string[] args) {
            // allocate a buffer
            byte[] zmq_buffer = new byte[1024];

            //  Prepare our context and socket
            ZMQ.Context context = new ZMQ.Context(1);
            ZMQ.Socket  socket = context.Socket(ZMQ.SocketType.REP);
            socket.Bind("tcp://*:5555");

            while (true) {
                try {
                    //  Wait for next request from client
                    zmq_buffer = socket.Recv();
                    string request = Encoding.ASCII.GetString(zmq_buffer);

                    // log that we got one
                    Console.WriteLine("Received request: [{0}]", request);
                    //  Do some 'work'
                    Thread.Sleep(1);

                    //  Send reply back to client
                    socket.Send(Encoding.ASCII.GetBytes("World".ToCharArray()));

                } catch (ZMQ.Exception z) {
                    // report the exception
                    Console.WriteLine("ZMQ Exception occurred : {0}", z.Message);
                }
            }
        }
    }
}

Old version of post: 老版本的帖子:

Which mono version are you using? 您使用哪种单声道版本? You may be interested in invoking gmcs or dmcs instead of mcs. 您可能有兴趣调用gmcs或dmcs而不是mcs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM