I have a problem in my tcp listener ,the problem is in the code below i can able toreceivea message at one time onlyfrom aclient machine when the same client machine send message 2nd time i cant get any message in listener,i dont where i gone wrong here is thecomplete code
In this code the thread part is the tcplistener part and the button click is message sendingto listener part,pls tell me where i gone wrong..
Public Class F_TCP_LIST
Dim tcp_listr_thread As New Thread(AddressOf TCPListener_thread)
Dim myTcpListener As Sockets.TcpListener
Dim tcpClient As New System.Net.Sockets.TcpClient()
Dim sData As String
Shared mysocket As Socket
Delegate Sub SetTextbox(ByVal lsString As String)
Private Sub F_TCP_LIST_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tcp_listr_thread.Start()
End Sub
Sub TCPListener_thread()
Try
myTcpListener = New Sockets.TcpListener(IPAddress.Any, 1100)
myTcpListener.Start()
While True
mysocket = myTcpListener.AcceptSocket()
Dim bytes_rec As Byte() = New Byte(500) {}
Dim iData As Integer = mysocket.Receive(bytes_rec)
Dim i As Integer
For i = 0 To iData - 1
sData = sData & Convert.ToChar(bytes_rec(i))
Next
UpdateTextbox(mysocket.RemoteEndPoint.ToString & ":" & sData.ToString() & vbNewLine)
End While
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
Public Sub UpdateTextbox(ByVal lsString As String)
If txtshow.InvokeRequired Then
Dim lsa As New SetTextbox(AddressOf UpdateTextbox)
Me.Invoke(lsa, New Object() {lsString})
Else
txtshow.Text &= lsString
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If tcpClient.Connected = False Then
tcpClient.Connect(Trim(TxtIp.Text), 1100)
End If
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(Trim(txtmsg.Text))
networkStream.Write(sendBytes, 0, sendBytes.Length)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
End Class