Android uses socket for local PC communication

Source: Internet
Author: User

I. Writing ideas

The mobile phone end is the client, the PC is the server, and the mobile phone accesses the PC communication. A virtual communication link needs to be established. The client sends a request to the server through socket, and the server listens to socket requests from the client through serversocket, and generate a socket. In this way, a virtual communication network is established, and then communication is performed through relevant methods. The project must establish a java program on the server and an android program on the client.

Ii. Coding

(1) PC-side code writing-java program

(1) related methods

Socket accept (): If a connection request from the client socket is received, this method returns a socket corresponding to the connection client; otherwise, the method remains in the waiting state, the thread is also blocked.

Serversocket (int port): Use the specified port to create a serversocket. This port should be a valid port INTEGER: 0 -- 65535. Query the PC port: Enter the command line

Netstat-na: select the port that is not listed above.

(2) java code

Package com. myServer;

Import java. io. IOException;
Import java. io. OutputStream;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class myServer {

/**
* @ Param args
*/
Public static void main (String [] args) throws IOException {
// TODO Auto-generated method stub

// Create a serversocket for listening to client socket connection requests
ServerSocket ss = new ServerSocket (3000 );

// Uses a loop to continuously accept requests from the client
While (true ){

// The server also generates a socket whenever a request is received from the client socket.
Socket s = ss. accept ();
OutputStream OS = s. getOutputStream ();
OS. write ("Hello, you have received the server's belief blessing \ n". getBytes ("UTF-8 "));

// Close the output stream and socket
OS. close ();
S. close ();
}

}
}

 

(2) android Program

(1) MainActivity. java

Package com. myServer;

Import java. io. IOException;
Import java. io. OutputStream;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class myServer {

/**
* @ Param args
*/
Public static void main (String [] args) throws IOException {
// TODO Auto-generated method stub

// Create a serversocket for listening to client socket connection requests
ServerSocket ss = new ServerSocket (3000 );
// Uses a loop to continuously accept requests from the client
While (true ){
// The server also generates a socket whenever a request is received from the client socket.
Socket s = ss. accept ();
OutputStream OS = s. getOutputStream ();
OS. write ("Hello, you have received the server's belief blessing \ n". getBytes ("UTF-8 "));
// Close the output stream and socket
OS. close ();
S. close ();
}

}
}

 

(2) activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/LinearLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
<EditText
Android: id = "@ + id/show"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: editable = "false"
Android: cursorVisible = "false"
/>

</LinearLayout>

(3) AndroidManifest. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. myclient"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "14"/>

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

<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "com. myclient. MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>

</Manifest>

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.