Android phone and computer communication in WiFi environment

Source: Internet
Author: User

I have written a Java implementation of the most basic socket network communication, this is similar to the previous one, just porting the client code to the phone, and then get the native IP method slightly different.

Let's take a look at the use of Android studio in this article.

3 basic steps to develop Android using Android Studio:

(1) New project

(2) Define the user interface of the application in the XML layout file.

Click Design and text in the circle to toggle the interface

(3) Write the business implementation in Java code.

It's used to write Java code.

In addition, this article also needs to add user rights

OK, here's the code:

Android server-side code:

 PackageCom.example.x_yp.socket;;;;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView;*;*; Public classMainactivityextendsactionbaractivity{//define the listening port number    Final intServer_port = 30000; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Print the IP address of this machineTextView myip=(TextView) Findviewbyid (R.ID.MYIP);        Myip.settext (Getlocalipaddress ()); //open a new thread to listen for client connections       NewThread () { Public voidrun () {startserver ();    }}.start (); }    //get the IP address under the local WiFi    PrivateString getlocalipaddress () {Wifimanager Wifimanager=(Wifimanager) Getsystemservice (Wifi_service); Wifiinfo Wifiinfo=Wifimanager.getconnectioninfo (); //get 32-bit integer IP address        intIpAddress =wifiinfo.getipaddress (); //return integer address converted to "*.*.*.*" address        returnString.Format ("%d.%d.%d.%d", (ipAddress& 0xFF), (ipAddress >> 8 & 0xFF), (ipAddress>> & 0xFF), (ipAddress >> & 0xFF)); }    //for socket Connection communication    Private voidStartServer () {Try{serversocket ServerSocket=NewServerSocket (Server_port); //Loop listening for client connection requests             while(true) {Socket ss=serversocket.accept (); Try                {                    //wraps the output stream corresponding to the socket into a printstreamPrintStream PS =NewPrintStream (Ss.getoutputstream ()); //perform normal IO operationsPs.println ("Hello,you has recevied Sever ' s Messege"); //turn off the output stream and close the socketPs.close ();                Ss.close (); }                Catch(Exception e) {e.printstacktrace (); }                finally{ss.close (); } thread.sleep (3000); }        }        Catch(Exception e) {e.printstacktrace (); }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = {            return true; }        return Super. onoptionsitemselected (item); }}

PC Client code:

/**PC-side code PC as client, connect server based on server IP address and port number*/*;*; Public classpc_client{ Public Static voidMain (string[] args)throwsIOException {//SOCKET socket = new Socket ("", 30000);//Here's the IP address to fill out the IP address of the mobile serverSocket socket =NewSocket ("", 30000);//Here's the IP address to fill out the IP address of the mobile server//wraps the input stream corresponding to the socket into a bufferedreaderBufferedReader br =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //perform normal IO operationsString line =Br.readline (); System.out.println ("Data from the server:" +Line ); //close the input stream, socketBr.close ();    Socket.close (); }}

OK, download to phone, mobile phone and computer can communicate

Add the content of the graphical interface used:

After adding the controls here, set an ID, which is set to Myip

The Java program then connects to the control via the Findviewbyid function.

Here with TextView myip= (TextView) Findviewbyid (R.ID.MYIP);

You can then control the control in Java.

Android phone and computer communication in WiFi environment

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: 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.