ironsniper01

CLient.vb

Oct 4th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.IO
  3. Imports System.Net.Sockets
  4. Public Class Form1
  5. Dim client As TcpClient
  6. Dim sWriter As StreamWriter
  7. Dim NIckFrefix As Integer = New Random().Next(1111, 9999)
  8. Sub xLoad() Handles Me.Load
  9. Me.Text &= " " & NIckFrefix
  10. End Sub
  11. Delegate Sub _xUpdate(ByVal str As String)
  12. Sub xUpdate(ByVal str As String)
  13. If InvokeRequired Then
  14. Invoke(New _xUpdate(AddressOf xUpdate), str)
  15. Else
  16. TextBox3.AppendText(str & vbNewLine)
  17. End If
  18. End Sub
  19.  
  20. Sub read(ByVal ar As IAsyncResult)
  21. Try
  22. xUpdate(New StreamReader(client.GetStream).ReadLine)
  23. client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing)
  24.  
  25. Catch ex As Exception
  26. xUpdate("You have disconnecting from server")
  27. Exit Sub
  28. End Try
  29. End Sub
  30. Private Sub send(ByVal str As String)
  31. Try
  32. sWriter = New StreamWriter(client.GetStream)
  33. sWriter.WriteLine(str)
  34. sWriter.Flush()
  35. Catch ex As Exception
  36. xUpdate("You're not server")
  37. End Try
  38. End Sub
  39. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  40. If Button1.Text = "Connect" Then
  41. Try
  42. client = New TcpClient(TextBox1.Text, CInt(TextBox2.Text))
  43. client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf read), Nothing)
  44. Button1.Text = "Disconnect"
  45. Catch ex As Exception
  46. xUpdate("Can't connect to the server!")
  47. End Try
  48. Else
  49. client.Client.Close()
  50. client = Nothing
  51. Button1.Text = "Connect"
  52. End If
  53. End Sub
  54. Private Sub TextBox4_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox4.KeyDown
  55. If e.KeyCode = Keys.Enter Then
  56. e.SuppressKeyPress = True
  57. send(NIckFrefix & " says : " & TextBox4.Text)
  58. TextBox4.Clear()
  59. End If
  60. End Sub
  61. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  62. send(NIckFrefix & " says : " & TextBox4.Text)
  63. TextBox4.Clear()
  64. End Sub
  65. End Class
Add Comment
Please, Sign In to add comment