Results 1 to 4 of 4

Thread: How to end client connection properly? [VB.NET]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Nov 2014
    Posts
    9

    How to end client connection properly? [VB.NET]

    how do you end client connection properly so that the port used previously to connect another computer can be used again? I used the timer so after every 2 minutes the server will get a statement from the client. My client side connects during the first time and it continues to listen but does not connect again. I think I did not close the connection properly that is why the port on the client side is still used. How do you end connection properly?

    Server side


    Code:
    Private itemCount as Integer = 0
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            'items in my listview
            For Each item In Me.Computers.Items
                Dim aa As String = item.Text
                GetStatement(aa, itemCount)
                itemCount = itemCount + 1
    
            Next
            itemCount = 0
        End Sub
        
       Private Sub GetStatement(ByVal s As String, ByVal index As Integer)
            
            Try
                clientIP1 = System.Net.Dns.GetHostByName(s).AddressList(0).ToString
    
                ''CLIENT
                Try
                    client1 = New TcpClient(clientIP1, connectPort)
                    client1.Close()
                    server = New TcpListener(serverIP, serverPort)
                    server.Start()
                    isListening = True
    
                    commThread = New Thread(New ThreadStart(Sub() Me.Listening(index)))
                    commThread.Start()
                Catch ex As Exception
                   
                    stmnt = " "
                    Try
                        Dim del As UpdateDelegate2 = AddressOf AddListBoxSubItem
                     
                        Me.Invoke(del, index, stmnt)
                    Catch x As Exception
    
                    End Try
                End Try
    
    
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
        Private Sub Listening(s As Integer)
            'CREATE LISTENER LOOP
    
            Try
                While (True)
                    Try
                        listenClient = server.AcceptTcpClient
                        clientData = New StreamReader(listenClient.GetStream)
                        statement = clientData.ReadLine
    
                        Try
                            Dim del As UpdateDelegate2 = AddressOf AddListBoxSubItem
                            Me.Invoke(del, s, statement)
                        Catch ex As Exception
    
                        End Try
                        listenClient.Close()
                        clientData.Close()
    
                    Catch ex As Exception
                        MsgBox("no connection")
                        Exit While
                    End Try
    
                End While
                'REDUCE CPU USAGE
                Thread.Sleep(100)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    Client
    Code:
    Private Sub ClientPart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          
    
            client = New TcpListener(IPAddress.Any, clientPort)
            serverIP = IPAddress.Any.ToString
            client.Start()
    
            commThread = New Thread(New ThreadStart(AddressOf Listening))
            commThread.Start()
        End Sub
    Private Sub Listening()
            'CREATE LISTENER LOOP
            Do Until isListening = False
    
                'ACCEPT INCOMING CONNECTIONS
                If client.Pending = True Then
                    server = client.AcceptTcpClient
    
                    Dim myEndPoint As Net.IPEndPoint = server.Client.RemoteEndPoint
                    If Not myEndPoint Is Nothing Then
                        serverIP = myEndPoint.Address.ToString
                    End If
                    If server.Connected Then
                        Try
                            'CLIENT
    
                            server.Close()
                            serverSend = New TcpClient(serverIP, connectionPort)
                            dataStream = New StreamWriter(serverSend.GetStream)
                            Send(statement)
                            serverSend.Close()
    
                        Catch ex As Exception
                            MsgBox(ex.Message)
                            Exit Sub
                        End Try
                    End If
                End If
    
                'REDUCE CPU USAGE
                Thread.Sleep(100)
            Loop
           
        End Sub
    
        Public Sub Send(Data As String)
            dataStream.Write(Data & vbCrLf)
            dataStream.Flush()
        End Sub

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: How to end client connection properly? [VB.NET]

    You do not accept a connection on the listener and use that as your client. You create another client to accept and communicate with the remote client.

    The listener should continue to listen for connections.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3

    Thread Starter
    Registered User
    Join Date
    Nov 2014
    Posts
    9

    Re: How to end client connection properly? [VB.NET]

    I thought once you use it to accept a client. it would not listen for another since the port is already used

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: How to end client connection properly? [VB.NET]

    A TCP server listens on a single port. The server accepts the connection on another client, which binds to another port, leaving the server continually listening on a single port.

    jmcilhinney has a good example of a tcp client server.

    http://www.vbforums.com/showthread.p...-amp-TcpClient
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width