C# TcpListener external IP

岁酱吖の 提交于 2019-12-12 05:17:37

问题


I'm creating a TcpListener, and I want clients from other computers to be able to join my listener.

I've read and understood that I have to do Port Forwarding, but it doesn't make any sense to me - when I publish my app, I want other people to create this Listener, and I can't tell them to do Port Forwarding.

Is there any possibility to create a TcpListener that clients will be able to join without Port Forwarding? Thank you.


回答1:


Well, lets try to clear somethings out first.

The main reason to use port forwarding is because you have a NAT router in front of an internal network. To setup a port forward is to instruct the NAT router to forward traffic to a certain port on the public interface to a port on an internal computer.

If you don't have a NAT router you don't need port forwarding.

Many routers today support UPnP, a technique to kindly ask the router to create a specific port forward. A suitable library to use might be ManagedUPnP.

However you still need to figure out the public IP of the router and what port you have opened and communicate that to your other applications.

If your router does not allow UPnP or there are other fire wall rules in place you can not set up a port forwarding correctly.




回答2:


You can create server application and forward ports on your pc. Client application (this one you will publish) will just connect to your pc so they can be on the NAT. You can also combine your application with some php/asp pages but it depends on data you would like to send. If it's some kind of PC statistics like uptime, hardware etc. you would just use http query in client app to website script you've created (for instance mypage.com/?uptime=100&ram=2gb&hash=xxxx etc.)




回答3:


Only the server (the computer which is accepting TCP requests) needs to have the port forwarded.

The common model is that you (the developer/producer of the service) host the server. Then customers (people who subscribe to your service) connect to your service using either an IP or a URL. If your service is behind a firewall (you have a router between your computer and your internet modem) then you will have to forward the port. You will probably also have to open the port in Window's firewall, but I expect you have done this already. In this model the customer does not have to do anything with their router (it is like using a web browser).

If you are making a product where your customers are hosting the service then they will have to deal with the port issues. In which case you could try ManagedUPnP like Albin Sunnanbo suggests or redirect them to one of the many sites explaining how to setup port forwarding.



来源:https://stackoverflow.com/questions/13367711/c-sharp-tcplistener-external-ip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!