Android client connects to server via socket

Source: Internet
Author: User

The Android client connects to the server through the socket.


Android Internet projects, the vast majority of the need to connect to remote servers, there are many ways to connect, can be a TCP way, of course, can also be through the socket way.
Compared to TCP, the way the socket is slightly more primitive, for the client, the complexity is more than the TCP way, after all, TCP connection has a ready-made framework can be used,
such as spring and so on. And the use of the socket way these work completely need the client to do, but also increased the workload of the client, but everything has pros and cons, through the socket way, traffic on
Compared to TCP and other ways more controllable, after all, less than a lot of useless information transmission.


The approximate idea is as follows:


The client first connects to the server:

Socket = new socket (); SocketAddress address = new Inetsocketaddress (Svrhost, Svrport); Socket.connect (address, time_out); Socket.setsotimeout (time_out); in = new BufferedReader (New InputStreamReader (Socket.getinputstream ())), out = new PrintWriter (New BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())), true);


After connecting to the server, call register or log in to get the token of the connection. Each subsequent connection gets information with the agreed token.


After the connection is established, the socket does not have to be shut down, after all, each connect also wastes resources, can hang up waiting for the service side of the push or heartbeat and other messages.
while (!exitsocket) {try {if (!socket.isclosed () && socket.isconnected () &&!socket.isinputshutdown ()) {char[] Lenbuffer = new Char[13];int len = 0;try  {len = In.read (Lenbuffer);} catch (Exception e) {utils.debug ("SOCKETSVR socket read timeout"); Stopsocketbyexception (true);}



Each time a request is requested, the data returned by the server is contained in Lenbuffer, which is generally a well-agreed JSON or other format.


The whole idea is the same as TCP, and more problems arise in detail processing and the maintenance of the life cycle of the socket.


For example, if a network break or an exception causes the socket to appear exception, you may need to close the current socket (timeoutexception should not need to restart), and then restart the new socket,
But for the end user, it should be avoided to let the user feel the interface of the different, need to reconnect the server immediately.


The above is a simple model for the socket client. For reference.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android client connects to server via socket

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.