-1

How would I check if a tcp port is available in a thread? I tried using a background worker thread, and it worked, but updated rather slowly. I want to report progress to my UI thread. Is this a correct way?

    BackgroundWorker authStatus = new BackgroundWorker {WorkerReportsProgress = true};
    authStatus.DoWork += AuthStatusWork;
    authStatus.ProgressChanged += AuthStatusProgressChanged;

    public void AuthStatusWork(object sender, EventArgs e)
    {
        var thread = sender as BackgroundWorker;

        using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            socket.SendTimeout = 3;
            while (true)
            {
                try
                {
                    socket.Connect(auth[0], Convert.ToInt32(auth[1]));
                    if (thread != null) thread.ReportProgress(1);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode != SocketError.ConnectionRefused &&
                        ex.SocketErrorCode != SocketError.TimedOut) continue;
                    if (thread != null) thread.ReportProgress(0);
                }
            }
        }
    }

    public void AuthStatusProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        switch (e.ProgressPercentage)
        {
            case 0:
                AuthStatus.Foreground = Brushes.Red;
                AuthStatus.Content = "Offline";
                break;
            case 1:
                AuthStatus.Foreground = Brushes.Green;
                AuthStatus.Content = "Online";
                break;
        }
    }
  • Have you seen this question? http://stackoverflow.com/questions/570098/in-c-how-to-check-if-a-tcp-port-is-available – fassetar Aug 16 '13 at 03:11
  • I'm not trying to check my own ipaddress though. – Tristan McPherson Aug 16 '13 at 03:14
  • I'm trying to check a remote server's port to see whether or not it is available to connect to. – Tristan McPherson Aug 16 '13 at 03:22
  • @TristanMcPherson you should reword your question so it doesn't sound like you're trying to check a local port. "available" usually means to bind to, not to connect to. All you want to do is an async connect. There's tons of resources about how to do that. You don't even need (or want) a thread to do this correctly. – xaxxon Aug 16 '13 at 03:46
  • @xaxxon Even if I want to loop? – Tristan McPherson Aug 16 '13 at 03:48
  • @TristanMcPherson I'm not a c# person, but in general, the best way to do anything to an arbitrarily large number of sockets is asynchronous calls -- in this case you're basically writing a port scanner and you want to use asynchronous connects. – xaxxon Aug 16 '13 at 03:50
  • I cannot, for the life of me, figure out how to do this. – Tristan McPherson Aug 16 '13 at 04:52

2 Answers2

2

Use asynchronous connections, as demonstrated here here

This will allow you to attempt to connect to as many remote tcp ports as you want and let you know when it succeeds or fails. You don't have to worry about how many threads you've started or running out of resources (other than sockets, which is a problem no matter what approach you take).

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • I have read the article, but cannot figure out when the connection is actually made, and when to signal to my UI thread that it has been. – Tristan McPherson Aug 16 '13 at 04:21
0

I'm trying to check a remote server's port to see whether or not it is available to connect to.

So try to connect to it. These games of trying to predict the future in some way are pointless. Try the connection and handle the error. This applies to any resource: the best way to see if any resource is available is to try to use it.

user207421
  • 305,947
  • 44
  • 307
  • 483