Communication over WAN - c#

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.

Related

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 to open Ports through 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

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.

TCP Server IP Client Connection Issue

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

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