using a server to manage tcp connections - c#

I have a server application and a client application in order to send and receive files between computers.
you may skip this part:
My algorithm is as follows:
`server: start listening for connections
client: establish a connection with the server
server: start a thread where that thread consist of a method listening for data send through the network stream
client: also start listening for data on a while loop runing on a separate thread.
\\so far if the client sends a byte array through the connection netwrok stream the server will receive it.
\\ also in each while lop the client and server there is a switch statement. if data received commes with the string "<file>" then it will start listening for the transfer of a file
client: send the string "<file>" to the server and start listening for that file on a different method.
server: because it receives the "<file>" keword it exits the listening loop and sends "<ok>" reply
.... so the talking between server and client keeps going ...........`
also the server is running yet more other threads such as still listening for
new connections. maybe another client want's to connect to it. so every time
a client connects the server, the server will start doing the same process
again on a separate thread (listening for new connections).
So as you can see everything that get's sent through the network stream get's received by the other application.
So everything works great when a connection is established
Now think about the things that have to happen in order to establish a connection from computer A that happens to be in a house and Computer B happens to be on a office on a different network. Note (computer A and computer B have internet access). so let's say you want your computer A to be the server and computer B to be the client. So you will have to start the server. great the server is now running. Then you will start the client application. You will also have to tell the router in the house to forward all traffic from port X to that server computer so that computer B is able to find it. Once you do that computer B is able to establish a connection. If next day you have a different IP address because most of us we don't have static IP you will have to change that IP on the client computer in order to establish a connection. so it can get quite complicated and not friendly. Also you may want your office computer to be the server because it might always be on but maybe you don't have access to the router so you are screwed.
Solutions:
1)
on my house call my internet service providers and pay to get a static IP.
I have not developed an algorithm but basically now computer A and computer
B will be clients and they will be connected to my computer at my house.
(on my house I will have to configure the router obviously to do port forwarding).
so know if computer A wants to send a file to computer B the server at my house
needs to have those to connections. the connection between computer A and the
connection through computer B. If something get's sent to the server at my
house from computer A then sent that to computer B and vice versa.
problems with this solution is: The upload speed at my house is very slow therefore
the transferring of files will be slow. also I don't know if my network at my
house will be able to manage multiple connections.
2)
step 1)
Host a website from my home still getting a static ip address. this time I also
have to get a domain so that users can remember me. so let's say that I am able
to host this website hosted on my house as somedomain.com . soon you understand
the purpose of the website....
step 2)
I don't know that much about this it is called "UDP punch hole into firewall".
this basically tells the router to port forward all traffic from port x to the
server so that users don't have to open ports on their routers. I DO NOT KNOW
HOW TO DEVELOP THIS I HAVE JUST HEARD ABOUT IT. so if I develop this computer
A will be able to connect to computer B without having to do any configuration
on the router which is nice.
step 3)
let's say that computer B is the server and computer A is the client that want's
to connect to computer B. so computer B will talk to my website at my house and
tell it that it is listening for connections on the ip address xxx.xxx.xxx.xxx .
then the client computer will access the website at my house and see the ip address
of computer B. now the connection is going to be able to be established.
step 4)
If the internet IP address of computer B changes for example. then let my website
know the new IP address. next time the client want's to connect to the server it
will be able to find the correct IP by looking into my website.
problems with this technique: I do not know how to implement
"UDP punch hole into firewall" so that users do not have to do any configurations
on their routers. an example of this will be helpful.
3)
there are a lot of companies out there that can host you a website. I don't know
about a company that will let me run a c# console application on their server.
Also I don't know if the file transfer of data going from computer A to a server
then to Computer B is more efficient than data going from computer A to computer B.
This solution will enable me to stop thinking about users having to configure their
routers which is nice.
This application enables you to do many more things. I know there are several applications that will enable me to just transfer files. It will be nice if someone will point me on the right direction of which technique to use.

If you use a a free DNS provider like DynDns free and a DNS updater for your dynamic IP like this one, then you can connect to your server by DNS name instead of IP address. Using the updater, when your address changes, the DNS record will be updated at the DNS provider.
Another option might be to use DropBox to synchronize a folder between the client and the server, and then convert your server application into a directory monitor that looks for new files synchronized by dropbox.

Related

Do I need to be connected to the Stun server to keep my public IP active?

I have a question, I am creating a remote desktop control service, I have investigated and it turns out that I need an accessible public IP, so I saw that there are Stun servers that provide you with a public IP so that they can connect to your computer, my question is , when sending a UDP packet to the Stun server and it returned my public IP along with the port, do I need to keep the connection alive with the server? And how can the destination computer connect to the source computer? Do you need to connect to the Stun server and find the source computer there?
If your implementation is to keep pinging the STUN server until it becomes time to bridge a connection, that's not the best way. And I'll spare the details - but this technique will not work reliably for all networks.
A better approach:
Have your endpoint connect to a signaling service. This can be a persistent TCP connection to a server that forwards messages, a push notification, server, or some hybrid approach. SIP, XMPP, node-js server, cups-with-strings, will all work fine. The hosts connect to the signaling service and awaits a message telling that there's someone waiting to connect.
Then upon exchanging addresses over the signaling service, you can reliably establish a direct connection between both endpoints.
More details here: https://stackoverflow.com/a/8524609/104458

