Sockets Ports

bobbynad

Newcomer
Joined
Jun 28, 2003
Messages
20
Location
India
Hello

Please Let me know to connect the server using the free ports available at time from the client machine usings sockets.
 
you would need to catch a " port already in use " error ( then maybe loop back to your connecting code to try a different port )
to test for an open / used port you could do something along these lines...
Visual Basic:
        Try
            Dim ip As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
            Dim ipaddy As Net.IPAddress = ip.AddressList(0)
            Dim ep As New Net.IPEndPoint(ipaddy, 80)
            Dim listner As New Net.Sockets.TcpListener(ep)
            listner.Start()

        Catch ex As SocketException
            If ex.ErrorCode = 10048 Then '/// error number for Port already in use.
                MessageBox.Show("The port you attempted to connect to is already in use!")
            Else
                MessageBox.Show(ex.ErrorCode & "   " & ex.Message)
            End If
        End Try
 
Back
Top