Hello DaniWeb,
This is my first topic here at DaniWeb. I have always been a great fan of this forum mostly for C# solutions.
Iam a 3rd year student IT.

Now I have a problem. Iam trying to make a application that checks if IP + Port is online.( it's a WAN IP )
The thought behind is a downloadable application for a friend of mine who runs a MMO Server Status site.

I kindly like to hear how can I manage to do so.

Till now I have tried:
- Socket, Problem with socket is that I cannot make more than one connection to a WAN IP.
- Ping, Doens't work because ping doesn't do IP + Port.
- Webrequest, Haven't really tried because I thought this has the least chance of success.

To clear things up this is what it supposed to do.

I have a list of 16 WAN IP + port numbers that each have to be checked for connectivity.
If there is no connectivity then label will say so.
Timer starts after check of all 16 WAN IP's and after timer is finished run the check again.

Hope this is enough information.

Big Thanks in advance,

Jerompie

P.s. If there are parts that are to confusing for you, post below and iam happy to explain it again.
C# Checking connectivity server status

Socket, Problem with socket is that I cannot make more than one connection to a WAN IP

With two sockets you get two connections. Anyway, with sockets you can make a port scanner. With googling you'll find a plenty of examples. Here's one VB Port Scanner in VB but easily converted to C#. And here's one more link about "pinging a port" Ping IpAddress with Port number? :D

HTH

Thank you for your reply Teme64.
I have 16 Ip's to check, I think theres a better way than to make 16 socket connections.
Also I don't want to make a port scanner. I just want a reply from the Socket that its still alive.

Here are a few IP + Ports for example:
CH1:Game.exe:5888 TCP pc:knetd 109.234.73.20:8585 ESTABLISHED
CH2:Game.exe:5888 TCP pc:2052 109.234.73.21:8585 ESTABLISHED
CH3:Game.exe:5888 TCP pc:1971 109.234.73.21:8586 ESTABLISHED

I will investigate the articles given by you.

EDIT: Link to VB Port scanner is broken. I have google skills. :)

bump. I still haven't figured it out.

I took from the second link in my previous post a snippet " ReadOnly Property ScanPort(ByVal IP As System.Net.IPAddress, ByVal Port As Integer) As Boolean " and converted it to C# function:

public bool ScanPort(IPAddress IP, int Port)
{
    TcpClient aSocket;

    aSocket = new TcpClient();
    try
    {
        aSocket.Connect(IP, Port);
    }
    catch(Exception ex)
    {
        // Something went wrong
        return false;
    }
    if(aSocket.Connected)
    {
        // Got connected to Address+Port
        aSocket.Close();
        aSocket = null;
        return true;
    }
    else
    {
        // Not connected
        aSocket.Close();
        aSocket = null;
        return false;
    }
}

and tested with

private void button1_Click(object sender, EventArgs e)
{
    string IP;
    int Port;
    bool connected;

    IP = textBox1.Text;
    int.TryParse(textBox2.Text, out Port);
    connected = ScanPort(IPAddress.Parse(IP), Port);
    MessageBox.Show(IP + ":" + Port.ToString() + " is " + (connected ? "connected" : "not connected"));
}

The code doesn't work "as is" for your special case, but has the basic technique you need.

Since you have 16 IPs+ports, you could use Threading or a Background Worker. It depends on, how often (and how fast) you have to check that the connection(s) is alive.

HTH

commented: Simple yet elegant!!! +10

Thanks this is the help is was looking for.
I'll try this code.

Works like a sharm. I want to thank you very much Teme64!
Big thumbs up ;)

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.