Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. Imports System.Net.Sockets
  2. Imports System.Threading
  3. Imports System.IO
  4. Imports System.Net
  5. Imports System.Text
  6.  
  7. Public Class Lobby
  8. Public connect As Boolean
  9. Dim username As String = Login.username
  10. Dim pictureArray As New ArrayList()
  11. Dim labelArray As New ArrayList()
  12. Dim wait As Boolean = False
  13. Delegate Sub AddMessage(ByRef message As String)
  14. Private Const port As Integer = 54545
  15. Private broadcastAddress As String = "255.255.255.255"
  16.  
  17. Private receivingClient As UdpClient
  18. Private sendingClient As UdpClient
  19.  
  20. Private receivingThread As Thread
  21.  
  22. Private Sub Lobby_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  23. broadcastAddress = InputBox("What is the hamachi IP")
  24. txtbxChat.Focus()
  25. InitializeSender()
  26. InitializeReceiver()
  27. pictureArray.Add(pctPlayerOne)
  28. pictureArray.Add(pctPlayerTwo)
  29. pictureArray.Add(pctPlayerThree)
  30. pictureArray.Add(pctPlayerFour)
  31. labelArray.Add(lblPlayerOne)
  32. labelArray.Add(lblPlayerTwo)
  33. labelArray.Add(lblPlayerThree)
  34. labelArray.Add(lblPlayerFour)
  35. If StartUp.isHost = True Then
  36. lblPlayerOne.Text = username
  37. lblPlayerOne.Visible = True
  38. pctPlayerOne.Visible = True
  39. Login.Close()
  40. StartUp.Close()
  41.  
  42.  
  43. End If
  44. End Sub
  45. Private Sub InitializeSender()
  46. sendingClient = New UdpClient(broadcastAddress, port)
  47. sendingClient.EnableBroadcast = True
  48. End Sub
  49.  
  50. Private Sub InitializeReceiver()
  51. receivingClient = New UdpClient(port)
  52.  
  53. Dim start As ThreadStart = New ThreadStart(AddressOf Receiver)
  54. receivingThread = New Thread(start)
  55. receivingThread.IsBackground = True
  56. receivingThread.Start()
  57. End Sub
  58.  
  59. Private Sub Receiver()
  60. Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port)
  61. Dim messageDelegate As AddMessage = AddressOf MessageReceived
  62.  
  63. While (True)
  64. Dim data() As Byte
  65. data = receivingClient.Receive(endPoint)
  66. Dim message As String = Encoding.ASCII.GetString(data)
  67. Invoke(messageDelegate, message)
  68. End While
  69. End Sub
  70.  
  71. Private Sub MessageReceived(ByRef message As String)
  72. rtbChat.Text += message + vbNewLine
  73. End Sub
  74.  
  75.  
  76.  
  77.  
  78.  
  79. Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
  80. Main.Show()
  81.  
  82.  
  83. End Sub
  84.  
  85. Private Sub btnSend_Click_1(sender As Object, e As EventArgs) Handles btnSend.Click
  86. txtbxChat.Text = txtbxChat.Text.TrimEnd()
  87.  
  88. If (Not String.IsNullOrEmpty(txtbxChat.Text)) Then
  89. Dim toSend As String = username + ": " + txtbxChat.Text
  90. Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
  91. sendingClient.Send(data, data.Length)
  92. txtbxChat.Text = ""
  93. End If
  94.  
  95. txtbxChat.Focus()
  96. End Sub
  97.  
  98. Private Sub txtbxChat_KeyDown(sender As Object, e As KeyEventArgs) Handles txtbxChat.KeyDown
  99. If e.KeyCode = Keys.Enter Then
  100. e.SuppressKeyPress = True
  101. txtbxChat.Text = txtbxChat.Text.TrimEnd()
  102.  
  103. If (Not String.IsNullOrEmpty(txtbxChat.Text)) Then
  104. Dim toSend As String = username + ": " + txtbxChat.Text
  105. Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
  106. sendingClient.Send(data, data.Length)
  107. txtbxChat.Text = ""
  108. End If
  109.  
  110. txtbxChat.Focus()
  111. End If
  112.  
  113. End Sub
  114.  
  115. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  116.  
  117. End Sub
  118. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement