Android Socket Development Technology

Source: Internet
Author: User

Based on previous experiences, the network communication of application software is nothing more than Socket and HTTP, where Socket can use TCP and UDP, and there are many methods derived from HTTP, basic http get and POST requests, followed by WebService SOAP.

Among these methods, Socket is of course the most basic. Therefore, start with Socket.

The server does not need to be explained too much. No matter what language or platform is used, you only need to follow the basic Socket listening mode.

In fact, Android Socket development is Java Socket development. Therefore, developers who have learned Java may feel that there is no difficulty, but there are still some differences between PC development and so on.

First, let's talk about TCP development.

The TCP development in JAVA is much simpler than in C #, And the implementation method can be completed in a few words.

[Java]
Socket socket = new Socket ("192.168.3.119", 7628); // create a Socket instance and bind it to the remote IP address and port
 
OutputStream ops = socket. getOutputStream (); // defines an output stream from the Socket output stream

Byte [] bytes = B. getBytes ();

Ops. write (bytes); // write data to the output stream
 
Ops. flush (); // click it to output the stream.
 
// At this point, when the connection is successful, the server should be able to receive the sent stream.
 
 
// The next step is to receive the data sent by the server.
InputStream ips = socket. getInputStream (); // defines the input stream, from the input stream of the socket
 
Byte [] bytes2 = new byte [20];
 
Ips. read (bytes2); // read the input stream data

String str = new String (bytes2); // convert it to a String
 
Btn. setText (str); // display (I am actually on the button, of course, this method is not formal, but it can make me put less space, it looks like the interface is clean, as long as you can see the effect)
 
Socket. close ();

Socket socket = new Socket ("192.168.3.119", 7628); // create a Socket instance and bind it to the remote IP address and port

OutputStream ops = socket. getOutputStream (); // defines an output stream from the Socket output stream

Byte [] bytes = B. getBytes ();

Ops. write (bytes); // write data to the output stream

Ops. flush (); // click it to output the stream.

// At this point, when the connection is successful, the server should be able to receive the sent stream.

// The next step is to receive the data sent by the server.
InputStream ips = socket. getInputStream (); // defines the input stream, from the input stream of the socket

Byte [] bytes2 = new byte [20];

Ips. read (bytes2); // read the input stream data

String str = new String (bytes2); // convert it to a String

Btn. setText (str); // display (I am actually on the button, of course, this method is not formal, but it can make me put less space, it looks like the interface is clean, as long as you can see the effect)

Socket. close ();
The server-side code is not much explained, as long as the Socket listening and receiving are implemented, the message can be sent. In this way, Tcp Socket on Android is complete, which is very simple.

The next step is UDP. In theory, the implementation of UDP should be simpler. However, due to the use of simulators for debugging, a small problem has occurred and it has been depressing for a long time. In addition, the Java UDP Socket corresponds to the initramsocket class. Unlike C #, c # sets parameters when creating the Socket to specify the protocol type. First, read the code.

[Java]
DatagramSocket dgs = new DatagramSocket (); // create a Socket. This Socket will act as a sender and send the Socket package out.

InetAddress inet = InetAddress. getByName ("192.168.3.119"); // create an InetAddress. The IP address is the IP address of the remote server to be started.

DatagramPacket dgp = new DatagramPacket ("test2 ". getBytes (), "test2 ". getBytes (). length, inet, 7628); // creates a UDP packet that contains the remote IP address and port

Dgs. send (dgp); // send

Dgs. close ();


DatagramSocket dgs2 = new DatagramSocket (9997); // create another UDPSocket,

DatagramPacket dgp2 = new DatagramPacket (new byte [20], 20); // create an empty packet package
Dgs2.receive (dgp2); // receives and fills in the packet package

String str = new String (dgp2.getData (); // obtain the data in the packet and convert it to a String

Btn. setText (str); // display the obtained data

Dgs2.close (); // close the Socket

DatagramSocket dgs = new DatagramSocket (); // create a Socket. This Socket will act as a sender and send the Socket package out.

InetAddress inet = InetAddress. getByName ("192.168.3.119"); // create an InetAddress. The IP address is the IP address of the remote server to be started.

DatagramPacket dgp = new DatagramPacket ("test2 ". getBytes (), "test2 ". getBytes (). length, inet, 7628); // creates a UDP packet that contains the remote IP address and port

Dgs. send (dgp); // send

Dgs. close ();


DatagramSocket dgs2 = new DatagramSocket (9997); // create another UDPSocket,

DatagramPacket dgp2 = new DatagramPacket (new byte [20], 20); // create an empty packet package
Dgs2.receive (dgp2); // receives and fills in the packet package

String str = new String (dgp2.getData (); // obtain the data in the packet and convert it to a String

Btn. setText (str); // display the obtained data

Dgs2.close (); // close the Socket
Note that, without Port ing, UDP may not be able to receive data sent from the server because it was developed using eclipse, the debugging program uses a simulated environment. In a simulated environment, port ing is required for receiving data because there is no real IP address or port in the simulated environment, the simulator runs on 5554. It is okay to send data to the PC. But when the PC sends data to the simulator, You need to map the port of the local machine to the simulator. This is not required in the real environment, the procedure is as follows:

1. Run the simulator
2. Open the doscommand line window
Run: telnet localhost 5554
5554 is the simulator port. After running the command, the system will go to the android console.
3. Run the following command on the console:
Redir add udp: 8000: 9000
The first port is the PC port, and the second port is the simulator port.
After this command is executed, the data received by port 8000 of the PC is forwarded to port 9000 of the simulator, and the simulator can receive UDP packets from port 9000.

In addition, to implement Socket, you must add the following permissions to AndroidManifest. xml:

[Xml]
<Uses-permission android: name = "android. permission. INTERNET"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.