0

I need to write an application that runs on Windows and receives Multicast messages. I have some specific questions.

I normally use Winsock control but out the box it does not support multicast operations. Can anyone help with API to get round this or do I need to but a custom OCX.

My PC NIC will be connected to a local network. How do I configure my PC to allow the multicast data to reach my application.

Russell M
  • 1
  • 2

1 Answers1

1

You can receive multicast packets using a UDP socket, which VB's WinSock does support. However, you must use the underlying Winsock API setsockopt() function, which the WinSock control does not support, to actually join the socket to the multicast group that packets will be received from.

Fortunately, the WinSock control does have a SocketHandle property, which you can use to access the actual socket so you can pass it to setsockopt() manually.

The 1st result I get from Googling "vb winsock multicast" is:

IP Multicasting with Winsock control

Which shows exactly how to do this:

It's easy to add IP Multicasting functionality to VB's Winsock control. First, create a new standard EXE project, name it Sender. Set the Caption property of the form to MSender. Draw on the form TextBox and WinSock controls. Set the Protocol propertyof WinSock to sckUDPProtocol, RemoteHost to 224.0.0.1, RemotePort to 9000. Add the code bellow to the form and save project.

'**************************************
' Name: IP Multicasting with Winsock con
'     trol
' Description:Implements IP multicasting
'     
' By: Iulianos Kakulidis (from psc cd)
'
'
' Inputs:None
'
' Returns:None
'
'Assumes:None
'
'Side Effects:None
'**************************************

Private Sub Form_Load()
    Winsock1.Bind 5000
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
        Winsock1.SendData Text1.Text
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End If
End Sub

Now, create new project, name it Listener, Set the Caption property of the form to MListener. Draw on the form TextBox and WinSock controls. Set the Protocol property of WinSock to sckUDPProtocol. Set the property MultiLine of the TextBox to true, ScrollBars to 3 (both). Add the code bellow to the form.

Private Sub Form_Load()
    Dim ipmreq As ipm_req
    Winsock1.Bind 9000
    ipmreq.ipm_multiaddr = inet_addr("224.0.0.1")
    ipmreq.ipm_interface = 0
    'join group
    setsockopt Winsock1.SocketHandle, _
    0, 5, ipmreq, Len(ipmreq)
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim stdata As String
    Winsock1.GetData stdata
    Text1.Text = Text1.Text & Chr$(13) & Chr$(10) & stdata
End Sub

Add the module to the Listener project with the code bellow, save the project.

'**************************************
'Windows API/Global Declarations for :IP
'     Multicasting with Winsock control
'**************************************

Public Type ipm_req
    ipm_multiaddr As Long
    ipm_interface As Long
    End Type

Public Declare Function setsockopt Lib "wsock32" _
    (ByVal s As Integer, ByVal level As Integer, _
    ByVal optname As Integer, ByRef optval As Any, ByVal optlen As Integer) As Integer

Public Declare Function inet_addr Lib "wsock32" _
        (ByVal cp As String) As Long

Run Sender and Listener applications. Type message in Sender's TextBox, press Enter, the same text will appear in the TextBox on the Listener's form. Tested on local network

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770