gpt4 book ai didi

c# - 使用命令行单声道构建 zeromq helloworld 示例

转载 作者:行者123 更新时间:2023-11-30 15:43:13 25 4
gpt4 key购买 nike

我正在尝试使用 C# 绑定(bind)为 zeromq 构建一个 helloworld 示例。我已成功构建 .Net 库 (clrzmq.dll)。我正在尝试构建单个 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

我不确定为什么在编译指南提供的表面上“微不足道”的 hello world 示例代码时会出现这么多错误。

zeromq 的 .Net 程序集已成功构建,所以我不明白为什么会出现上述错误(假设上面的代码没有任何问题)- 我该如何解决这个问题?

我在 64 位 Ubuntu 10.0.4 LTS 上运行。

[编辑]

我的单声道构建的详细信息如下:

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)

最佳答案

看起来示例有点过时,因为库得到了更好的锐化;)我刚刚下载了 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);
}
}
}
}
}

帖子的旧版本:

您使用的是哪个单声道版本?您可能对调用 gmcs 或 dmcs 而不是 mcs 感兴趣。

关于c# - 使用命令行单声道构建 zeromq helloworld 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6909568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com