How to deal with multiple client TCP/IP in C#

I am starting a small file sharing project on a local network. A user must be able to send a sentence (for the moment) to another user of his choice, connected to the same wifi of our house. The problem is that I cannot make a server with several clients, because at any time the server is liable to stop.
For example: imagine that I have three laptops: A, B and C. A is the server and communicates with B and C, and B and C also communicates thanks to A. But I must be able to turn off A but maintain a connection between B and C so they can continue to send sentences to each other.
I have started to create a windows service in C # which will run in the background. But is there any other way than to make A, B and C be both client and server ?
Thanks
As I know there is no limit for a node in network to be a server and at the same time a client.
If that system has IP and a port open, it can act like server. It should only listen on that port for incoming requests. Also at the same time it can connect to another server.
If system B knows about system C's IP and port, they can communicate with each other.
But in real situations like a messaging app, clients should send messages to server cause each time a client connects to a the network, it gets new IP.
So if this is not a problem in your network you can do what you want. But if it is, you can define a simple protocol between clients that at the start of the connection, they exchange their IPs so if the server turned off, they can talk to each other.

How to connect my .net application to SQL Server via internet

I have created a C# application with a login form (I use SQL Server database and SSMS). The database is on my PC, when I run the program on another PC which is in the same network as mine, everything works fine, but when I connect one of the laptops on another network, an error occurs: "The path wasn't found".
Is it possible to connect with the server remotely, via internet or something,
without having to be on the same network?
I already have:
Opened tcp and udp ports
Allowed SQL Server ports in Windows Firewall
Added an inbound rule for tcp (port 1433) and udp (port 1434)
Allowed remote connections
Here is my connection string:
Data Source=<server name>;Initial Catalog=Name;User ID=Minko;Password=Password1
This is not a programming question, but a Networking one.
To have a server reachable in the internet, is a costly mater. The static IP adress alone will cost a pretty penny, never mind actuall hardware.
On a small/no budget, VPN is perhaps the only way. Via VPN you can connect two local Networks (the home side of the Routers), allowing devices to talk as if they were physically in the same network.
No change to the programm is nessesary. If it works local, it works over VPN. The network classes do not care if the target is on the same computer, the same Switch or the Voyager probes.
The user will have to connect to teh VPN endpoint you have to provide - and usually can provide for cheap with any decent Router.
First off you should definitely change that PW.
Also - you make the comment that you are trying to use the same program to connect from a different network - implying that you haven't changed the IP address...
Are you sure you're using your public IP address? (Not your local network one that will start with 10. / 172. / 192.
Make sure you're using your public IP address... if you do this even if you're on the same network it will treat it as if you're not. It seems to me like you're still trying to use the local network's IP.
If it persists make sure you can reach the PC itself: ping ip-of-host in Powershell.
Connecting C# with SQL Server Database in a Local Area Network (LAN)
Try these steps one by one :
1- Find your public IP address using ( www.whatismyip.com ) for example.
2- Check that you can connect to the database using the public IP. To do that go to SSMS, and in the server name try to use IP-address only, or "IP-address\[instance-name]. If doesn't work, then you have to fix this first. Check this: https://dba.stackexchange.com/questions/62165/i-cant-connect-to-my-servers-sql-database-via-an-ip-address
3- When step 2 works, then you can use the "IP-Address\[instance-name]" in the connection string.

Why can't I connect to my server using my public IP?

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.

LAN based app - How to connect without static IP?

I am developing a LAN-based database application. It involves a central "server" app to house the database, along with many "client" applications that access it.
The "server" will be a simple C#-based HTTP server that responds to GET and POST requests. However, since it is designed to be able to run from any laptop on the network, I am wondering how to establish the connection between clients and the server without knowing the IP address.
I suppose I could ping every IP address from 192.168.0.0 to 192.168.0.255, and then test those that responded to see if any are my server. But I would only do that if there is no better way. Any suggestions?
Many of these types of discovery services run by putting out some kind of beacon on either the subnet broadcast address (for 192.168.0.0/24 it would be 192.168.0.255) or by putting out a beacon on a multicast address.
Multicast is particularly interesting because in a properly configured network, it allows hosts to find the service even across subnets. Routers and switches won't generally forward broadcast packets across subnet boundaries, but multicast packets will.
The beacon would have information in it such as the port the service is running on, what type of service it is, whatever is needed to start using the service.
To head you in the right direction, what you should do is have the database server running on a specified port. Then send out a broadcast to that port from the client (the system needing to connect to the database). When the database server receives this, it will be able to respond to the sender, allowing a handshake to occur.
Of course, you will need to validate the database server's authenticity (to make it secure, unless you aren't worried about that). This can be as simple as having the client display 4 numbers which then need to be typed into the database, so that the database can send the 4 numbers back to the client proving it is the right computer (how the iTunes remote works), or you can use certificates (but that is too complex a topic for me to cover correctly).
After that the two computers will know each others IPs, and you're set!

Categories