安卓TCP通訊

來源:互聯網
上載者:User

標籤:

tCPServer.java

伺服器的兩個socket都沒有關閉:s,ss.兩個都是一直開啟,然後資料才有顯示

 class tcpServer{    public static void main(String[] args) throws IOException{         //建立服務端socket服務,並監聽連接埠     ServerSocket ss =new ServerSocket(30000);// 採用迴圈不斷接受來自用戶端的請求 while (true) {     //通過accept方法擷取連結過來的用戶端對象(s中有內容,連接埠,IP屬性)     Socket s = ss.accept();     /*接收行動數據*/     //IP:     String ip =s.getInetAddress().getHostAddress();     System.out.println(ip+"...串連成功" );     //內容:擷取用戶端發送過來的資料,那麼要使用用戶端對象s     InputStream in = s.getInputStream();     byte[] buf =new byte[1024];     int len=in.read(buf);     String content = new String(buf,0,len);     System.out.println("內容:"+content );    /*回傳給行動數據*/     OutputStream os = s.getOutputStream();            os.write("歡迎回來學安卓,您收到了泡泡的祝福!\n"                .getBytes("utf-8"));     //s.close();//關閉用戶端,伺服器可以控制客戶     //ss.close();//關閉服務端,可選操作 }}}
View Code

安卓後台代碼:

package com.simpleclient;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.ServerSocket;import java.net.Socket;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;public class MainActivity extends Activity {    EditText show;    String content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                show = (EditText) findViewById(R.id.show);        new Thread()        {            @Override            public void run()            {                try                {                    // 建立串連到遠程伺服器的Socket                    Socket socket = new Socket("192.168.1.108" , 30000);  //①                    String lineWrite = "tcp:mobilephone/3.1415926";                     //向服務端發送資料                      BufferedWriter bufwriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));                                        bufwriter.write(lineWrite);//向服務端發送資料                      bufwriter.newLine();                      bufwriter.flush();                                         // 將Socket對應的輸入資料流封裝成BufferedReader                    BufferedReader br = new BufferedReader(                        new InputStreamReader(socket.getInputStream()));                    // 進行普通IO操作                    String line = br.readLine();                    show.setText("讀取來自伺服器的資料:" + line);                                        // 關閉輸入資料流、socket                    br.close();                    socket.close();                }                catch (IOException e)                {                    e.printStackTrace();                }            }        }.start();            }    }

前台代碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <EditText        android:id="@+id/show"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false"        android:ems="10" >        <requestFocus />    </EditText>  </LinearLayout>
View Code

許可權代碼:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.simpleclient"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".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>
View Code

總體而言,實現了伺服器和用戶端一發一收的效果,但是中文有亂碼現象產生,並且不嚴謹,沒有類封裝

安卓TCP通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.