1

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:

  1. Both client register report its ip to server and then server exchange their information.
  2. 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.
  3. 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?

Ciel
  • 5,551
  • 5
  • 17
  • 24

2 Answers2

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.

ja_mesa
  • 1,971
  • 1
  • 11
  • 7
  • Hi, thank you for replying first. I have tried the second way on text chat and it works. The only thing I worry about is the Internet speed. But in video chat, since server has to receive and resend a video stream, it will be slowly right? I don't know how skype implements its video chat function. Is there any P2P solution? There are many P2P download software. How can they transfer file without setting up port forwarding on routers? – Ciel Mar 06 '14 at 04:00
  • @Ciel Skype and any other chat system do it the way I told you as the second solution. You talk to a server and the server talks to your mate, this way you may have more than 1 receiver like a group of friends. But the server acts as a router. The clients don't have to open any port because they create the connection. I don't think the stream get slower since it acts just like any other router or device in the network; it reads from one socket and sends to another (or others) socket/s. The server does not do anything with the data, it only routes it. – ja_mesa Mar 06 '14 at 10:21
0

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!

Marcus Harrison
  • 819
  • 6
  • 19