I'm building one client server application in c# where server is remote host. I've router and firewall in my network.
My client side code is
hostSocket = new TcpClient();
hostSocket.Connect(serverIp, serverPort);
And My server side code was
eqListner = new TcpListener(IPAddress.Parse(eqIp), eqPort);
in this scenario I'm able to connect client on same pc by giving ip 127.0.0.1 but cannot connect when I ran server on another pc in my network.
Then I've changed my server side code by following:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, eqPort);
TcpListener eqListner = new TcpListener(ipLocalEndPoint);
But still the result is same. Then I changed my server side code again by this:
eqListner = new TcpListener(IPAddress.Any, eqPort);
And this works perfectly fine. I've read msdn for IPAddress.Any and found that this property set server to listen for client activity on all network interfaces.
My question is why IPAddress.Any needs to connect to remote host? and other functions cannot connects?
Thanks in advance....
in this scenario I'm able to connect client on same pc by giving ip 127.0.0.1 but cannot connect when I ran server on another pc in my network
127.0.0.1 is the loopback address that is always the local host. When the service and the client are only on the same machine this can be convenient.
You are better off coding services to listen on all interfaces, this way should the server change address your application doesn't need to be updated or restarted.
The problem may be in the first line.
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
It will get the first ip of the host were it is running. That may be a IPv6 or localhost address.
If you want to listen in a specific address, it will be better to add it to project settings. It will be stored in app.config, and you can change it without recompile.
Related
I am working on client, server project according to this project the client wait for the server to come online and if the server is online connect with it and then wait for commands to act on.
I would like to implement server on Web so that the Dns ip can be resolve easily into ip address using
IPHostEntry hostEntry;
hostEntry= Dns.GetHostEntry(host);
//you might get more than one ip for a hostname since
//DNS supports more than one record
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.IP);
s.Connect(ip, 80);
}
But Here are some issue when connecting client with server on different networking scenario. That's why I would like to shift the server on web application because a Url is accessible by all in any networking scenario.
Issue When Implementing on Windows Application(Form)
The client program when run under the router (Infrastructure mode) cannot be able to connect with server if server using dynamic IP.
Have to Port Forwarding on client side.
Please suggest me any method (if exist) to make connection between client/server without having these issues.
I have wrote a group chat using C#, using client socket and server socket.
the communication between the client and the server works fine when I run the programs (both server and client) in my own pc using VS 2017.
When I run the Client program in a laptop and the server in my own pc (still using VS 2017, although I don't think this is matter) the client doesn't connect to the server.
my question is how I connect the server and the client outside of the localhost?
I will add the functions from the server and client side the responsible for connecting each other.
function in the server code that start up the server:
public static void ServerUp()
{
IPAddress ipAdd = IPAddress.Parse("127.0.0.1");
TcpListener myListener = new TcpListener(ipAdd, 8001);
myListener.Start();
Console.WriteLine("The server running at port: " + myListener.LocalEndpoint);
users = new List<ClientSocket>();
}
function in the client code that connect to server:
public static void ConnectToServer()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Connecting...");
IPEndPoint ipAdd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
socket.Connect(ipAdd);
Console.WriteLine("Connected");
Console.WriteLine(new string('-',Console.WindowWidth));
}
If you run the client on another computer, it crosses the network.
Unless you opened a firewall port for your server somehow, the windows integrated firewall will block all access from outside sources.
Simple as that.
Go to the advanced firewall settings and open the port for the server.
Change the IPs. 127.0.0.1 is used for localhost. Use your local network IPs e.g. 192.168.1.10 etc etc. Also have the required port (8001) open on the firewall of the server machine.
You need the server to listen on the real ip address of your pc instead of 127.0.0.1, and also in the client, on your laptop, you have to use the ip address of your computer instead of 127.0.0.1.
127.0.0.1 is the local network address, which is not bound to your network connection and only reachable on the same computer, not on the network.
Further your windows firewall might block the incoming connection - either add a inbound tcp rule for port 8001 an your computer or temporarily disable the firewall on your computer.
127.0.0.1 is local IP, use the IP assigned by your network's DHCP server.
I have been trying to setup a basic scenario where a TcpClient connects to a TcpListener on the same machine, but so far it only works for 127.0.0.1 and 192.168.X.Y. Here is my code:
var server = new TcpListener(IPAddress.Any, 51328);
server.Start();
var client = new TcpClient();
client.ConnectAsync(IPAddress.Parse("address from whatismyip"), 51328);
server.AcceptTcpClient(); // hangs indefinitely here
Console.ReadKey();
I got my external address from whatismyip but I'm not sure if I'm doing it correctly. Is something incorrect with my procedure?
I am assuming you are trying to connect over the internet? If you are connected via some Internet provider like COMCAST then you probably have a cable modem? To do this sort of thing you are going to need to setup PORT forwarding on a router.
The internet only see's your cable modem, all your requests to the internet go out as your cable modems IP, the router is able to "route" packets to and from your 192.168.x.x address on your behalf, so in order to have your 192.168 machine be able to listen and accept you must tell your router to forward any messages on port 51328 to your machine.
So your code would listen to port 51328 using the 192.168.x.xx address, then you setup the router. To test it you would connect using the public internet address that is assigned to your modem.
I've spent the last month writing a multiplayer game. I have only been testing it on one machine, using 127.0.01:9051 as the IPEndPoint.
I changed the IP address to my WAN IP, configured port forwarding on my router, configured my software firewall etc... But, it doesn't connect.
I have checked if the port is open using this site. Result: the port is really open.
Also, when I check the port from that website, my server receives packets just fine; however, when I connect from my own machine... it doesn't receive anything.
I've broken everything down to the basics to make sure it wasn't a problem with my code.
This basic code does not work:
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("XXX.XXX.XXX.XXX"), 9051);
TcpClient client = new TcpClient();
client.Connect(iep);
Any ideas are much appreciated.
If you are attempting to connect to the IP that is Forwarded (outsideIP) to the same machine (insideIP), it won't work. There are very few enterprise firewalls and no consumer devices (I know of) that will route a packet from the inside out and translate it back in.
[Internet] -- outsideIP[Router/Firewall]insideIP1 -- insideIP2[Computer]
In this case, packets from insideIP(X) will not be able to connect to outsideIP.
Try to see if you can telnet to your wan IP on port 9051. Routers don't really like it when you connect to their wan IP from within the network itself.
You could use Dyndns or some other service for the outside world to connect to your server and put the same name into your host file that resolves to 127.0.0.1 so that you can connect internally.
Is your client and your server (both the same machine in your failing test) using the same endpoint ip/port? You can only bind a port once at a time. Try using two ports one for server in one for server out (clinet in). That way they will not clash. Use Telnet to check you can connect to the IP/Ports you are using and Netstat to check they are not in use already.
I've a server with several IP Addresses assigned to the network adapter.
On that server is a client app to connect to another server app via TCPClient. For all outgoing communications my servers default IP address is being used, however for this one application I'd like the outgoing communication to be send out on another local IP address.
Is it possible when communicating out to specify another locally assigned IP?
I'm trying to make the remote server app think it's from another IP so it will pass through firewalls etc....
Thanks in advance
You can use the constructor of TcpClient that accepts a local endpoint address:
TcpClient c=new TcpClient(new System.Net.IPEndPoint(...));
For example:
TcpClient c=new TcpClient(new IPEndPoint(IPAddress.Parse("192.168.1.1"), 0);
Reference: TcpClient Constructor (IPEndPoint)