Android uses sockets for local PC communication

Source: Internet
Author: User

First, the preparation of ideas

Mobile phone terminal for the client, the PC for the server, mobile phone access to PC communications, the need to establish a virtual communication link, the client through the socket to send the request to the server, the server through the ServerSocket listener from the client socket request, and generate a socket. This establishes a virtual communication network, which is then communicated through related methods. The project needs to build a Java program on the server, and the client builds an Android program.

Second, code writing

(a) PC-side code writing--java program

(1) Related methods

Socket Accept (): If a connection request is received from the client's socket, the method returns a socket corresponding to the connecting client, otherwise the method is waiting and the thread is blocked.

ServerSocket (int port): Creates a serversocket with the port specified. The port should be a valid port integer value: 0--65535. Querying the port of a PC: prompt command input

Netstat-na, select a port that is not on the top.

(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 to listen for connection requests from the client socket
ServerSocket ss = new ServerSocket (3000);

Using loops to continuously accept requests from clients
while (true) {

Whenever a request to the client socket is received, the server also generates a socket
Socket s = ss.accept ();
OutputStream OS = S.getoutputstream ();
Os.write ("Hello, you have received the server's faith blessing \ n". GetBytes ("Utf-8"));

Turn off the output stream and close the socket
Os.close ();
S.close ();
}

}
}

(ii) 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 to listen for connection requests from the client socket
ServerSocket ss = new ServerSocket (3000);
Using loops to continuously accept requests from clients
while (true) {
Whenever a request to the client socket is received, the server also generates a socket
Socket s = ss.accept ();
OutputStream OS = S.getoutputstream ();
Os.write ("Hello, you have received the server's faith blessing \ n". GetBytes ("Utf-8"));
Turn off the output stream and close the 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" >

<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"

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

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.