Im trying to make a thread that accept and reads incoming message but when i send a message it wont display it
PS: Is their a better to do this with multi-connection?
Listener
Code:Imports System.Net.Sockets Public Class Form1 Dim MyListener As New TcpListener("6961") Dim MyClient As New System.Net.Sockets.TcpClient Dim MyNetStream As System.Net.Sockets.NetworkStream Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MyListener.Start() Dim MainThread As Threading.Thread = New Threading.Thread(AddressOf ReadInfo) End Sub Private Sub ReadInfo() MyClient = MyListener.AcceptTcpClient If MyClient.Connected = True Then Try MyNetStream = MyClient.GetStream Dim BytesFrom(MyClient.ReceiveBufferSize) As Byte MyNetStream.Read(BytesFrom, 0, CInt(MyClient.ReceiveBufferSize)) Dim ByteToString As String = System.Text.ASCIIEncoding.ASCII.GetString(BytesFrom) Dim MySelect() As String = ByteToString.Split("|") Select Case MySelect(0) Case "Msg" TextBox1.Text = TextBox1.Text + MySelect(1) + " said : " + MySelect(2) End Select Catch ex As Exception End Try End If End Sub End Class
Client:
Code:Public Class Form1 Dim MyClient As New System.Net.Sockets.TcpClient Dim MyNetStream As System.Net.Sockets.NetworkStream Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try MyClient.Connect(TextBox1.Text, TextBox2.Text) Button1.Hide() Button2.Show() TextBox1.ReadOnly = True TextBox2.ReadOnly = True MsgBox("Connected to : " & TextBox1.Text & " on Port : " & TextBox2.Text, MsgBoxStyle.Information, Application.ProductName) Catch ex As Exception MsgBox("Could not connect to : " & TextBox1.Text & " on Port : " & TextBox2.Text, MsgBoxStyle.Critical, Application.ProductName) End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try MyClient.Close() Button1.Show() Button2.Hide() TextBox1.ReadOnly = False TextBox2.ReadOnly = False MsgBox("Disconnected to : " & TextBox1.Text & " on Port : " & TextBox2.Text, MsgBoxStyle.Information, Application.ProductName) Catch ex As Exception End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click SendMsg("Msg" + "|" + TextBox3.Text + "|" + TextBox4.Text) End Sub Private Sub SendMsg(ByVal MsgString As String) If MyClient.Connected = True Then MyNetStream = MyClient.GetStream Dim MsgASCII() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(MsgString) MyNetStream.Write(MsgASCII, 0, MsgASCII.Length) End If End Sub Enb Class




Reply With Quote