How to make P2P application with Java NIO?

How to make P2P application?

Just a simple chatting application?

How many times difficult would it be compared to Client-Server app?

What's the basic idea behind it?

Do I have to bypass firewalls in computers and in routers? How to do that?



I have read a bit about ssh tunneling, but that's not excactly what Im after. I have also heard something about a master server that allows peers to connect to each other by some magical means. I have also made a small client-server demo with JGN and I understand that a p2p app needs server -and client socket, but I needed to port-forwarding for that demo to work over the internet.



But can I just make a Peer 2 Peer app that does not need any server at all?



Can anybody please point me to some simple working example or some good document?

first, yes you can, but without a central server it would require every one to manually config their firewalls/routers

Are you absolutely sure you want to use P2P? Using P2P means you have no way of validation.

P2P basically means a client connects to an other client without interference of a server.


A simple chat application is fully possible, given users' routers have been properly configured to port forward, and there's a database on a central server that can map nickname to IP address.

Momoko_Fan said:

A simple chat application is fully possible, given users' routers have been properly configured to port forward, and there's a database on a central server that can map nickname to IP address.


So the user wouldn't have to port forward manually? What is such port that I can use? What about NAT?
Henri said:

So the user wouldn't have to port forward manually?

User would have to enable port forward on his router manually for your application. Unless their router has UPnP enabled, which allows applications to set up their own port forwarding. It depends on a lot of things, I believe UPnP requires manual activation for some routers while being enabled by default for others.

Henri said:

What is such port that I can use?

For the most part, you can use any port after 1024. You should also have an option to change the predefined port.

Henri said:

What about NAT?

That's probably what would defeat the P2P thing, since user cannot set up port forwarding and UPnP is not available. You would have to re-route the communication through some central server as a fallback option when NAT is used.
Momoko_Fan said:

That's probably what would defeat the P2P thing, since user cannot set up port forwarding and UPnP is not available. You would have to re-route the communication through some central server as a fallback option when NAT is used.


Thank you for the reply. Do you mean that I would in that case have to make all communication go through a specific server, but then it would not be peer2peer anymore?
There must be a way, cuz there are so many p2p games and chatting apps.

I have heard something like if there is a "master" server that can enable communication directly between 2 peers. (I have no idea how though.
Now I just wonder, if there is NAT, but how can then client communicate with server? I mean like, when I made the a client-server game with JME and JGN and I had my server running at countryside (port-forwarded from router to the server app)and I was enable to connect to the server from my dorm room that has a subnet where my ip is 10.173.18.254 and it goes through my campus subnet(192.168.0.254) and then my universities subnet. I just don't f.....g get it. What does my client exactly tells to the server, so that they can start communication between each other? I just can't understand it, I think I should ask my computer networks teacher also.

What if the server would serialize that first clients socket and send it do the second client that would deserialize it. Could the second client in some magical way use that socket to connect directly to the first client directly?

Your cleints tells the nat where the answers are expected. (So the nat uses for tcp jsut the connections to find out who gets a packet when it is recived, while for udp it sotres for a certain amount of time the reciver. (basically a portforwarding by used ports for ips)



Basicall a central server can help enableing p2p, by telling both clients where the other one is, so that the conenction is initiated from both sides at the same time, and so both nat's remember where to route the incoming packets.

Empire Phoenix said:

Basicall a central server can help enableing p2p, by telling both clients where the other one is, so that the conenction is initiated from both sides at the same time, and so both nat's remember where to route the incoming packets.


What exactly do I have to make the central server tell to both clients where the other one is?

send ip and port , then elt the clients simultanously try to connect several!(cause the first few attempts are probably eaten by the nat's) times to the other.

So I just need to send the actual ip that a client has, i mean if it's in a subnet?

Is it really that simple?

Its kind of difficult for me to belive. I'm gonna give try it out.



What does elt mean?

tell

:). I don't belive it's that simple, it just doesent make sense to me. When my ISP gives my router ip 84.50.51.248 and router gives my pc ip 192.168.79.1.



There is no freaking way this would work like that, if it would then I could ping this ip, but that's impossible.

How can another client connect my pc by just knowing  192.168.79.1? (Well ok port also)

use the external router ip from both sides.



beause the clients try to establish a connection(on the same port, important!) where to route the incoming connection (In fact they belive it's their own outgoing connection, wich is why the forward that port automatically)



This applies esepcially to UDP, where the router has no way of knowing there are apckets expected other than that packets were send out before from a port (like source engine, your client sends a connection request, wich is why the router knows to wich pc the answers shall be routed, just in your case that you open it from both sides, so both routers know what to do with incoming packets on that port.  Btw this is also the reason why two udp based clients must have a different port when used behind a router. (This does not apply to TCP, since the routers can keep track of the connections due to the protocol it uses for connection establishing and ending (remind UDP is not connection based, you can always send to anywhere without establishing a connection, so if you write a UDP based system, make sure it is able to deal with packets from somewhere unrelated (especially important if you use standartports, like 27015 wich is where for example source engine games usually run(and a again i wrote a complete to long sentence in inorder style lol^^))))