Advertisement
TheVideoVolcano

SIMPLE VB.NET TCP SOCKET CHAT CODE

Mar 8th, 2014
4,562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.45 KB | None | 0 0
  1. 'SIMPLE VB.NET TCP SOCKET CHAT CODE
  2.  
  3. Imports System.Net.Sockets
  4. Imports System.Text
  5. Imports System.Threading
  6.  
  7. Public Class Form1
  8.  
  9. Dim listenthread As New Thread(AddressOf listen)
  10. Dim server As New TcpListener(45888)
  11. Dim client As New TcpClient
  12. Dim stream As NetworkStream
  13. Dim connected As Boolean
  14.  
  15. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  16. Try
  17. client.Connect(TextBox1.Text, 45888)
  18. connected = True
  19. stream = client.GetStream
  20. listenthread.Start()
  21. Catch ex As Exception
  22. connected = False
  23. End Try
  24. End Sub
  25.  
  26. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
  27. listenthread.Abort()
  28. End Sub
  29.  
  30. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  31. connected = False
  32. CheckForIllegalCrossThreadCalls = False
  33. End Sub
  34.  
  35. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  36. server.Start()
  37. client = server.AcceptTcpClient
  38. stream = client.GetStream
  39. listenthread.Start()
  40. End Sub
  41.  
  42. Sub listen()
  43. Dim rec(client.ReceiveBufferSize) As Byte
  44. Do
  45. stream.Read(rec, 0, client.ReceiveBufferSize)
  46. Dim rectext As String = Encoding.ASCII.GetString(rec)
  47. MsgBox(rectext)
  48. Loop
  49. End Sub
  50.  
  51. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  52. Dim sendtext() As Byte = Encoding.ASCII.GetBytes(TextBox2.Text)
  53. stream.Write(sendtext, 0, sendtext.Length)
  54. End Sub
  55. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement