Client Server Socket C# communication - c#

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.

Related

Putty can connect to server but client cannot

I have a command line application that listens on an address ie. 192.168.1.89:8001. I also have a c# client application that uses a socket to communicate with the server. Both of these applications are running on the same machine and I am not able connect to the server.
When I try to input 192.168.1.89:8001 as the address for the client I get a "No connection could be made because the target machine actively refused it" error. However when I used putty to make a raw connection to that same address I was successfully able to communicate with the server. Any ideas why putty is able to connect but not the client?
The client is creating the socket like this
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Parse(HostIP), Port);.

Connect Windows Form Client With Tcp Server on Asp.Net Mvc Web

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.

How to correctly use sockets over the internet in C#?

my Sockets are correctly working in my local network, but when sending the program to a friend, he cant connect to me.
I host the server with this Code:
Socket listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.IPv6Any, 30000));
listener.Listen(500);
And he connects to this server using my external IP address (gotten from http://whatismyip.org/) and my specified port (30000) with this code:
Socket inOut = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
inOut.Connect(new IPEndPoint(IPAddress.Parse(textBox5.Text), 30000));
When using cmd netstat -a i cant find a listening entry with port 30000, so i guess that my server has the wrong settings or i need to specify something different, but i already opened the port in my router (30000).
As-is, the code you have shown should work, IF all of these conditions are met:
your ISP has assigned your Internet connection an IPv6 address. Ask your ISP. Many ISPs don't do this yet for home users. If your ISP has not, your client will have to use AddressFamily.InterNetwork (IPv4) instead. Depending on what kind of IP your client wants to connect to, it needs to create an appropriate Socket type. An IPv4 socket can't connect to an IPv6 IP address, and an IPv6 socket can't connect to an IPv4 address (unless it is configured to be a dual-stack socket).
your ISP is not blocking inbound TCP connections to your Internet IP. Many ISPs do, so they can charge customers extra for hosting their own servers. Most home Internet users don't need to run their own servers. Ask your ISP if this feature is enabled for your Internet account.
your router is configured to forward inbound packets received by the router's public IP address on port 30000 to your server's LAN IPv6 address on port 30000. If your router does not have a public IPv6 address (because the ISP hasn't assigned one), you will have to use AddressFamily.InterNetwork on the client side. Because a router is sitting between the client and server, they can use different IP versions to communicate with the router.
your server code is calling listener.Accept() or Listener. BeginAccept() after listener.Listen() exits without error. This is a given if your code already works on the local LAN, but I'm throwing it in anyway for good measure.
Try doing AddressFamily.InterNetwork instead of AddressFamily.InterNetworkV6

TcpClient- how to connect to an external IP address?

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.

Why IPAddress.Any needs to connect to remote host?

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.

Categories

Resources