ironsniper01

Form1.vb

Oct 4th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.IO
  3. Imports System.Net.Sockets
  4. Public Class Form1
  5. Dim Listning As TcpListener
  6. Dim Allclient As TcpClient
  7. Dim clientList As New List(Of ClassforClient)
  8. Dim pReader As StreamReader
  9. Dim pClient As ClassforClient
  10. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  11. Listning = New TcpListener(IPAddress.Any, 3818)
  12. Listning.Start()
  13. UpdateList("Server Starting", False)
  14. Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
  15. End Sub
  16. ' create a delegate
  17. Delegate Sub _cUpdate(ByVal str As String, ByVal relay As Boolean)
  18. Sub UpdateList(ByVal str As String, ByVal relay As Boolean)
  19. On Error Resume Next
  20. If InvokeRequired Then
  21. Invoke(New _cUpdate(AddressOf UpdateList), str, relay)
  22. Else
  23. TextBox1.AppendText(str & vbNewLine)
  24. ' if relay we will send a string
  25. If relay Then send(str)
  26. End If
  27. End Sub
  28. Sub send(ByVal str As String)
  29. For x As Integer = 0 To clientList.Count - 1
  30. Try
  31. clientList(x).Send(str)
  32. Catch ex As Exception
  33. clientList.RemoveAt(x)
  34. End Try
  35. Next
  36. End Sub
  37. Sub AcceptClient(ByVal ar As IAsyncResult)
  38. pClient = New ClassforClient(Listning.EndAcceptTcpClient(ar))
  39. AddHandler(pClient.getMessage), AddressOf MessageReceived
  40. AddHandler(pClient.clientLogout), AddressOf ClientExited
  41. clientList.Add(pClient)
  42. UpdateList("New Client Joined!", True)
  43. Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
  44. End Sub
  45. Sub MessageReceived(ByVal str As String)
  46. UpdateList(str, True)
  47. End Sub
  48. Sub ClientExited(ByVal client As ClassforClient)
  49. clientList.Remove(client)
  50. UpdateList("Client Exited!", True)
  51. End Sub
  52. Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
  53. If e.KeyCode = Keys.Enter Then
  54. e.SuppressKeyPress = True
  55. UpdateList("Server says : " & TextBox2.Text, True)
  56. TextBox2.Clear()
  57. End If
  58. End Sub
  59. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  60. UpdateList("Server says : " & TextBox2.Text, True)
  61. TextBox2.Clear()
  62. End Sub
  63. End Class
Add Comment
Please, Sign In to add comment