使用服务器来管理TCP连接

时间:2011-09-02 16:09:37

标签: c# networking tcp hosting

我有一个服务器应用程序和一个客户端应用程序,以便在计算机之间发送和接收文件。

您可以跳过此部分:

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

因此,您可以看到通过网络流发送的所有内容都被其他应用程序接收。

所以当建立连接时一切都很好

现在考虑一下为了与计算机A建立连接而发生的事情,恰好在一个房子里,而计算机B碰巧在不同网络的办公室。注意(计算机A和计算机B可以访问Internet)。所以假设您希望计算机A成为服务器,计算机B成为客户端。所以你必须启动服务器。伟大的服务器现在正在运行。然后,您将启动客户端应用程序。您还必须告诉家中的路由器将所有来自端口X的流量转发到该服务器计算机,以便计算机B能够找到它。一旦你这样做,计算机B就能建立连接。如果第二天您拥有不同的IP地址,因为我们大多数人都没有静态IP,您必须在客户端计算机上更改该IP才能建立连接。所以它会变得非常复杂而且不友好。此外,您可能希望您的办公室计算机成为服务器,因为它可能始终打开,但您可能无法访问路由器,因此您被搞砸了。

解决方案:

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. 

此应用程序使您可以执行更多操作。我知道有几个应用程序可以让我只传输文件。如果有人能指出我使用哪种技术的正确方向,那将是很好的。

1 个答案:

答案 0 :(得分:2)

如果您使用免费的DNS提供商(如DynDns free)和动态IP的DNS更新程序(如this one),则可以通过DNS名称而不是IP地址连接到您的服务器。使用更新程序,当您的地址更改时,DNS记录将在DNS提供程序中更新。

另一种选择可能是使用DropBox来同步客户端和服务器之间的文件夹,然后将服务器应用程序转换为目录监视器,以查找由dropbox同步的新文件。