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.