TCP Server IP Client Connection Issue - c#

OK so I've just started messing with TCP using c#, and I've successfully set up a server that i can send a 'Hello World' Message to, anyway I've been doing this locally (because both laptops are connected to the same router) i just use the 192.168 number to connect. but The whole purpose of it is to work over the internet, and the routers ip address is obviously the same for both computers, if i type the routers IP address it doesn't work, and if i type the 192.168 number that definitely won't work over the internet... So what IP do i use, or what is a better solution?
here's the line of code if it matters
var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("192.168.1.142", 10085));
Where 192.168.1.142 is the local ip of the laptop with the server started on it
and 10085 is the port.

You have to configure your router to forward any incoming connection to the port 10085 to you local IP adress. Then anyone will be able to connect using your external IP adress.
Each router has it's own configuration system so you have to search "port forwading" and your router model in google.

It depends on what your trying to acheive i guess. If for example your making a chat application. The client (behind the router) lets say its local IP is 192.168.1.111 and router IP is 80.120.78.100. The client would connect to the server.
Once that connection is made it doesn't matter about sending back to the client because the connection is already open between client and server so the server would just use the same connection. The router figures out where to "route" the packet, stuff which generally you dont need to know about.
If however your server is the one looking for clients, then thats different.

I'll try to explain a little about networks, but you'll have to search about it.
Basically, understand an IP mask, such as 192.168.1.0 as one network. Router's role is to connect different networks, that's why a router typically has 2 ports, WAN (wide) and LAN (local).
With this concept, you can see the internet as one big network made from the connection of various ISPs. Each ISP has a router to its network, and another one that gives you an internet connection. Finally, you have a router at your home. So, from this, you can understand that there are 3 networks connected: your home, your ISP and the internet.
In order for you to be able to connect to a computer at my home, I have to make this computer available from the internet, I have to publish it. I do this by setting up a NAT (network area translation) at my router. This NAT says "anything that comes from the internet on port 12345, forward to 192.168.1.10 (my server) at port 80".
This is an extremely simple explanation, ok?
Now, let's take a big step back. If you have another computer available on your network, you can test if your program is working with a much simpler approach.
Connect both computers to you LAN, so they will acquire similar IP addresses. Let's pretend they are 192.168.1.10 and 192.168.1.20
Run the server at 10 and disable all kind of firewalls (Windows and third party)
Run the client at 20 and try to connect to the server

Related

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.

Add a NAT UPnP mapping on a complex network using C#

