问题
I am developing a Java Video chat application. I use Java socket to communicate. But it seems I have to set up the exact IP and Port to connect a socket server. My design is:
- Both client register report its ip to server and then server exchange their information.
- Choose one client(A) to be the video chat socket server and report its ip and port to server. Server will tell another client(B) his video chat server ip and port.
- Client B register itself to Client A. Now A and B can communicate directly.
Here comes problem. If A has a public Internet IP, things work well. Problem is that if client A and B are both behind a router, for example client A and B are in different University, how can they communicate with socket?
回答1:
Well, there are some implications here
1. You have to open ports in the router (NAT) so that it knows the IP of the PC/Device which is listening at that port. You can open ports in most of the routers either a specific port or a range of ports, also you may configure a DMZ host so that everything that comes in goes to that Host.
2. You have to configure the firewall at those Devices to allow this/these port/s.
The problem with this approach is that every time you reset the router or change a new router you have to re-configure it. I do not recommend NAT Port Mapping or IGD, they both create "holes" to malicious abuse, and I do not think the university allows you to use this kind of software in their routers.
Another solution is to use the Server as a gateway, I mean, make the server that both clients connect acts as a gateway or router. When client A connects to the server the server keeps a list of connected clients and announce these clients, when client B wants to connect to client A the server will do the traffic from one to another, it may be used by mobile phones, tablets, PC, etc. This way you don't have the router (NAT) problems since both clients make the connection to the server. Of course, this involves much work at server side and it must be a dedicated server. It's like any chat application like Whatapps or Google Talk, the clients talk to the server and the server routes the information to the appropriate client/s.
回答2:
In the case that clients are behind a router, there are two possible solutions.
The first is that you just ignore the case completely, document the ports used by your program and instruct users to manually set up port forwarding on their gateways. This moves the burden from you as a developer onto your users and simplifies the software.
An alternative is to use the Internet Gateway Device Protocol and the NAT Port Mapping Protocol to automatically set up port-forwarding on the user's gateway device. Most routers and firewalls support one or both of these protocols. However, I cannot find any direct support for these protocols in the Java standard library, so you will either have to find and use third-party libraries or implement the protocols yourself. I cannot recommend any third-party libraries myself, but perhaps others with more experience can provide links to third-party libraries they recommend. This removes the burden from the user and allows "just works" functionality, however it puts a larger burden on you, the developer, to ensure that the router is correctly set up and to prevent multiple separate devices attempting to forward the same port, as well as synchronise IP/port numbers with the server to ensure connections are routed correctly.
In the case that the users are, for example, behind a university gateway, depending on the university's protocols the users may still have to manually allow/forward ports to their local device.
I hope this answer points you in the right direction, at least. Good luck!
来源:https://stackoverflow.com/questions/22193743/video-chat-application-using-socket-over-public-internet