如何使用MSMQ发送/接收多播消息? [英] How do I send/receive multicast messages using MSMQ?

查看:269
本文介绍了如何使用MSMQ发送/接收多播消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我找到一些示例代码,以使用您选择的任何.NET语言使用MSMQ发送和接收多播消息.我搜索了一下,发现发送了:

Can someone please help me find some sample code to SEND and RECEIVE Multicast messages using MSMQ using any .NET language of your choice. I have searched around and kind of see the send being:

MessaegQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")

我尝试用Receive做相同的想法:

I tried to do the same idea with Receive:

MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Receive();

但是我什么也没得到.谁能显示一些有关如何接收多播消息的示例代码?还是我发错了?

but I get nothing. Can anyone show some sample code on how to receive multicast messages? or am I sending them wrong?

推荐答案

所以我知道了.

要发送多播消息:

MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")

要接收多播消息:

这有点棘手,因为您无法订阅多播地址.您需要做的是创建一个队列,最好是创建一个私有队列,该队列将附加到您要监视的多播地址,然后侦听您创建的多播地址INSTEAD的私有队列.像这样:

It is a little tricky because you can't subscribe to the multicast address. What you need to do is create a queue, probably best to create a private queue that will be attached to the multicast address you want to monitor and then listen to the private queue you created INSTEAD of the multicast address. Something like this:

  Dim privMulticastQueue As String = GetPrivateQueueForMulticastAddress("formatname:multicast=234.1.1.1:8081")
  Dim msgq as MessageQueue = GetMessageQueue(privMulticastQueue)
  msgq.MulticastAddress = GetMulticastAddress(destination)
  msgq.Label = "Private Queue for receiving messages from: " & destination
  msgq.Receive()

以及一些支持方法(可能有更好的编写方法,因此可以随时进行纠正,但这是我的第一个破解方法):

And some supporting methods (probably there is a better way to write them so feel free to correct but this is my first crack at it):

 Private Function GetPrivateQueueForMulticastAddress(ByVal dest As String) As String
    Dim privateQ As String = GetMulticastAddress(dest).Replace(".", "_").Replace(":", "_")
    Return ".\Private$\" & privateQ
 End Function

Private Function GetMulticastAddress(ByVal dest As String) As String
    Return dest.Split("=")(1)
End Function

Private Function GetMessageQueue(ByVal dest As String) As MessageQueue   
      Try
           If Not MessageQueue.Exists(dest) Then
             MessageQueue.Create(dest)
            End If

            Dim msgq As MessageQueue = New MessageQueue(dest)
            Return msgq
      Catch ex As Exception
            Throw New EMGException("Failed while trying to use destination: " & dest, ex)
      End Try

End Function

这篇关于如何使用MSMQ发送/接收多播消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