Advertisement
ASP_Volume2 Internet/ HTML #33259

IP Multicasting with Winsock control

Implements IP multicasting

AI

Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Codice sorgente
original-source
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 property  of WinSock to sckUDPProtocol, RemoteHost to 224.0.0.1, RemotePort to 9000. Add the code bellow to the form and save project.
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.
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
Commenti originali (3)
Recuperato da Wayback Machine