Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Net.Sockets
- Imports System.Threading
- Imports System.IO
- Imports System.Net
- Imports System.Text
- Public Class Lobby
- Public connect As Boolean
- Dim username As String = Login.username
- Dim pictureArray As New ArrayList()
- Dim labelArray As New ArrayList()
- Dim wait As Boolean = False
- Delegate Sub AddMessage(ByRef message As String)
- Private Const port As Integer = 54545
- Private broadcastAddress As String = "255.255.255.255"
- Private receivingClient As UdpClient
- Private sendingClient As UdpClient
- Private receivingThread As Thread
- Private Sub Lobby_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- broadcastAddress = InputBox("What is the hamachi IP")
- txtbxChat.Focus()
- InitializeSender()
- InitializeReceiver()
- pictureArray.Add(pctPlayerOne)
- pictureArray.Add(pctPlayerTwo)
- pictureArray.Add(pctPlayerThree)
- pictureArray.Add(pctPlayerFour)
- labelArray.Add(lblPlayerOne)
- labelArray.Add(lblPlayerTwo)
- labelArray.Add(lblPlayerThree)
- labelArray.Add(lblPlayerFour)
- If StartUp.isHost = True Then
- lblPlayerOne.Text = username
- lblPlayerOne.Visible = True
- pctPlayerOne.Visible = True
- Login.Close()
- StartUp.Close()
- End If
- End Sub
- Private Sub InitializeSender()
- sendingClient = New UdpClient(broadcastAddress, port)
- sendingClient.EnableBroadcast = True
- End Sub
- Private Sub InitializeReceiver()
- receivingClient = New UdpClient(port)
- Dim start As ThreadStart = New ThreadStart(AddressOf Receiver)
- receivingThread = New Thread(start)
- receivingThread.IsBackground = True
- receivingThread.Start()
- End Sub
- Private Sub Receiver()
- Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port)
- Dim messageDelegate As AddMessage = AddressOf MessageReceived
- While (True)
- Dim data() As Byte
- data = receivingClient.Receive(endPoint)
- Dim message As String = Encoding.ASCII.GetString(data)
- Invoke(messageDelegate, message)
- End While
- End Sub
- Private Sub MessageReceived(ByRef message As String)
- rtbChat.Text += message + vbNewLine
- End Sub
- Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
- Main.Show()
- End Sub
- Private Sub btnSend_Click_1(sender As Object, e As EventArgs) Handles btnSend.Click
- txtbxChat.Text = txtbxChat.Text.TrimEnd()
- If (Not String.IsNullOrEmpty(txtbxChat.Text)) Then
- Dim toSend As String = username + ": " + txtbxChat.Text
- Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
- sendingClient.Send(data, data.Length)
- txtbxChat.Text = ""
- End If
- txtbxChat.Focus()
- End Sub
- Private Sub txtbxChat_KeyDown(sender As Object, e As KeyEventArgs) Handles txtbxChat.KeyDown
- If e.KeyCode = Keys.Enter Then
- e.SuppressKeyPress = True
- txtbxChat.Text = txtbxChat.Text.TrimEnd()
- If (Not String.IsNullOrEmpty(txtbxChat.Text)) Then
- Dim toSend As String = username + ": " + txtbxChat.Text
- Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
- sendingClient.Send(data, data.Length)
- txtbxChat.Text = ""
- End If
- txtbxChat.Focus()
- End If
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement