android使用socket進行本地PC通訊

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   使用   

一、編寫思路

   

 手機端為用戶端,PC為服務端,手機訪問PC通訊,需建立一個虛擬通訊鏈路,用戶端通過socket發送請求到服務端,服務端通過serversocket監聽來自用戶端的socket請求,並產生一個socket。這樣就建立了一條虛擬通訊網路,然後再通過相關方法進行通訊。項目需在服務端建立一個java程式,用戶端建立一個android程式。

二、代碼編寫

   (一) PC端的代碼編寫——java程式

          (1)相關方法

                 Socket accept() :如果接收到一個來自用戶端的socket的串連請求,該方法將返回一個與串連用戶端對應的socket,否則該方法一直處於等待狀態,線程也被阻塞。

                 Serversocket(int port ): 用指定的連接埠port來建立一個serversocket。該連接埠應該是一個有效連接埠整數值:0 --65535 。查詢PC的連接埠:提示命令符下輸入                  

                                                    netstat -na ,選擇上面沒有的連接埠。

           (2)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

                           // 建立一個serversocket,用於監聽用戶端socket的串連請求
                          ServerSocket ss = new ServerSocket(3000);
                 

                           // 採用迴圈不斷接受來自用戶端的請求
                           while (true) {
                 

                           // 每當接受到用戶端socket的請求,服務端也產生一個socket
                           Socket s = ss.accept();
                           OutputStream os = s.getOutputStream();
                           os.write("您好,您收到了伺服器的信念祝福\n".getBytes("utf-8"));

                           // 關閉輸出資料流,關閉socket
                          os.close();
                          s.close();
                                }

                            }
                        }

 

    (二)android程式

           (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

                     // 建立一個serversocket,用於監聽用戶端socket的串連請求
                         ServerSocket ss = new ServerSocket(3000);
                     // 採用迴圈不斷接受來自用戶端的請求
                         while (true) {
                     // 每當接受到用戶端socket的請求,服務端也產生一個socket
                         Socket s = ss.accept();
                         OutputStream os = s.getOutputStream();
                         os.write("您好,您收到了伺服器的信念祝福\n".getBytes("utf-8"));
                     // 關閉輸出資料流,關閉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="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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.