Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'SIMPLE VB.NET TCP SOCKET CHAT CODE
- Imports System.Net.Sockets
- Imports System.Text
- Imports System.Threading
- Public Class Form1
- Dim listenthread As New Thread(AddressOf listen)
- Dim server As New TcpListener(45888)
- Dim client As New TcpClient
- Dim stream As NetworkStream
- Dim connected As Boolean
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Try
- client.Connect(TextBox1.Text, 45888)
- connected = True
- stream = client.GetStream
- listenthread.Start()
- Catch ex As Exception
- connected = False
- End Try
- End Sub
- Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
- listenthread.Abort()
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- connected = False
- CheckForIllegalCrossThreadCalls = False
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- server.Start()
- client = server.AcceptTcpClient
- stream = client.GetStream
- listenthread.Start()
- End Sub
- Sub listen()
- Dim rec(client.ReceiveBufferSize) As Byte
- Do
- stream.Read(rec, 0, client.ReceiveBufferSize)
- Dim rectext As String = Encoding.ASCII.GetString(rec)
- MsgBox(rectext)
- Loop
- End Sub
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- Dim sendtext() As Byte = Encoding.ASCII.GetBytes(TextBox2.Text)
- stream.Write(sendtext, 0, sendtext.Length)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement