Hi,
I'm trying to connect my chat client to my server, but I keep getting an error. I have chat server which is running in a machine which is published in internet and it has fixed IP address and it is running on port 5001. When my chat client tries to connect to that server then I am get the error. The line which is raising error as follows
ipAddr = IPAddress.Parse(txtIp.Text);
// Start a new TCP connections to the chat server
tcpServer = new TcpClient();
tcpServer.Connect(ipAddr, 5001);
When I run both my chat client & server in my local machine with local machine IP, it works.
Please tell me what I need to changes in code as a result my chat server & client both can communicate over the internet. Thanks
Fixed IP might not be enough if it goes first through some network of computers.
You might need to Forward Port if it is simple network, or if it is more complicated probably it has own direct line but have closed Ports so you have to open them through Firewall within system as well within possible hardware that can filter traffic.
It probably not the code issue at all.
As well when you create server socket, try to Bind it to IPAddress.Any, because if you bind it to specific device it might be source of problem as well.
Related
I have a synchronous TCP server and client application the works absolutely fine on two separate host machines.
What I'd like to know is what IP and port do I bind the server socket and the client socket to when the applications are both running on the same host machine.
I can't find any solid information on Google about this.
When I try and use my network IP which was 192.168.0.32 I get an error that says the Host actively refused the connection.
I cannot find any reasonable information about this error.
Can I listen and send on the same Port?
What IP address should I use to bind the server and the client, when both applications are running on the same machine?
Thanks for your time.
In order to run both client and server applications on the same host you should bind your server socket to localhost (you can actually write "localhost" it's a preserved word or 127.0.0.1 ) and address it from the client as well.
Localhost allways refers to the computer you work on.
If you'd like to access your server from a machine which is outer to your local network using your network ip you've mentioned, you should first search for "IP FORWARDING" option in your router settings and forward incomming requests to the machine where the server is running on.
Or (my favourite) use the great IP TUNNELING service of ngrok. You can find it here https://ngrok.com/
good luck.
So the answer to this question is that I must bind to my loop back address with separate ports for the client and the server !!
The IP address could be the loopback 127.0.0.1 for both, or your IP address, I don't see why it would not work.
The port on the other hand has to be the same for it to work, assuming the client application doesn't also listens to the port that you "bind" it to.
You have to tell the server on which port it should listen. The client then has to send data on the same port for the server to get the information.
This example should get you going: https://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
I have a server program and a client program. While developing the program I run the server and the client on the same machine for convenience. The server starts listening to incoming connections using these lines:
var listener = new TcpListener(IPAddress.Any, 7070);
listener.Start();
The client connects to the server using these lines (simplified):
var client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(IPAddress.Loopback, 7070);
I use IPAddress.Loopback because I run the programs on the same machine. But, knowing that the server and the client won't be necessarily run on the same machine in the future, I changed it to my public IP from http://icanhazip.com (IPAddress.Parse(...)). Because of that the client was unable to connect to the server on the same machine with the exception No connection could be made because the target machine actively refused it <my public ip:7070>.
I tried disabling my firewall but it's still not working. Why is the server refusing the connection? Didn't I specifically tell it to listen to all interfaces with IPAddress.Any?
Why does that happen and how do I fix it?
Here's an answer built from my comments on the question, which are hopefully correct:
Your public IP is provided by your ISP and is actually the address of your router. The router does network address translation (NAT) for outgoing connections from computers within your local network. These requests look to the Internet like they're all coming from one IP, and your router sends responses to the right local computer based on an address translation table. This works for outgoing connections but not incoming connections.
If something tries to open a TCP connection from the Internet to your router, the router has no idea what local computer it might be trying to connect to unless you specifically configure it to forward that traffic to a particular computer on your local network. That's where port forwarding comes in. If you haven't configured port forwarding, the router just says, "sorry, I'm not handling incoming requests on port 7070."
Is your development machine behind a router?
Network traffic sent to you via your public IP address reaches your router on a given port via a specific networking protocol. Your router needs to know where to send this traffic internally on your network. Traffic is coming from the Internet to your machine, and your router either cannot or will not forward the traffic to your computer's machine.
You don't notice this in your day-to-day life thanks to the power of Network Address Translation (NAT) and Universal Plug and Play (UPnP). Glossing over some details here, Network Address Translation allows traffic headers to be modified to route traffic from your public IP to your actual machine's IP on the network. When incoming traffic attempts to open a port for connectivity on your network, the router needs to be configured to forward that traffic appropriately. Universal Plug and Play is a protocol supported on many modern routers to allow software and devices to seamlessly route traffic without the need to forward ports.
This leaves you with two options:
For development purposes, access your router and forward the desired port to your development machine
For a more robust application, especially if you're going to be running this on different machines or different networks, consider adding UPnP support to your application while also understanding that UPnP may not be supported or enabled by some users, in which case port forwarding is still necessary.
I have C# TCP Server program which is running into VMWare and network type is NAT. There TCP server works as a http server, so that I can connect it using browser. Now when I am connecting it using this url http://localhost:33333/OrderDetails, it works without any problem. But when I am trying to connect it from outside of VMWare using this link http://1.39.37.243:33333/OrderDetails (1.39.37.243 is my PC's public ip), then its not working. I can't change the network type NAT, because my customer's office Network system is also NAT system. Where am I doing it wrong ? Any thoughts or suggestion will be highly appreciated.
This isn't working because the machine receiving the TCP packets has no reason to forward them to the VM. In other words, this isn't working because you haven't done anything to make it work.
You need to configure VMWare's NAT to forward these incoming TCP connections to the VM.
You could also have other problems:
The server might not be listening on the right IP address. For example, if the listening socket is bound to localhost, only localhost connections will work.
There might be something else in the path keeping the TCP packets from getting to the host. For example, if the host itself is behind NAT, port forwarding may need to be configured in the router.
Port forwarding won't work from inside the LAN unless the router supports hairpinning. If the host itself is behind NAT, test from outside the LAN after setting up port forwarding in the router.
I am working on a project where I am making my own SMTP server. This is pretty much working but there is a bit of a problem.
When I run my SMTP server on my development machine and have a php script to send the email or telnet from my local computer to the local smtp server it works fine and my program receives the SMTP messages and I send the relevant responses.
I am then copying the program on to a server and running my smtp server and when I telnet from the server to the local smtp server i.e. telnet localhost 25 it works.
If I then try and connect to my smtp server through a remote PC on the same network and attempt to telnet to it I get Connecting to 192.168.1.74 (this is the address of the server)...could not open connection to the host, on port 25: connect failed and if I run the php script http://192.168.1.74/send-mail.php it fails.
On the smtp server code when I bind the socket I have tried binding to 127.0.0.1 and 192.168.1.74 on port 25 but it makes no difference.
Why would this not be working from a remote PC making the connection to the server but locally it works fine.
Thanks for any help you can provide.
A couple stabs in the dark here, but:
Does the server have a firewall running? If so, have you made the appropriate exceptions for port 25?
Is the remote PC really on the same network (i.e. same subnet)? If you've got a router in between you'll have to forward the port appropriately.
Found out what the problem was, I was being dumb, when I set the network card to be static I forgot to add the default gateway. Now that I have done that it is working now as expected.
Thanks everyone for your help and suggestions.
Have anyone able to use visual studio socket server and client to work successfully over WAN network and not LAN?? I got it to work on LAN but not over the WAN.
By far the most common cause of problems when you go from LAN to WAN is firewall setup. If the server process is behind a NAT firewall, then you will need to connect to the public IP address assigned to you by your ISP, but it won't work until you set up a pinhole in the firewall's configuration that directs incoming connections to the appropriate internal IP/port.