0

I've made a short program where the user enters an IP address of a computer, and the program proceeds to check if the computer is online by trying to connect to it with port 80 (the assumption is that if a computer is turned on, it's connected to the internet).

Now, the problem is, everytime I click the Start button it does nothing for a few seconds and then it crashes.

Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Online_Checker
{
    public partial class Form1 : Form
    {
        int success;
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress.Text), 80);
            while (true)
            {
                success = 1;
                try { client.Connect(ep); }
                catch { status.BackColor = Color.Red; status.Text = "OFFLINE"; success = 0; }
                if (success == 1)
                {
                    status.BackColor = Color.Red;
                    status.Text = "ONLINE";
                    client.Close();
                }
                Thread.Sleep(5000);
            }
        }
    }
}

As you can see I've even made a delay of 5 seconds between each check to make sure it doesn't crash.

So where's the problem? And how can I solve it?

Kara
  • 6,115
  • 16
  • 50
  • 57
BlueRay101
  • 1,447
  • 2
  • 18
  • 29
  • When you say it crahes, what's the error? – Mark Dec 18 '13 at 16:30
  • Did you catch any exceptions? – OmniOwl Dec 18 '13 at 16:33
  • No error. I just click the start button, the start button freezes, and the whole form freezes. Even if it sometimes doesn't crash after a few seconds, it goes into "Not Responding" mode and I can't do anything inside it. Vipar, I caught the socket connection exception (to check if the computer is turned on/off), as you can see. – BlueRay101 Dec 18 '13 at 16:36
  • It is not Thread safe. – Quintium Dec 18 '13 at 16:36
  • On just a quick glance I noticed that you're trying to connect via UDP; only TCP implements connection handshakes. – OnoSendai Dec 18 '13 at 16:36
  • Even with TCP, 'trying to connect to it with port 80 (the assumption is that if a computer is turned on, it's connected to the internet).' Sure - as a CLIENT. Most boxes will not be running a server on port 80. – Martin James Dec 18 '13 at 16:46

2 Answers2

1

You can't do an infinite while/sleep loop on the UI thread: the UI thread is required to service the windows event queue - and if that is ignored it can't paint and is logged as "not responding".

Consider either a timer or a background worker instead.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

You're implementing an UDP socket. According to MSDN,

Since the UDP protocol is connectionless, the Connect method does not block.

There'll be no error to be Catched; thus, you enter an infinite loop on your main thread.

Suggested solutions:

  • Implement your test using the TCP protocol.
  • Move the check procedure to a secondary thread. The Connect method isn't asynchronous.

Now, you're trying to determine if a computer is alive or not. You may try to implement Ping. Here's a similar question on SO that have an answer from mef that addresses this issue:

What is the best way to check for Internet connectivity using .NET?

Hope it works for you.

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46