日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android简单实现Socket通信,客户端连接服务器后,服务器向客户端发送文字数据

發(fā)布時間:2024/1/17 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android简单实现Socket通信,客户端连接服务器后,服务器向客户端发送文字数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

案例實現(xiàn)的是簡單的Socket通信,當(dāng)客戶端(Android客戶端)連接到指定服務(wù)器以后,服務(wù)器向客戶端發(fā)送一句話文字信息(你可以拓展其它的了)

先看一下服務(wù)端程序的實現(xiàn)吧

Server.java

import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket;public class Server {/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub// 創(chuàng)建一個ServerSocket,用于監(jiān)聽客戶端socket的連接請求 ServerSocket ss = new ServerSocket(30000); // 采用循環(huán)不斷接受來自客戶端的請求,服務(wù)器端也對應(yīng)產(chǎn)生一個Socket while (true) { Socket s = ss.accept(); OutputStream os = s.getOutputStream(); os.write("你好,因為你上線了,所以服務(wù)器發(fā)給了你這條信息".getBytes("utf-8")); os.close(); s.close(); }} }

接下來實現(xiàn)的就是手機客戶端的上線并接收數(shù)據(jù)了,看一下 MainActivity.java

package com.example.socket;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;public class MainActivity extends Activity { private TextView display; private Handler handler; private String host; private Button btn; private Socket socket; private String line; private EditText et;@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler = new Handler(); btn = (Button) findViewById(R.id.send); et = (EditText)findViewById(R.id.editText1); et.setText("192.168.1.100"); OnClickListener listener = new OnClickListener() {@Override public void onClick(View v) { // TODO Auto-generated method stub new Thread() { public void run() { try { host = et.getText().toString(); socket = new Socket(host, 30000); // 設(shè)置10秒之后即認為是超時 socket.setSoTimeout(10000);BufferedReader br = new BufferedReader( new InputStreamReader( socket.getInputStream())); line = br.readLine(); Log.d("Read:", line);br.close(); socket.close();} catch (UnknownHostException e) { // TODO Auto-generated catch block Log.e("UnknownHost", "沒找到主機"); e.printStackTrace(); } catch (IOException e) { Log.e("IOException", "輸入輸出出現(xiàn)錯誤"); // TODO Auto-generated catch block e.printStackTrace(); } handler.post(new Runnable(){@Override public void run() { // TODO Auto-generated method stub display = (TextView) findViewById(R.id.display); display.setText(line); }}); } }.start(); } }; btn.setOnClickListener(listener); } }

這里需要注意的是,與網(wǎng)絡(luò)相關(guān)的更新UI界面的一定不以在主線程中進行更,必須要用到Handler實現(xiàn)UI更新

好了,看一下布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" ><TextView android:id="@+id/display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /><Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="72dp" android:text="Link to Server" /><EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="18dp" android:ems="10" ><requestFocus /> </EditText></RelativeLayout> 

最后,不得不提的是,因為用到了網(wǎng)絡(luò),所以在AndroidMainFest.xml中千萬不要忘記加上以下權(quán)限

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

總結(jié)

以上是生活随笔為你收集整理的Android简单实现Socket通信,客户端连接服务器后,服务器向客户端发送文字数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。