well here’s my problem with the multithreading part… presently i have the server only accepting and then handing off to middleman serverthreads that have the socket for each client. The server holds all of the information (namely the Sprite instance for the main character of every client) and distributes those througout the clients by a method within the server, accessed by the middleman, that sends out the arraylist to each client.
so far so good… just like every tutorial on the subject:
- client connects to server and server receives the Hero (the character being controlled) and adds him to the heros arraylist.
- server passes the received socket off to middleman(serverthread) that loops through receiving from and sending to the client via the socket.
- in the client’s send to, it accesses the server’s heros arraylist and sends that to it’s client to paint.
The problem is that via this method, the whole class is sent every time. I tried it to see if it would be that big of a deal, and indeed it brought me from 60fps to less than 1fps. I tried thinking of a way that each client only sends its x,y coords after having sent it’s whole class to the server on construct to be added to the arraylist. that way, the server can deal each Hero instance to each client everytime a new one shows up (a new person logs on) and then distribute only coords every time after that.
I’ve been mapping this for days, trying to figure out the best relationship between server, client, and serverthread and i still can’t figure out a way to effectively pull this off. The closest i got was that the server can tag a byte on before every Hero sent to each client. that byte will say whether it’s a new hero to be added to the client’ repertoire, or an existing Hero of which to update coords. My problem with that is, how can i, from within the server, figure out whether every client has the new Hero’s content so as to change that byte back from newHero to justCoords?
It’s a big mess… and I’m very new to java, but I’ve been told that UDP might be more effective in this regards. If the server had a way of skipping ServerSocket::accept() if there’s nothing to be accepted, that would work perfectly because then the server could cycle through clients with a for loop and set any newHeros back to justCoords at the end of the loop, ensuring that everyone gets the new hero without overhead.
Oy!