Updating Coordinates to All Clients

Hey all. I was just wondering if someone could give me an outline, or a VERY general explanation of how i should organize this. What I am doing is whenever a client connects to my server (via an applet) they will be able to move a box around with the arrow keys, and they will be able to see everyones boxes that are connected to the server. I'm also not sure whether to use TCP or UDP for this. I'm not sure which is more efficient for a project like this.

I am not asking anyone to program anything for me, I understand how themechanics of my game are going to work, and i've already built a single player version, except slightly more complicated with a rotating image. I also understand how Sockets, and ServerSockets, and all that stuff works as well. The thing i really need help with is structure and organization of my networked program.

1) Structure

2) TCP vs UDP

I'm looking for something along the lines of this.

--

Server

*Accepts connections

*Sends connection to a World Object

Client

*Tries to connect to the Server

*Runs the listeners and moves character

*Sends position to server (or something, this is where i'm having trouble, how to update the positions of all my characters)

*Receives others positions(?)

World

*Receives connections and coordinates and relays them to other clients.(?)

*Keeps world attributes to send to other clients(?)

-

How might one go about tranferring a set of numbers to all clients connected to a server. If in the server i have an ArrayList of sockets to send them to?

Thanks so much for reading this, and i would really appreciate a reply!

Message was edited by:

PaRlOaGn

[1836 byte] By [PaRlOaGna] at [2007-11-27 8:27:32]
# 1

Is this so difficult?

for (Socket socket : sockets)

{

socket.getOutputStream().write(...);

}

// or ...

for (OutputStream out : outputStreams)

{

out.write(...);

}

Of course there is a practical difficulty, in that a client which is hung up for whatever reason can cause this loop to block. Which is a problem with TCP broadcasting systems generally, which is why you generally either have a write thread per client or use NIO.

ejpa at 2007-7-12 20:17:12 > top of Java-index,Core,Core APIs...
# 2

Well, yes i understand what you're saying, just write the info to output stream. But sending the soldiers coordinates from the client to the server.. I'd need to send some sort of custom packet class through an object stream? Here let me explain: if you have multiple clients connected, say 6 people. And i have an ArrayList of coordinates, the client has to KNOW it's position in the ArrayList for starters, then send it's position logged with it's current coordinates through an output stream, then send the WHOLE ArrayList to all the clients once it reaches the server... i'm just confused as to a good way to do that.. I understand how streams work... yeesh.

PaRlOaGna at 2007-7-12 20:17:12 > top of Java-index,Core,Core APIs...
# 3

OK, well you can write an ArrayList via ObjectOutputStream.writeObject(), as long as everything in the list is Serializable, and read it via ObjectInputStream.readObject(). Or you can define your own 'custom packet class' and define it as Serializable, then ditto.

The co-ordination of all the data is up to you ...

ejpa at 2007-7-12 20:17:12 > top of Java-index,Core,Core APIs...
# 4

Thank you sir, you have answered my question completely. Except there's ONE more thing i'm not quite sure how to handle. I need a private data member: private int index;

that's inside my client. Then somehow set the clients index to the number in which he connected to the server. Is there an easy way to do that? If not, you've already helped me enough!

Thank You Again!

PaRlOaGna at 2007-7-12 20:17:13 > top of Java-index,Core,Core APIs...
# 5
Well I suppose the server will have to provide that to the client, probably in response to the initial connect request.
ejpa at 2007-7-12 20:17:13 > top of Java-index,Core,Core APIs...