Connect clients - c#

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.

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.

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

Need concept to develop p2p file sharing apps using c# 4.0

I need to develop a file sharing apps over the internet by p2p. I have very rough idea for developing p2p based apps.
Suppose I have one server which has a static IP which is running at specific port like 5001. Now client connects to server and sends its own public IP.
Questions 1
With the help of C# how client can connect to server and send its own public IP?
Guide me which class I need to use?
When server will get IP from client it will store. So when two clients connect then server has IP info of two different PCs. PC1 will request server to get the IP of PC2 and same way PC2 will request to get ip of PC1. So now PC1 & PC2 both have the IP of each other.
Questions 2
Now question is how PC1 can connect to PC2 which code i need to write to connect?
Questions 3
Now question is if PC1 can connect to PC2 then how PC1 can send file to PC2 ?
Please tell me which class I need to use in order to develop this type of P2P-based file sharing apps.
When it comes to peer2peer, you always need at least one PC who has port forwarding enabled. This means, that if you have a listener/server at pc1 and a client at pc2, you would need a port forwarding on pc1.
This is called HighID/LowID in general and is a symptom of the NAT firewall implemented in almost every router. Without at least one port forwarding, there is no way two PCs can connect. This is because the router wouldn't know which local IP to map to an incoming TCP packet unless there is a forwarding.
As for what classes you can use: Take a look at System.Net.TcpClient and System.Net.TcpListener

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.)

Control Devices Over the Internet

I want to control devices to remote location using Internet.
I did some Experiments. I have one Laptop connected via WIFI and a computer connected via Router. it works fine. now when i run from system connected to other router it did not worked even both had internet connections.
one reason may be it cannot trace IP because there are many computers. Did implementation done here http://csharp.net-informations.com/communications/csharp-server-socket.htm
is it possible to control devices over the internet?
secondly what is difference b/w Sockets and Winsock?
When you tried your system with wifi and the router I assume you were on the same network. Now you are using the internet you should be using the public IP addresses of your routers and map the port(s) you are using to the specific computers you want the data routed to. If you do not have static IP addresses available you probably should look into using something like DyDNS and OpenVPN to create a private network.
As far as your question about Winsock vs Sockets look at this article
Edit: Take a look at this article on port forwarding

Categories

Resources