TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are twoimportant protocols in transport layer of the network. The usage for TCP andUDP in Android is the same as in Java. They can be achieved based on socketwhich is a handler in one communication link.
TCPprovides reliable, ordered delivery of a stream of bytes from a program on onecomputer to another program on another computer. Applications, which do notrequire reliable data stream service, may use UDP, which provides a datagramservice that emphasizes reduced latency over reliability.
Here,we mainly talk about Android UDP socket. There is no obvious different betweenUDP server and UDP client because no connection should be establish at first.Each application can act as server or client to send and receive the data fromothers. The sender just needs to know the IP and port of the destination tosend the data without caring about the result.
InAndroid or in Java, we can use a Datagram socket to accomplish one UDP transmitand receive process within three steps.
- Step 1. Define DatagramSocket.
- Step 2. Define aDatagramPacket
- Step 3. Send or receive thedata packet
Wegive the following example to compete the simple process of sending andreceiving data using UDP socket. You can find how easy it is.
whensending the data packet:
public static void send(String message) throws Exception {
int port = 2012;
String Msg = "Hello IdeasAndroid!";
// Step 1. Define DatagramSocket
DatagramSocket Send_Socket = new DatagramSocket();
// Step 1. Define DatagramPacket
DatagramPacket Send_Packet = new DatagramPacket(Msg.getBytes(),
Msg.length(), InetAddress.getByName("localhost"), port);
// Step 2. Send the data packet
Send_Socket.send(Send_Packet);
// Step 3. Close the socket
Send_Socket.close();
}
Whenreceiving the data packet:
public static byte[] Receive() throws Exception {
int port = 2011;
byte[] message = new byte[1024]; // data buffer
// Step 1. Define DatagramSocket
DatagramSocket Receive_Socket = new DatagramSocket(port);
// Step 1. Define DatagramPacket
DatagramPacket Receive_Packet = new DatagramPacket(message,
message.length);
// Step 2. Receive the data packet
Receive_Socket.receive(Receive_Packet);
Log.d("UDP Receive", Receive_Packet.getAddress().getHostAddress()
.toString()
+ ":" + new String(Receive_Packet.getData()));
// Step 3. Close the socket
Receive_Socket.close();
return message;
}
Also,we should add the following two statements on the top of the source file.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
DatagramSocketclass implements a UDP socket for sending and receiving DatagramPacket. ADatagramSocket object can be used for both endpoints of a connection for apacket delivery service. There are several important methods.
- DatagramSocket(int aPort):Constructs a UDP datagram socket which is bound to the specific port aPort onthe localhost.
- send(DatagramPacket pack):Sends a packet over this socket.
- receive(DatagramPacketpack): Receives a packet from this socket and stores it in the argument pack.
- close(): Closes this UDPdatagram socket and all possibly associated channels.
For more information,visit http://developer.android.com/index.html. This is a link to Androidonline developer documents. It’s very useful for our development.