I have developed a game on Android. Now I want to play it on wifi or 3G. I have game packets which I want to send form client (mobile) to server then to another client (mobile).

I don't know how to write code in Java to send the playPackets continuously to server and receive the playPacket continuously from the server to the clients.

I guess I have to use two thread one for sending and one for receiving.

Can someone help me with the code, or the procedure to write code for it?


2 Answers 2


Ok lets start from the beginning. I don't know what you are doing and how but when doing with network you have to give it a try to the REACTOR pattern.

Basically reactor is a way to avoid the use of threads or to defere it where a threaded-task is more decoupled by the core.

The center of the reactor is the select function: you register your event sources and ask the select to return when something happends to one of those sources.

When something appends, the reactor main loop simply finds What appened to What source reacting by creating an Event and dispatch this event to objects interested in those kind of events.

I'm not a Java Guru but i know that you can setup socket (even UDP ones) and you can set them as non-blocking. Along whit this I know there is a class called Selector in a package called NIO. Those stuffs concur to define a multiplexed, non-blocking I/O facility

All you need is simply setup two UDP channels: one for server listening and the other for server talking; register the listening one to the reactor and integrate the reaction step of reactor into your main loop.

Take into account that this kind of approach let you to connect simultaneusly with different channels so you can consider to develop a peer-to-peer game (no server bottleneck and distribuited design... sounds good!)


Please note that if you are using some sort of GUI tookit/facility, you probably are already using a Reactor...


A good start for client-server programming would be kryo net since its easy to learn and often used/referred in java game develoment. Its event-based, so u will not need any threads here.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .