i was trying to connect two computers..
i used synchronous server and client with the help of some socket programming..
but whenever i try to connect to the server which is on a remote computer an exception is thrown whereas it works fine if both server and client are on the same machine..

here is the code

server side code:

namespace Consoleserver
{
    class Program
    {
        static void Main(string[] args)
        {
            
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp );

       
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);

           
            while (true)
            {
                Console.WriteLine("Waiting for a connection...");
           
                Socket handler = listener.Accept();
                Console.WriteLine("connected");
            }
            
        }
        catch { Console.WriteLine("error"); }



        }
    }
}

client side code:

namespace consoleclient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                IPHostEntry ipHostInfo = Dns.Resolve("59.178.131.180");
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);


                Socket sender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);


                try
                {
                    sender.Connect(remoteEP);

                    Console.WriteLine("Socket connected to {0}",
                        sender.RemoteEndPoint.ToString());
                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                    Console.Read();
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                    Console.Read();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                    Console.Read();
                }

            }
            catch
            { Console.WriteLine("could not connect A"); }
        }
    }
}

The exception that is being thrown is :
System.Net.Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond 122 .162.98.164:11000
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sock ets.Socket.Connect(EndPoint remoteEP)
at consoleclient.Program.Main(String[] args) in C:\Users\mukul\Desktop\consol
eclient\consoleclient\Program.

Assuming that your IP address and port are correct, have you ensured that the server is not blocking remote connections through that IP/port?

Check firewall and configuration settings on the server.

This sounds like a firewall issue. Your listener could also be set to listen on 127.0.0.1 and not all available IPs.

I have very little faith in opening a socket like you have in your example, but i see people do it all the time:

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

If you're using TCP you could use a TCPListener instead of a socket:

TcpListener listener = new TcpListener(IPAddress.Any, port);

Edited 7 Years Ago by sknake: n/a

This article has been dead for over six months. Start a new discussion instead.