[java] Networkaction in Java

Started by
3 comments, last by dr-m 23 years, 5 months ago
Hi, Looking at examples of networking and sockets in Java, all I seem to find are lame send-one-string-and-return-another type of examples, or all sorts of non-action, turn based games. But what about when it comes to sending messages at rapid speed between server and clients, like in a normal high fps actiongame? I got a implementation running, but it doesn''t behave like expected (duh!). Its using java DataInput/Output- Streams to send positions and events server<->client(s). I need some pointers to get going again... My streams are just not behaving.. . For one thing, by just pumping in data in one end very fast, results in lost data at the other end -- that shouldnt be,..or? And I seem to have to send just as much data as i recieve, or things go bad. Anyone done something like this or know of any examples (good or bad) thats doing it? The internet needs more multiplayer action in java, damn it! And I''m sooo close.. / dr-m
/ dr-m
Advertisement
Try something like this -

1) Use object serialisation to send stuff b/w and forwards across sockets.

2) Set up a object that all it does is read in an object in a loop continuously. Make that object extend both Observable, and Thread. It can then go off and do its own thing. Then, every time it reads in an object, make it call update(), and pass across the object it just read in.

3) Then make the update() method in the whatever it is that your running do whatever it needs to do to make your game.. well... do its game stuff.

Hope that was what you were looking for, cause its what just popped into my head.

Mark



----
www.emanatemultimedia.com
----
<< ...and here is one we developed earlier... >>
Thats almost what I do, I take like small snapshots of the gamestate and continously cross-send them every 40ms or so (by using a thread dedicated to this). Also, I have another loop running that sits and waits for any messages from the server, and then processes it accordingly.

The problem is more like that I''m not good enough at working the streams in the right way. I want it to work like a pipe or simular, so that I just can read() from it at my own speed and just separate the messages from eachother by hand. But it doesn''t quite work that way now.

Anyway, 98% of game time is spent doing only;
client->server:update (send own)
server->client:update (recieve all other players dito)

The problem appears when i want to send an event, in its own separate and "spontaneous" message. Currently this is only the NEW_PLAYER event, informing about a new player logged in. Sending that message (being at a different size and all) seems to disturb the order and just makes everything mad. Hmm.. it''s really starting to sound like a bug in my code... Probably is.

I guess I just want to learn more about working the streams at this speed with accuracy.


/ dr-m
/ dr-m
"I want it to work like a pipe or simular, so that I just can read() from it at my own speed and just separate the messages from eachother by hand. But it doesn''t quite work that way now."

Thats why object serialisation is so useful, it does that for you, and you don''t have to seperate it by hand.

Another option is to write your own "decode" and "encode" methods for a particular object, in which you simply pass in a BufferedReader or Printwriter, and it does the rest. Particularly useful when your only sending ints, or char''s.. and don''t want all the extra "object data" going across.


"I want it to work like a pipe or simular"

Stupid question of the day - your not closing the sockets after you send each message are you?"

"I have another loop running that sits and waits for any messages from the server"

Does this also happen at the client side as well? and both are on their own threads?

Mark


----
www.emanatemultimedia.com
----
<< ...and here is one we developed earlier... >>
>"I want it to work like a pipe or simular"

>Stupid question of the day - your not closing the
>sockets after you send each message are you?

Well, no.. (only on logout). But its /possible/ that the streams gets destroyed once and a while, tho.. But does that really matter? Shouldn''t a call to socket.getInputStream() return the same stream object anyway?

>"I have another loop running that sits and waits for
>any messages from the server"

>Does this also happen at the client side as well?
>and both are on their own threads?

Yes, the server starts a new thread for each player that logs in and waits for data to processes. Data is also send from that thread. Are you suggesting that I use two threads per player, one for writing and one for reading? Seems a bit messy.

I think I''m gonna revert back to testprograms soon and test some more, its getting a bit out of hand...


/ dr-m
/ dr-m

This topic is closed to new replies.

Advertisement