Windows Develop Bookmark and Share   
 index > Windows Forms General > Tcp Listener problm
 

Tcp Listener problm

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
Jeyanth.s  Wednesday, September 16, 2009 5:06 PM
AcceptSocket() should not be inside the loop.

Hans Passant.
  • Marked As Answer byJeyanth.s Thursday, September 24, 2009 11:09 AM
  •  
nobugz  Wednesday, September 16, 2009 7:31 PM
AcceptSocket() should not be inside the loop.

Hans Passant.
  • Marked As Answer byJeyanth.s Thursday, September 24, 2009 11:09 AM
  •  
nobugz  Wednesday, September 16, 2009 7:31 PM

You can use google to search for other answers

Custom Search

More Threads

• Pass variable between forms
• Handling WM_CHAR in html view
• some WebClient questions
• Intercepting keydown "Delete"
• strange tabindex behaivour
• Script Error For Webbrowser Control
• How to draw on top of the form
• How to automatically resize toolstripcombobox when form is resized?
• How to use a flash object
• help me