1、在.xml文件中添加權限?
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.INTERNET"/>
2、修改顯示界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="服務器ip地址:" /><EditTextandroid:id="@+id/ipEditText"android:layout_width="200dp"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/port_TextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="服務器端口號:" /><EditTextandroid:id="@+id/port_EditText"android:layout_width="200dp"android:layout_height="wrap_content"android:text="8080"android:layout_below="@id/port_TextView" /><Buttonandroid:id="@+id/start_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="啟動" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><EditTextandroid:id="@+id/sendData_EditText"android:layout_width="300dp"android:layout_height="wrap_content"android:layout_below="@id/port_TextView" /><Buttonandroid:id="@+id/sendData_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="發送消息" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="接收消息:" /><TextViewandroid:id="@+id/receiveTextView"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></LinearLayout>
3、獲取手機的ip地址,方便客戶端連接
private String getLocalIpAddress() {WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);WifiInfo wifiInfo = wifiManager.getConnectionInfo();// 獲取32位整型IP地址int ipAddress = wifiInfo.getIpAddress();//返回整型地址轉換成“*.*.*.*”地址return String.format("%d.%d.%d.%d",(ipAddress & 0xff), (ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));}
4、為啟動服務器設置監聽事件
//開啟服務器按鈕
startButton.setOnClickListener(startButtonListener);//啟動服務按鈕監聽事件private View.OnClickListener startButtonListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub/*** 啟動服務器監聽線程*/}};
5、創建一個服務器線程
/*** 服務器監聽線程*/class ServerSocket_thread extends Thread{public void run()//重寫Thread的run方法{try{int port =Integer.valueOf(portEditText.getText().toString());//獲取portEditText中的端口號serverSocket = new ServerSocket(port);//監聽port端口,這個程序的通信端口就是port了}catch (IOException e){e.printStackTrace();}while (true){try{clicksSocket = serverSocket.accept(); //監聽連接 ,如果無連接就會處于阻塞狀態,一直在這等著runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){startButton.setText("斷開服務器");}});}catch (IOException e){e.printStackTrace();}}}}
6、發送消息
@Overridepublic void onClick(View v) {//子線程中進行網絡操作new Thread(new Runnable() {@Overridepublic void run() {if(clicksSocket!=null){try {String str = sendDataEditText.getText().toString()+"\r\n";outputStream = clicksSocket.getOutputStream();outputStream.write(str.getBytes());} catch (UnknownHostException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}else{runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"請等待客戶端連接",Toast.LENGTH_SHORT).show();}});}}}).start();}
7、完整工程
注意:此工程有兩個bug,一個是只支持一個客戶端連接,第二個是點擊啟動按鈕后,斷開無效
鏈接:https://pan.baidu.com/s/1PciOp9MOzSbswQ9-R70XSg?pwd=8888?
提取碼:8888