How to open Ports through C#? - c#

I'm kind of a beginner at programming, so I don't know what I'm about to ask is even possible. I want to make a TCP/IP connection to a server over WAN.
Both Client and Server programs were made by me they work just fine in a LAN. I'm now trying over a WAN. I know the server IP Address and the Port he's listening from, but as a client I can't make the connection. I supposed it's because the Port is not actually open/available.
How can I make this to happen in the server side? Is there a way for me to start the server (maybe as administrator) and for it to open the Port by itself? Is this a router or Firewall issue?
If I wasn't clear enough, feel free to ask.
Cheers.
PS: Let me put it this way:
If I make the connection in a LAN, it works. I set it to the local IP Address (192.168....) and choose whatever port I want and it works just fine. Once I put my public IP provided my ISP, it doesn't. I set the firewall router to accept everything and put a new rule in windows firewall to open port 100. What changed?

Please check the following list,
Server Firewall rules to exclude the desired port
Router Firewall to exclude the desired port
Check any installed security software blocking your desired port
Last and crucial! Open router interface and configure your NAT settings so the packets running via the desired port are served to your local server IP
If everyhting is fine and your ISP is not blocking the desired port then you will have no problem.
You can also visit canyouseeme.org in order to check that server listening alright!
Kind Regards,
KB

Related

Cannot connect through tcp remotely

I’m developing an app that connects on port 9050 through the public ip of my server. It works fine on lan but if I’m connected on a different network it fails what can I do to fix this?
first try from Command Prompt this command: telnet ip 9050
if it working means the port 9050 between your pc and the server already opened , otherwise you should check why and where the port is blocked (may be firewall in this case you need to create rule in firewall to pass ).
I don't have any code to go by and I know nothing about your network configuration, but I'll tell you what I generally do when debugging a network-enabled app. First, look at your firewall rules. This will obviously vary depending on what OS you're using but on Windows 10 application-specific permissions are located in Control Panel > System and Security > Windows Defender Firewall > Advanced Settings.
After making sure your app can get through your devices' firewall, check the router settings next. Your packets may not getting by whatever inbuilt security protocols are in place. Since your program works with LAN but not over the Internet, this is a distinct possibility. Also, depending on your network configuration, port forwarding may be necessary; if you're sending data via a public IP address, the router needs some way to know which device on the local network needs to be receiving it. Additionally, if you're trying to connect to your server from the same network you're hosting it on, make sure you use the IP loopback address, which I believe is always 127.0.0.1, or in C# IPAddress.Loopback. Public addresses often won't work when connecting to a local host.
If you're still having problems, I recommend using Wireshark. It's an advanced diagnostics tool that tells you exactly what is being sent and received over your network, and can help pinpoint otherwise hard to spot issues.

c# Socket.RemoteEndPoint shows different port that I'm using to connect

I've developed a basic console application that will work as server-client. My general workflow will be like, Server starts clients connect to server and once a client send something the server will receive it and send it to ALL clients. Currently I can do this with my codes but my question is when I send back I've tested Socket.RemoteEndPoint and I saw ports like 65xxx. Like my first client had 65020, second 65021 and so go on. I wonder why this happen since I connect with using 9001 port and listen to 9001 port on my computer. I've done a little bit research but couldn't find a solution to my question. Why do I get these port numbers when I use Socket.RemoteEndPoint? It's okay for me since I don't use firewall or anything while I'm testing to even if the clients connects from another ports it's fine by me as long as its works but what if I have a firewall and I open ONLY 9001 port? Will my client-server based programs work then?
So far as the server/firewall on that machine is concerned, what normally matters is the server port. You wouldn't normally apply any filtering based on the client's port, which as I said in a comment, will normally be an ephemeral port. Under most circumstances, client code will make no effort to specify the local port or IP address and will let the OS pick appropriate values.
The Socket class's LocalEndPoint and RemoteEndPoint use "local" and "remote" to mean "from the perspective of the machine on which this code is running", not "who initiated the connection vs who was connected to".
So on the server, LocalEndPoint will give you information about the server end of the connection and RemoteEndPoint will give you information about the client end of the connection. Irrespective of who initiated the connection.

How can make my c# messenger works on the internet not only on the local networks?

I'm working on a simple c# messenger and its works on the local network only. Does somebody know what should i do to make it works on the internet?
The solution contains the server project that listening on a certain port, and a client project connect to the server with that port.
Your clients need to be able to communicate with the server, which means that either:
You need to host the server application on a computer that is directly connected to the internet.
You need to setup port forwarding on the router that controls internet access for your network.
In either case the clients will need to know the IP address to connect to, and any firewall interactions need to be considered. This may also include ISP firewalls, as some ISPs limit inbound connections to subscriber connections.
I won't go into the specifics of setting up port forwarding on a router... there are plenty of examples, and every router is different. Google will help you with this if necessary.
Ideally you should have a static IP address for this, or some method for the clients to locate the IP address that your server is hosted on. If you must run from a dynamic IP address (such as some ISPs still use for subscribers) then one of the Dynamic DNS options might work for you.
In-order to make it work on Internet a simple answer is you need to enable ports which is used for communication.Generally the ports will be blocked by the Companies firewall for security reasons.So contact your IT dept to enable the port.
Or Use common port like 80.

Unable to connect to local machine IP address using internet IP address

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.

Communication over WAN

I want to create app client-server in C# but not only in LAN. There it's easy: TcpListener, TcpClient etc.
I want to make sth like in this e.g.
On my comp is server that's waiting for a connection.
Someone in another network has client. He begin connection, but...where...what is a IP of server?
He see only ip of router.
Any ideas? :)
EDIT
Big problem is also double-NAT
You still use the same TcpListener and TcpClient (if you want to make it that low level).
The technologies for communicating over a WAN are the same for communicating over a LAN. The difficult part is getting the networks in between the client and server to allow you to use the necessary ports and protocols.
You question doesn't explain the whole scenario.. but with my understanding of the problem I can suggest the following answer:
Depending on the Target customer base's locations (support for Corporate networks or NOT), you can use various routing options like UPnP, STUNT or IPv6, or some other NAT traversal options, so that you can inform the client about where the listener is. There should always be a central registry server to which the Listener would inform its whereabouts and the mode of the communications permitted in its environments. Use of an XMPP server would be an easy option for such purposes, which solves most of such issues. Once the client queries about the location of the listener from the discovery server, it can directly connect to the Listener.
Sounds like you just want to set up port forwarding on your router. When an incoming connection is made on the specific port a client connects to, the router should redirect the connection to the machine you specify on the LAN. (Usually an IP like 192.168.x.x). This should also be the IP address your TcpListener is listening for connections on.
You can try portforward.com if you need help setting it up for a particular router.

Categories

Resources