3

C#バインディングを使用してzeromqのhelloworldの例を作成しようとしています。.Netライブラリ(clrzmq.dll)を正常に構築しました。monoに付属のcsharpコンパイラを使用して、コマンドラインから単一のcsharpソースを構築しようとしています。

以下にソースファイルの内容を含めました。

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);
                }
            }
        }
    }
}

これが私が使用するコマンドラインコマンドと私が受け取るエラーメッセージです。

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

ガイドラインに付属している「些細な」helloworldサンプルコードである、表面上はコンパイル時にエラーが非常に多い理由がわかりません。

zeromqの.Netアセンブリは正常にビルドされたので、上記のエラーが発生する理由がわかりません(上記のコードに問題がないと仮定して)-これを修正するにはどうすればよいですか?

Ubuntu 10.0.4 LTS、64ビットで実行しています。

[編集]

私のモノビルドの詳細は次のとおりです。

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)
4

2 に答える 2

2

ここで両方の部分:

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);
                }
            }
        }
    }
}
于 2012-10-18T03:51:49.007 に答える
2

ライブラリの csharp が改善されたため、例は少し古くなっているようです ;) チェックするために clrzmq をダウンロードしました。このバージョンを試してください:

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);
                }
            }
        }
    }
}

投稿の古いバージョン:

どのモノバージョンを使用していますか? mcs の代わりに gmcs または dmcs を呼び出したいと思うかもしれません。

于 2011-08-02T09:27:25.687 に答える