Android mobile app development experience via socket (TCP/IP) and PC communication

Source: Internet
Author: User

Android is an open-source mobile operating system platform that has been viewed by many developers as the most promising smartphone operating system of the future. Moreover, in a short period of time in the Android Market on the emergence of a large number of third-party applications for users to download and use, some of which enhance the mobile phone application features, while others will fully play

The interaction of mobile phones with other electronic devices. Today, this article mainly explains how to achieve Android and PC communication.

First, we first create the server side of the PC with the following source code:

Public class Tcpdesktopserver implements Runnable {

Public Static Final String ServerIP= "192.168.1.100";

Public Static Final int ServerPort= 51706;

@Override

Public void Run () {

Try {

System. out. println ("Server: Connecting ...");

ServerSocket serversocket = new serversocket (serverport);

while (true) {

Socket client = Serversocket.accept ();

System. out. println ("Server: Receiving ...");

Try {

BufferedReader in = new bufferedreader (new

InputStreamReader (Client.getinputstream ()));

String str = in.readline ();

System. out. println ("Server: Received: '" + str + "'");

}catch(Exception e) {

System. out. println ("Server: Error! ");

E.printstacktrace ();

}

finally {

Client.close ();

System. out. println ("Server: Off. ");

}

}

}catch(Exception e) {

System. out. println ("Server: Error! ");

E.printstacktrace ();

}

}

Public Static void Main (String a[]) {

Thread desktopserverthread = new thread (new tcpdesktopserver ());

Desktopserverthread.start ();

}

}

The above source code analysis is as follows:

Public Static Final String ServerIP= "192.168.1.100";

Public Static Final int ServerPort= 51706;

Specifies the port and server IP address that the server listens on.

ServerSocket serversocket = new serversocket (serverport);

An ServerSocket object is created by the application before the IP and port specified.

Socket client = Serversocket.accept ();

Used to listen for and capture clients that connect through the socket.

BufferedReader in = new bufferedreader (new

InputStreamReader (Client.getinputstream ()));

Apply the socket to create a BufferedReader object that receives data from the socket stream.

Second, create the client in Android with the following source code:

Try {

InetAddress serveraddr = inetaddress. Getbyname ("192.168.1.100");//tcp Server IP address

Log. D ("TCP", "Server: Connecting ...");

Socket socket = new socket (serveraddr,51706);

String message = "Hello!" Android. ";

Try {

Log. D ("TCP", "Server is sending information: '" +message+ "'");

PrintWriter out = new printwriter ( new BufferedWriter ( New OutputStreamWriter ( Socket.getoutputstream ())),true);

OUT.PRINTLN (message);

} catch (Exception e) {

Log. e ("TCP", "Server Error", e);

}finally{

Socket.close ();

}

}catch(Exception e) {

Log. e ("TCP", "Server Error", e);

}

}

Source Code Analysis:

Specifies the IP address of the server.

InetAddress serveraddr = inetaddress. Getbyname ("192.168.1.100");

Application server IP and port to establish socket object

Socket socket = new socket (serveraddr,51706);

Creates a printwriter based on the established socket, sending the information through this object to the server, which contains three parts: OutputStreamWriter, BufferedWriter, PrintWriter.

PrintWriter out = new printwriter ( new BufferedWriter ( New OutputStreamWriter ( Socket.getoutputstream ())),true);

The above is an example of how Android communicates with Java server running on a PC. Since I do not have a real-world test, the effect is not noticeable on the emulator. As shown in 1. If other friends have a better way, hope to communicate with me. Thank you for that.

Android mobile app development experience via socket (TCP/IP) and PC communication

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.