C# script to check server status online/offline

04/26/2011 20:30 S/W#1
I am coding my own auto patch and and i wanna display server status in win form any one haw simple code how cheek server status like Online Offline?
04/26/2011 20:41 |NeoX#2
are you serious? How do you plan to write a patcher if you cant connect to a specific port and say if it responds or not?

Good luck, you will need it.

PHP Code:

Socket sock 
= new Socket(AddressFamily.InterNetworkSocketType.StreamProtocolType.Tcp);
    
sock.Connect(IPPORT);
    if (
sock.Connected == true)
        
//ONLINE
    
else
       
//OFFLINE
    
sock.Close(); 
04/26/2011 20:52 S/W#3
Quote:
Originally Posted by |NeoX View Post
are you serious? How do you plan to write a patcher if you cant connect to a specific port and say if it responds or not?

Good luck, you will need it.

PHP Code:

Socket sock 
= new Socket(AddressFamily.InterNetworkSocketType.StreamProtocolType.Tcp);
    
sock.Connect(IPPORT);
    if (
sock.Connected == true)
        
//ONLINE
    
else
       
//OFFLINE
    
sock.Close(); 
My Teacher [Only registered and activated users can see links. Click Here To Register...]
04/26/2011 22:08 -impulse-#4
Quote:
Originally Posted by S/W View Post
My Teacher [Only registered and activated users can see links. Click Here To Register...]
[Only registered and activated users can see links. Click Here To Register...]
04/26/2011 22:28 _Emme_#5
To check if it's on (or actually, if it's in the state to accept connections), you basically try to connect to it and read from it's result. You could use the Socket class (as provided above) or a TcpClient.

Code:
static string IP = "79.143.177.32";
        static int Port = 9959;
        static void Main(string[] args)
        {
            Console.WriteLine("Server on port {0} is {1}",Port, Online() ? "online" : "offline");
        }
        static bool Online()
        {
            TcpClient client = new TcpClient();
            try { new TcpClient().Connect(IP,Port); return true; }
            catch { return false; }
        }
04/26/2011 23:21 InfamousNoone#6
Quote:
Originally Posted by EmmeTheCoder View Post
To check if it's on (or actually, if it's in the state to accept connections), you basically try to connect to it and read from it's result. You could use the Socket class (as provided above) or a TcpClient.

Code:
static string IP = "79.143.177.32";
        static int Port = 9959;
        static void Main(string[] args)
        {
            Console.WriteLine("Server on port {0} is {1}",Port, Online() ? "online" : "offline");
        }
        static bool Online()
        {
            TcpClient client = new TcpClient();
            try { new TcpClient().Connect(IP,Port); return true; }
            catch { return false; }
        }
Should probably disconnect the socket before returning.
04/27/2011 00:08 |NeoX#7
I already gave an example how to do it. Imo, this thread should be closed.