Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Net.Sockets
- Imports System.Runtime.InteropServices
- Public Class Form1
- <DllImport("user32.dll", EntryPoint:="FlashWindow")> _
- Public Shared Function FlashWindow(ByVal hwnd As Integer, ByVal bInvert As Integer) As Integer
- End Function
- Dim mCast As New UdpClient(4446)
- Dim ep As Net.IPEndPoint = New Net.IPEndPoint(Net.IPAddress.Parse("230.0.0.1"), 4446)
- Dim ep2 As Net.IPEndPoint = New Net.IPEndPoint(Net.IPAddress.Parse("230.0.0.1"), 4446)
- Dim KAFlags As New List(Of Boolean)
- Dim KAcheck As Integer = 0
- Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface
- Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- Try
- If mCast.Client.Connected = True Then
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :Disconnect")
- mCast.Send(sendbytes, sendbytes.Length, ep)
- mCast.Close()
- End If
- Catch ex As Exception
- End Try
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- mCast.Client.ReceiveTimeout = 100
- mCast.Client.Blocking = False
- mCast.MulticastLoopback = False
- Dim AddressString As String = ""
- For i = 0 To mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1
- If mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
- Dim bytez() As Byte = mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.GetAddressBytes
- AddressString = bytez(0) & "." & bytez(1) & "." & bytez(2) & "." & bytez(3)
- Exit For
- End If
- Next
- mCast.JoinMulticastGroup(Net.IPAddress.Parse("230.0.0.1"), Net.IPAddress.Parse(AddressString))
- mCast.Client.EnableBroadcast = True
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :Connect")
- ListBox1.Items.Add(TextBox1.Text)
- mCast.Send(sendbytes, sendbytes.Length, ep)
- Timer1.Enabled = True
- Timer2.Enabled = True
- Button2.Enabled = True
- Button3.Enabled = True
- Button1.Enabled = False
- End Sub
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :Disconnect")
- mCast.Send(sendbytes, sendbytes.Length, ep)
- mCast.Close()
- Timer1.Enabled = False
- Timer2.Enabled = False
- Button2.Enabled = False
- Button3.Enabled = False
- Button1.Enabled = True
- End Sub
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- Try
- Dim rcvbytes() As Byte = mCast.Receive(ep2)
- Dim MCSubRcv As String = System.Text.Encoding.ASCII.GetString(rcvbytes)
- If MCSubRcv <> "" And MCSubRcv.Contains(":Chat") Then
- RichTextBox1.AppendText(MCSubRcv.Split(":")(0) & ": " & MCSubRcv.Split(":")(1) & Chr(13))
- RichTextBox1.SelectionStart = RichTextBox1.TextLength
- RichTextBox1.ScrollToCaret()
- If Me.Focused = False Then
- FlashWindow(Me.Handle, 1)
- End If
- End If
- If MCSubRcv <> "" And MCSubRcv.Contains(":Disconnect") = True Then
- ListBox1.Items.RemoveAt(MCSubRcv.Split(":")(0))
- End If
- If MCSubRcv <> "" And MCSubRcv.Contains(":KeepAlive") = True Then
- If ListBox1.Items.Count > 0 Then
- Dim found As Boolean = False
- For i = 0 To ListBox1.Items.Count - 1
- If ListBox1.Items(i) = MCSubRcv.Split(":")(0) Then
- found = True
- KAFlags(i - 1) = True
- End If
- Next
- If found = False Then
- ListBox1.Items.Add(MCSubRcv.Split(":")(0))
- KAFlags.Add(True)
- End If
- Else
- ListBox1.Items.Add(MCSubRcv.Split(":")(0))
- KAFlags.Add(True)
- End If
- End If
- Catch ex As Exception
- End Try
- End Sub
- Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ": :" & "KeepAlive")
- mCast.Send(sendbytes, sendbytes.Length, ep)
- If KAcheck >= 4 Then
- For i = 0 To KAFlags.Count - 1
- If KAFlags(i) = False Then
- ListBox1.Items.RemoveAt(i + 1)
- KAFlags.RemoveAt(i)
- Exit For
- End If
- Next
- KAcheck = 0
- For i = 0 To KAFlags.Count - 1
- KAFlags(i) = False
- Next
- Else
- KAcheck = KAcheck + 1
- End If
- End Sub
- 'Send Chat
- Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
- If e.KeyCode = Keys.Enter Then
- sendz()
- End If
- End Sub
- Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
- sendz()
- End Sub
- Private Sub sendz()
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & ":" & TextBox2.Text & ":" & "Chat")
- mCast.Send(sendbytes, sendbytes.Length, ep)
- RichTextBox1.AppendText("Me: " & TextBox2.Text & Chr(13))
- RichTextBox1.SelectionStart = RichTextBox1.TextLength
- RichTextBox1.ScrollToCaret()
- TextBox2.Text = ""
- End Sub
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
- For i = 0 To mycomputerconnections.Length - 1
- ComboBox1.Items.Add(mycomputerconnections(i).Name)
- Next
- ComboBox1.SelectedIndex = 0
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement