簡體   English   中英

如何通過工作組連接到MSMQ?

[英]How can I connect to MSMQ over a workgroup?

我正在使用MSMQ編寫一個簡單的控制台客戶端-服務器應用程序。 我試圖在我們設置的工作組上運行它。 當它們在同一台計算機上運行時,它們運行得很好,但是我無法讓它們通過網絡連接。 我嘗試添加Direct=OS:以及其他一些前言的組合,但我的想法已經用盡,而且顯然不知道正確的方法。 我的隊列沒有GUID,這也有些令人困惑。 每當我嘗試連接到遠程計算機時,都會收到無效的隊列名稱消息。 我必須做些什么才能使這項工作?

服務器:

class Program
{
    static string _queue = @"\Private$\qim";
    static MessageQueue _mq;
    static readonly object _mqLock = new object();

    static void Main(string[] args)
    {
        _queue = Dns.GetHostName() + _queue;
        lock (_mqLock)
        {
            if (!MessageQueue.Exists(_queue))
                _mq = MessageQueue.Create(_queue);
            else
                _mq = new MessageQueue(_queue);
        }
        Console.Write("Starting server at {0}:\n\n", _mq.Path);
        _mq.Formatter = new BinaryMessageFormatter();
        _mq.BeginReceive(new TimeSpan(0, 1, 0), new object(), OnReceive);
        while (Console.ReadKey().Key != ConsoleKey.Escape) { }
        _mq.Close();
    }

    static void OnReceive(IAsyncResult result)
    {
        Message msg;
        lock (_mqLock)
        {
            try
            {
                msg = _mq.EndReceive(result);
                Console.Write(msg.Body);
            }
            catch (Exception ex)
            {
                Console.Write("\n" + ex.Message + "\n");
            }
        }
        _mq.BeginReceive(new TimeSpan(0, 1, 0), new object(), OnReceive);
    }
}

客戶:

class Program
{
    static MessageQueue _mq;

    static void Main(string[] args)
    {
        string queue;
        while (_mq == null)
        {
            Console.Write("Enter the queue name:\n");
            queue = Console.ReadLine();
            //queue += @"\Private$\qim";
            try
            {
                if (MessageQueue.Exists(queue))
                    _mq = new MessageQueue(queue);
            }
            catch (Exception ex)
            {
                Console.Write("\n" + ex.Message + "\n");
                _mq = null;
            }
        }
        Console.Write("Connected. Begin typing.\n\n");
        _mq.Formatter = new BinaryMessageFormatter();
        ConsoleKeyInfo key = new ConsoleKeyInfo();
        while (key.Key != ConsoleKey.Escape)
        {
            key = Console.ReadKey();
            _mq.Send(key.KeyChar.ToString());
        }
    }
}

您需要使用以下格式來連接到遠程專用隊列:

FormatName:Direct=OS:machinename\\private$\\queuename

有一個方便的文章在這里有更多的位信息

我一次工作了。 我的建議是:1:不要這樣做; 2:不要這樣做; 3:僅使用專用隊列並在192.168.xx子網中為工作站分配靜態IP地址。 為每台計算機提供一個主機文件以將計算機名稱映射到IP地址,或者直接在格式名稱中使用IP地址。 依靠工作組中的姓名解析,希望能夠在拉斯維加斯贏得輪盤賭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM