Search Tools Links Login

Multicast Functions using Winsock Control


Visual Basic 6, or VB Classic

Allows you to recieve multicast UDP packets.(Please vote and leave comments)

Original Author: Dolphin6776

API Declarations

Declare Function setsockopt Lib "wsock32.dll" (ByVal s As Long, ByVal Level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
'Taken from winsock.h
Const IP_PROTOIP = 0
Const IP_MULTICAST_TTL = 3
Const IP_MULTICAST_LOOP = 4
Const IP_ADD_MEMBERSHIP = 5
Const IP_DROP_MEMBERSHIP = 6
Const IP_DEFAULT_MULTICAST_TTL = 1
Const IP_DEFAULT_MULTICAST_LOOP = 1
Public Type ip_mreq
imr_multiaddr As Long
imr_interface As Long
End Type

Code

'To send a multicast you set winsock protocol to UDP with an address of 224.0.0.1 and set a port.
'Bind to any port...
'To set TTL call SockMulti_TTL using .SocketHandle from Winsock Control.
'To recieve a multicast, bind to the port that is to recieve on,set protocol to UDP, and Call SockADD_Membership using 224.0.0.1 as MultiAddr.
'To stop recieving multicast, call SockDRP_Membership using the same options you used to join.
'Use this to add membership to a multicast address.
Public Sub SockADD_Membership(SocketHandle As Long, MultiAddr As String, Optional LocalInterface As Long = 0)
Dim ip_m As ip_mreq
ip_m.imr_multiaddr = inet_addr(MultiAddr)
ip_m.imr_interface = LocalInterface 'Interface to join on
setsockopt SocketHandle, IP_PROTOIP, IP_ADD_MEMBERSHIP, ip_m, Len(ip_m)
End Sub
'Use this to drop membership to a multicast address.
Public Sub SockDRP_Membership(SocketHandle As Long, MultiAddr As String, Optional LocalInterface As Long = 0)
Dim ip_m As ip_mreq
ip_m.imr_multiaddr = inet_addr(MultiAddr)
ip_m.imr_interface = LocalInterface 'Interface to leave on
setsockopt SocketHandle, IP_PROTOIP, IP_DROP_MEMBERSHIP, ip_m, Len(ip_m)
End Sub
'Sets TTL for sends to multicast addresses.
Public Sub SockMulti_TTL(SocketHandle As Long, TTL As Long)
setsockopt SocketHandle, IP_PROTOIP, IP_MULTICAST_TTL, TTL, Len(TTL)
End Sub

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 301 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.