Client check if server socket is open C#

10/08/2014 21:45 abdeen#1
i want the client check if the server socket is online or not , and if it's not it's wait 20 sec then check again .. i tried to figure it out by making this code but still doesn't make what i want ... i think there is a good solution better than the one i made .

PHP Code:
        public bool Connect(string IPint Port)
        {
            
int tries 0;
            
IPAddress ipAddress IPAddress.Parse(IP);
            
IPEndPoint ipEndPoint = new IPEndPoint(ipAddressPort);
            while (
tries == && !Connection.Connected)
            {
                try
                {
                    
Connection.BeginConnect(ipEndPoint, new AsyncCallback(AsyncConnect), null);
                if (
Connection.Connected)
                {
                    return 
true;
                }
                }
                catch
                {
                    
System.Threading.Thread.Sleep(20000);
                }
            }
            return 
false;
        } 
Edit : i have done it after editing my code to ...

PHP Code:
public bool Connect(string IPint Port)
        {
            
int tries 0;
            
IPAddress ipAddress IPAddress.Parse(IP);
            
IPEndPoint ipEndPoint = new IPEndPoint(ipAddressPort);
            while (
tries == && !Connection.Connected)
            {
                try
                {
                    
HashSocket.Connect(ipEndPoint);
                    if (
HashSocket.Connected)
                    {
                        
Connection.BeginConnect(ipEndPoint, new AsyncCallback(AsyncConnect), null);
                        return 
true;
                        if (
Connection.Connected)
                        {
                            return 
true;
                        }
                    }
                }
                catch
                {
                    
System.Threading.Thread.Sleep(20000);
                }
            }
            return 
false;
        } 
it's works for me , thanks anyway...;)
10/09/2014 18:50 Xio.#2
PHP Code:
TcpClient tcpclnt = new TcpClient();
    
try
{
        
tcpclnt.Connect("172.21.5.99",9958);
}
catch
{
//Server Offline

10/12/2014 07:45 Super Aids#3
Quote:
Originally Posted by Xio. View Post
PHP Code:
TcpClient tcpclnt = new TcpClient();
    
try
{
        
tcpclnt.Connect("172.21.5.99",9958);
}
catch
{
//Server Offline

Code:
static bool TryConnect(this Socket soc, IPEndPoint ipe) {
	try {
		soc.Connect(ipe);
		return true;
	} catch { return false; }
}
Code:
if (soc.TryConnect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9988))) {
    // use soc as client socket ...
}
else {
    // soc did not connect ....
}
10/18/2014 00:38 abdeen#4
Quote:
Originally Posted by Super Aids View Post
Code:
static bool TryConnect(this Socket soc, IPEndPoint ipe) {
	try {
		soc.Connect(ipe);
		return true;
	} catch { return false; }
}
Code:
if (soc.TryConnect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9988))) {
    // use soc as client socket ...
}
else {
    // soc did not connect ....
}
i want it keep trying till server socket is open and client connected !!
10/20/2014 14:19 Xio.#5
So put it in a loop, gosh...
10/21/2014 09:02 Super Aids#6
Jesus fucking christ.
Code:
while (!soc.TryConnect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9988))) {
    System.Threading.Thread.Sleep(2000);
}
// use soc as client socket ...