Hi 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 it form client(mobile) to server then to another client2(mobile).
I don't know how to write code in Java开发者_如何学Go 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.
Thanks in advance.
In the truest sense of the word continuously, it can't be done. Ethernet does not support continuous point to point data streams, for that you'll need serial cables or something like ATM.
However, you can approximate a continuous data stream by allowing for "bursts" in data which are hidden in upper levels. There is just no guarantee that the underlying network will be able to transmit or receive the minimum number of packets per minute necessary to simulate a continuous data stream.
UDP can support greater transmission rates, with a very important caveat: UDP packets can be dropped at any moment without actually being received. To use UDP, you'll have to take great care that loss of a few packets, or out-of-order delivery of packets don't mess up your game state.
If you determine that you can live with UDP's limitations, Sun / Oracle has a good introductory tutorial here.
As far as I understand what you need is a way to receive and send messages without messing with threads. This is what non-blocking IO do. You could use NIO as it is does not blocking thread of control while sending or (which is more important) receiving messages.
http://www.cordinc.com/blog/2010/08/java-nio-server-example.html http://download.oracle.com/javase/1.4.2/docs/guide/nio/example/index.html
精彩评论