Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,282 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,365 people online right now. Registration is fast and FREE... Join Now!




Peer to peer socket programming

 
Reply to this topicStart new topic

Peer to peer socket programming

boro84
13 Oct, 2008 - 08:04 AM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 2

Hi,

I am making a simple 4 player online game which I had implemented using a client-server architecture.

Right now, I would like to convert it into a peer-to-peer architecture but I am not quite sure how to start.

Do I need to write the server code inside the client, and let the 1st client who connects be the server? Or is there a true peer-to-peer implementation.

Thanks.




User is offlineProfile CardPM
+Quote Post

Gloin
RE: Peer To Peer Socket Programming
13 Oct, 2008 - 10:57 AM
Post #2

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
In a peer 2 peer architecture, both (all) hosts act as both client and server.
If you use TCP you'll have to create n*(n-1) serversockets and equally many clientsockets (assuming n hosts).
If you use UDP you only need 1 socket for each host but instead you have to handle all traffic on your own since it's not a reliable connection. It's pretty much flawless on a LAN though. However, you need to send data like who is it from and who is the reciever and so on.
You have to decide which suites you better.
User is offlineProfile CardPM
+Quote Post

boro84
RE: Peer To Peer Socket Programming
13 Oct, 2008 - 09:10 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 2

Hi Gloin,

Thnx for the reply. I got the idea already.

Another question. I should be using TCP. All of the 4 clients(1 player and 3 bots) will be connected using localhost. When creating the serversockets, each of the clients need to listen to different ports if I'm not wrong.

However, all of the clients are running the same application. How do I make sure that all of them listen to different ports? I need a server for that?

Also, I believe each client have to create 4 seperate threads for each of the players?

Regards.

This post has been edited by boro84: 13 Oct, 2008 - 09:22 PM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Peer To Peer Socket Programming
13 Oct, 2008 - 11:38 PM
Post #4

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
I believe you can leave the port option blank and it will be taken care of by the method itself. Otherwise you can either hard-code it or use a loop that searches for available ports within some range [1024..65535]
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:30AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month