Using Android Socket

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.







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值