服務端:java
import android.content.Context; import android.net.wifi.WifiManager; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; /** * Created by Zzm_Pc on 2016/12/28. */ public class StartingServerUtils { private InputStream input; private NewServerThread newServerThread; private Handler wifiHandler; private WifiAcceptThread acceptThread; private WifiServerThread serverThread; private WifiManager wifiManager; private ServerSocket wifiServerSocket; private Socket wifiSocket; private Context me; public StartingServerUtils(Context me, Handler handler) { this.me = me; this.wifiHandler = handler; } //accept connect private class WifiServerThread extends Thread { @Override public void run() { super.run(); Log.i("information","WifiServerThread~~"); try { wifiServerSocket = new ServerSocket(1099); wifiSocket = wifiServerSocket.accept(); if (wifiSocket != null) { input = wifiSocket.getInputStream(); Log.i("information", "鏈接成功~~"); wifiServerSocket.close(); acceptThread.start(); newServerThread.start(); } } catch (IOException e) { Log.i("information", e.toString()); } } } //turn string_ip method private String getStringIp(int ip) { return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF); } //accept information thread private class WifiAcceptThread extends Thread { private boolean is=true; @Override public void run() { super.run(); Log.i("information","wifiAccept~~"); if (input != null) { while (is) { Log.i("information","讀取~~"); byte[] bytes = new byte[1024]; int length = 0; try { if (input!=null && (length = input.read(bytes)) != -1) { Message msg = new Message(); msg.obj = new String(bytes, 0, length); wifiHandler.sendMessage(msg); } } catch (IOException e) { is=false; Log.i("information", "讀取異常~~"); } } } } } //開啓服務的方法 public void startServer() { wifiManager = (WifiManager) me.getSystemService(Context.WIFI_SERVICE); while (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); Toast.makeText(me, "請開wifi~~", Toast.LENGTH_SHORT).show(); } String ip = getStringIp(wifiManager.getConnectionInfo().getIpAddress()); //展現ip // newServerThread = new NewServerThread(); acceptThread = new WifiAcceptThread(); serverThread = new WifiServerThread(); serverThread.start(); } //是否斷開鏈接,斷開則從新開啓服務 private class NewServerThread extends Thread { private boolean is = true; @Override public void run() { super.run(); Log.i("information","NewServerThread~~"); while (is) { try { wifiSocket.sendUrgentData(9); } catch (IOException e) { Log.i("information", "斷開鏈接了~~"); is = false; serverThread.interrupt(); acceptThread.interrupt(); wifiServerSocket = null; try { wifiSocket.close(); input.close(); } catch (IOException e1) { Log.i("information", "Socket關閉失敗~"); } input = null; wifiSocket = null; wifiManager = null; startServer(); } } Log.i("information","將會從新開啓服務~~"); } } }
客戶端:android
public class ConnectServerUtils { public static String ip = "192.168.1.89"; private AppCompatActivity me; private WifiManager wifiManager; private OutputStream output; static Socket wifiSocket; public ConnectServerUtils(AppCompatActivity me) { this.me = me; } private class WifiConnectThread extends Thread { @Override public void run() { super.run(); try { wifiSocket = new Socket(ip, 1099); output = wifiSocket.getOutputStream(); if (output != null) { showToast("控制鏈接成功~~"); } else { showToast("控制鏈接失敗~請重試!"); } } catch (IOException e) { Log.i("information", e.toString()); } } } private void showToast(final String toastInformation) { me.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(me, toastInformation, Toast.LENGTH_SHORT).show(); } }); } public OutputStream startConnectServer() { wifiManager = (WifiManager) me.getSystemService(Context.WIFI_SERVICE); if (!wifiManager.isWifiEnabled()) { Toast.makeText(me, "請開啓wifi~~~~", Toast.LENGTH_SHORT).show(); wifiManager.setWifiEnabled(true); } Log.i("information", getStringIp(wifiManager.getConnectionInfo().getIpAddress())); new WifiConnectThread().start(); SystemClock.sleep(500); return output; } //turn string_ip method private String getStringIp(int ip) { return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF); } }
最後記得要關閉:ide
@Override protected void onDestroy() { super.onDestroy(); try { ConnectServerUtils.wifiSocket.close(); } catch (IOException e) { Log.i("information", e.toString()); } ConnectServerUtils.wifiSocket = null; }
否則按返回鍵的時候,再次鏈接,服務端會拒絕你的鏈接,只要加上以上代碼就好了。this