I am creating an application that listens for connections (server) from a different application (client) via a tcp connection. So if both application where to be on the same network it will be easy to establish the connection. so I am trying to add a method to the server so that users do not have to open ports on their routers in order to make the app work when using it over the internet. so let me show you a few different scenarios:
1)---------------------------------------------------SCENARIO_1-------------------------------------------------------------
the server is a computer in a house. That computer is connected to a router and that router is connected to the internet. The client is a computer on a office that has access to the internet as well.
for this scenario to work, before I used to open the ports on the house router and forward them to the server computer. on the client I just had to supply the correct IP address and same port in order to establish the connection.
now I avoided opening the ports on the house router with a really cool technique:
first I add this reference:
then I tell the router which ports I want to be forwarded to my computer (the server):
NATUPNPLib.UPnPNATClass upnpnat;
NATUPNPLib.IStaticPortMappingCollection mappings;
public ServerExample()
{
InitializeComponent();
// server local IP address
mappings.Add(1300, "TCP", 1300, "192.168.0.120", true, "my server");
//.... etc
..
when doing this I am able to connect from my office computer by providing port 1300, the WAN ip address of the server computer which is a different one. (that address can be found at: whatismyipaddress.com) THE COOL THING IS THAT I DO NOT HAVE TO DO ANY CONFIGURATION ON THE ROUTER!!!
2)-----------------------------------------------SCENARIO_2----------------------------------------------
know here comes my problem. The server happens to be on a office. that server is connected to router A, router A is connected to router B and router B is connected to the internet. In other this example is like the last example on the server with an extra router in between. and the client is somewhere else. we do not care how the network setup is on the client computer as long as it has internet access.
the way I used to solve this was by forwarding the packages from router x to the server computer and also port forwarding the traffic from port x of router B to the ip address of router A. when setting up that configuration I was able to establish a connection with the client when providing the WAN ip address of the server. (the ip address that is shown at: whatismyipaddress.com when retrieved from the network of the office).
so it will be nice if I can avoid doing all this configuration on the routers and do something similar to:
public delegate void SomeDelegate(string parameters);
NATUPNPLib.UPnPNATClass upnpnat;
NATUPNPLib.IStaticPortMappingCollection mappings;
public ServerExample()
{
InitializeComponent();
// server local IP address
mappings.Add(1300, "TCP", 1300, "192.168.150.141", true, "my server");
mappings.Add(1300, "TCP", 1300, "another ip", true, "router's ip");
so I have been playing around with that and I have not been able to make it work. also it will be nice if I could find out if the server connection is like scenario 1 with c# or scenario 2 or maybe a different scenario so that the users setting up the server do not have to specify all that info. for example I am sure that limewire does something similar.
Edit:
here is scenario 1 illustrated:
and here is scenario 2 illustrated
The feature that you are using is called Universal Plug and Play (UPnP) which is basically a way for a device to tell another device how to communicate with it. What you are doing is telling a computer to tell the router what ports you want forwarded to it. Unfortunately UPnP is a non-routable protocol which means that it can't jump across routers. So the short answer is that doing what you are trying will not work with multiple routers without manually configuring the ones "farthest" away from the client.
It might be possible to do something with Internet Gateway Device protocol (IDG) but you'd have to research that more and the library you are targeting doesn't support that. Post this question to Server Fault and you might get a better answer. (Don't post your code, just explain the basics of your problem, that you're trying to have a machine register itself with a router using UPnP which is working but you have another router in front of that that you want to automatically forward, too.)

Connect clients

I am trying to connect 2 or more computers running a c# application. All TCP examples I find only show how to connect them if they are running on the same network.
My question is how to connect them over the internet?
I want to create a service like MSN but with direct connection (like a torrent software does),
any help?
regards!
Connecting 2 computers over the internet should, mostly, be the same as connecting them on a LAN. Your problems will mainly be with the firewall. In your C# code, you will have to assign an IP address & port for both computers. You should ensure that your firewall will allow this port through without blocking (beware of security vulnerabilities when opening new ports).
If your computers don't have public IPs, you will also have to set up some NAT on your router. This will map a port on your router + your public IP address to your local computers IP address + port. The other computer will then configure this public IP address + port in their configuration. There is too many configurable things that have the potential to screw up to list in a single post. I'd suggest learning some basics on networks.
However, having said all that, in terms of C# code you shouldn't have to change a thing to get the program to work over the internet if it works over a LAN.

Can lidgren-network library connect directly peer to peer given both IP addresses

I am using lidgen-network library 3 to try and create a peer to peer connection. I am new to network programming which is why i'm using this library.
Using the DiscoverKnownPeer() function i have been able to connect within my network. What i have read online is that i need to do a NAT punchthrough which requires a running server that is outside the router. I am hoping for a solution that doesnt require this extra server.
If i can store both ip addesses on a web server, and get create a web service to send the ip's to each respective client through XML, would I be able to create a peer to peer connection?
Thank you in advance.
No, you won't be able to establish a connection through NAT without a rendezvous server
NAT stands in your way because even if you know the IP address, that only gets you as far as the router. The router receives a request coming from the internet, and doesn't know which internal IP to send it to, so it throws it away.
NAT punchthrough works by making both peers send outgoing request to each other, the router then "knows" that packets directed at its public IP on the given port should go to the given computer.
So, you need to connect both computers to an external server not behind NAT, and then use that external server to coordinate connecting the two NAT obscured peers together (this is the peer introduction thing that Lidgren has built in).
I know this is old but...
You didn't specify that using the DiscoverKnownPeer() was necessary in your setup. If it isn't then you can setup peer to peer by appplying port forwarding rules in your router.

Categories

Resources