Help communicating with sockets through nat
i want to make a simple p2p messenger in java, i have managed to communicate with 2 computers in the same network but when i try to send a message to another computer from outside the network, the message is disappearing... I know that i could make forwards in the router but i don't want to do that, i want to make it like yahoo,gtalk or other im servers do. I think that they use the nat system to do that, but i don't know very well how :D.
Is somebody who can explain me how NAT works, and if this is the way of solving my problem?
I read that I need an static and publ开发者_高级运维ic ip for the server and when the client(with a private ip) sends a request to the server, his NAT will know what to do with the message that will come from the server next time... but if the client don't send a paket to the server and the server sends a packet to client then the NAT doesn't know where to send the packet inside the network(to the client).
Is it right what i think?
Thank you very much!
You're almost right in your NAT description. The only mistake you've made is that pointed out by Peter - the client is always the one that initiates a connection.
Now, if both sides have public IPs, everything is fine and either side can be a server.
If one side is behind a NAT and the other one has a public IP, then it's okay if the one that's behind a NAT initiates the connection. In such case the NATing router will remember where to send responses from the server.
And finally, if both sides are behind NATs, it looks like it is impossible to initiate a connection, but in fact it's not always so. There's one trick that is called "hole punching". This is a kind of white magic and it doesn't always work, but it works often enough to be successfully used by Skype, for example.
The client by definition initiates the request, the server services that request and sends back a response. The assumption is that clients can be trusted to make network connections, servers cannot. BTW: On some networks, clients are not even trusted to do that and must use proxies.
I repeat; the client is the one creating the connection, whether it is on your network or on the internet.
All the other system you are mentioning use a server with a well known public IP to help clients communicate with each other. Each client communicates with the server and the server sends every message to the appropriate client. This is perhaps the only way to do it, because otherwise:
- Every client should have a public IP in order for other clients to reach him. Each client should also act both as client and a server.
- It will be very difficult to discover clients as you would need to know their IPs
Your system may work inside a local network, but it won't work in the internet. Each computer in a network has a local IP assigned to it by a local gateway. This local IP is invisible to outsiders, unless there is NAT setting that maps a public IP to it. Also there must be a setting for outgoing messages.
精彩评论