ANDROID SOCKET Development

Source: Internet
Author: User

Articles reproduced in Zeta (http://blog.sina.com.cn/s/blog_685790700100xjuo.html)

Recently in the study of Android application software development, before the 7, 8 years of C #, not familiar with Java, but fortunately can read some!

According to previous experience, the application software network communication is nothing more than a socket and HTTP, where the socket can be used with TCP and udp,http to derive a lot of ways, the base of the HTTP GET and post requests, and then WebService soap.

In these ways, the socket is of course the most basic. So start with the socket first.

Service side does not need too much explanation, no matter what language to use what platform can, as long as follow the basic socket listening mode on the line.

In fact, the development of the socket of Android is Java socket development, so the developers who have studied Java may feel that there is no difficulty, but there are still some different from the development of the PC need to pay attention to

The first is to talk about TCP development

The development of TCP in Java is much simpler than in C #, and can be accomplished in a few words.

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

OutputStream Ops = Socket.getoutputstream ();//define an output stream from the socket output stream

byte[] bytes = B.getbytes ();

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

Ops.flush ();//brush Line output stream

At this point, when the connection is successful, the server should be able to receive a stream of past messages.

Next is the data sent by the receiving server.
InputStream ips = Socket.getinputstream ();//define input stream, input stream from socket

byte[] Bytes2 = new BYTE[20];

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

String str = new string (bytes2);//Convert to String

Btn.settext (str);//display (I am the reality on the button, of course, this method is not normal, but can let me less space, it seems that the interface clean point, as long as you can see the effect on the line)

Socket.close ();

Service-side code is not much explained, as long as the implementation of the socket listening and receiving, send on the line. So the TCP socket on Android is complete, very simple.

Next is UDP, in theory, the implementation of UDP should be more simple, but, because of the use of the simulator to debug the relationship, a little problem, depressed for a long time, in addition, Java with UDP socket corresponding class is datagramsocket, unlike C #, C # is to create the socket when the parameters to develop the protocol type, first look at the code.

Datagramsocket DGs = new Datagramsocket ();//Create a socket that will act as a transmitter and send the socket packets

InetAddress inet = Inetaddress.getbyname ("192.168.3.119");//Create a INETADDRESS,IP address for the remote server IP to be launched

Datagrampacket DGP = new Datagrampacket ("Test2". GetBytes (), "Test2". GetBytes (). length,inet,7628);//Create a UDP packet, The packet contains the IP address and port of the remote

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
Dgs2.receive (DGP2);//Receive data and populate the packet

String str = new String (Dgp2.getdata ());//Get the data in the packet and convert it to a string

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

Dgs2.close ();//Close socket

It is important to note that, without port mapping, UDP may not be able to accept data sent from the server side, because when using Eclipse development, the debug program is used by the simulation environment, in the simulated environment, the receiving data is to do port mapping, because the simulation environment, Without their own real IP address and port, the simulator is running with 5554来, sending data to the PC is no problem, but when the PC is sent to the simulator, it needs to map the port of the machine to the simulator, the real machine environment does not need, the specific operation mode is as follows

1. Running the simulator
2. Open the DOS command line window
Execution: telnet localhost 5554
5554 is the port of the simulator and will enter the Android console after execution
3, under the console execution:
Redir Add udp:8000:9000
Where the first port number is the port of the PC, and the second port number is the emulator port.
After executing this command, the data received by the PC 8000 port is transferred to the 9000 port of the simulator, and the simulator can receive UDP packets from the 9000 port.

In addition, to implement the socket, you must add the following permissions in the Androidmanifest.xml

<uses-permission android:name= "Android.permission.INTERNET"/>

ANDROID SOCKET Development

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.