Advertisement
Robomatics

UDP Multicast Chat

Apr 23rd, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.50 KB | None | 0 0
  1. Imports System.Net.Sockets
  2. Imports System.Runtime.InteropServices
  3.  
  4. Public Class Form1
  5.     <DllImport("user32.dll", EntryPoint:="FlashWindow")> _
  6.     Public Shared Function FlashWindow(ByVal hwnd As Integer, ByVal bInvert As Integer) As Integer
  7.     End Function
  8.  
  9.     Dim mCast As New UdpClient(4446)
  10.     Dim ep As Net.IPEndPoint = New Net.IPEndPoint(Net.IPAddress.Parse("230.0.0.1"), 4446)
  11.     Dim ep2 As Net.IPEndPoint = New Net.IPEndPoint(Net.IPAddress.Parse("230.0.0.1"), 4446)
  12.     Dim KAFlags As New List(Of Boolean)
  13.     Dim KAcheck As Integer = 0
  14.     Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface
  15.  
  16.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  17.         Try
  18.             If mCast.Client.Connected = True Then
  19.                 Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :Disconnect")
  20.                 mCast.Send(sendbytes, sendbytes.Length, ep)
  21.                 mCast.Close()
  22.             End If
  23.         Catch ex As Exception
  24.         End Try
  25.     End Sub
  26.  
  27.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  28.         mCast.Client.ReceiveTimeout = 100
  29.         mCast.Client.Blocking = False
  30.         mCast.MulticastLoopback = False
  31.         Dim AddressString As String = ""
  32.         For i = 0 To mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1
  33.  
  34.             If mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
  35.  
  36.                 Dim bytez() As Byte = mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.GetAddressBytes
  37.                 AddressString = bytez(0) & "." & bytez(1) & "." & bytez(2) & "." & bytez(3)
  38.                 Exit For
  39.             End If
  40.         Next
  41.  
  42.         mCast.JoinMulticastGroup(Net.IPAddress.Parse("230.0.0.1"), Net.IPAddress.Parse(AddressString))
  43.         mCast.Client.EnableBroadcast = True
  44.         Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :Connect")
  45.         ListBox1.Items.Add(TextBox1.Text)
  46.         mCast.Send(sendbytes, sendbytes.Length, ep)
  47.         Timer1.Enabled = True
  48.         Timer2.Enabled = True
  49.         Button2.Enabled = True
  50.         Button3.Enabled = True
  51.         Button1.Enabled = False
  52.     End Sub
  53.  
  54.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  55.         Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :Disconnect")
  56.         mCast.Send(sendbytes, sendbytes.Length, ep)
  57.         mCast.Close()
  58.         Timer1.Enabled = False
  59.         Timer2.Enabled = False
  60.         Button2.Enabled = False
  61.         Button3.Enabled = False
  62.         Button1.Enabled = True
  63.     End Sub
  64.  
  65.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  66.         Try
  67.             Dim rcvbytes() As Byte = mCast.Receive(ep2)
  68.             Dim MCSubRcv As String = System.Text.Encoding.ASCII.GetString(rcvbytes)
  69.  
  70.             If MCSubRcv <> "" And MCSubRcv.Contains(":Chat") Then
  71.                 RichTextBox1.AppendText(MCSubRcv.Split(":")(0) & ": " & MCSubRcv.Split(":")(1) & Chr(13))
  72.                 RichTextBox1.SelectionStart = RichTextBox1.TextLength
  73.                 RichTextBox1.ScrollToCaret()
  74.                 If Me.Focused = False Then
  75.                     FlashWindow(Me.Handle, 1)
  76.                 End If
  77.             End If
  78.             If MCSubRcv <> "" And MCSubRcv.Contains(":Disconnect") = True Then
  79.                 ListBox1.Items.RemoveAt(MCSubRcv.Split(":")(0))
  80.             End If
  81.  
  82.             If MCSubRcv <> "" And MCSubRcv.Contains(":KeepAlive") = True Then
  83.                 If ListBox1.Items.Count > 0 Then
  84.                     Dim found As Boolean = False
  85.                     For i = 0 To ListBox1.Items.Count - 1
  86.                         If ListBox1.Items(i) = MCSubRcv.Split(":")(0) Then
  87.                             found = True
  88.                             KAFlags(i - 1) = True
  89.                         End If
  90.                     Next
  91.                     If found = False Then
  92.                         ListBox1.Items.Add(MCSubRcv.Split(":")(0))
  93.                         KAFlags.Add(True)
  94.                     End If
  95.                 Else
  96.                     ListBox1.Items.Add(MCSubRcv.Split(":")(0))
  97.                     KAFlags.Add(True)
  98.                 End If
  99.             End If
  100.         Catch ex As Exception
  101.         End Try
  102.     End Sub
  103.  
  104.     Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
  105.         Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :" & "KeepAlive")
  106.         mCast.Send(sendbytes, sendbytes.Length, ep)
  107.         If KAcheck >= 4 Then
  108.             For i = 0 To KAFlags.Count - 1
  109.                 If KAFlags(i) = False Then
  110.                     ListBox1.Items.RemoveAt(i + 1)
  111.                     KAFlags.RemoveAt(i)
  112.                     Exit For
  113.                 End If
  114.             Next
  115.             KAcheck = 0
  116.             For i = 0 To KAFlags.Count - 1
  117.                 KAFlags(i) = False
  118.             Next
  119.         Else
  120.             KAcheck = KAcheck + 1
  121.         End If
  122.  
  123.     End Sub
  124.  
  125.     'Send Chat
  126.     Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
  127.         If e.KeyCode = Keys.Enter Then
  128.             sendz()
  129.         End If
  130.     End Sub
  131.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  132.         sendz()
  133.     End Sub
  134.     Private Sub sendz()
  135.         Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ":" & TextBox2.Text & ":" & "Chat")
  136.         mCast.Send(sendbytes, sendbytes.Length, ep)
  137.         RichTextBox1.AppendText("Me: " & TextBox2.Text & Chr(13))
  138.         RichTextBox1.SelectionStart = RichTextBox1.TextLength
  139.         RichTextBox1.ScrollToCaret()
  140.         TextBox2.Text = ""
  141.     End Sub
  142.  
  143.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  144.         mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
  145.         For i = 0 To mycomputerconnections.Length - 1
  146.             ComboBox1.Items.Add(mycomputerconnections(i).Name)
  147.         Next
  148.         ComboBox1.SelectedIndex = 0
  149.     End Sub
  150. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement