数据传输Socket,欧洲人用手机几乎不花钱

日期:2019-08-21编辑作者:教育留学

图片 1

首先,描述下功能,两个手机,其中一个手机开热点,另外一个手机连接热点,2个手机间数据通信。简述一下原理:开热点的手机相当于路由器,连接的手机作为客户端,获取路由器的IP建立Socket 连接,开始双方通信。

我要是买得起,首选这辆跑车

图片 2

  顺便提一下在国内同样代表人身份的手机。欧洲的手机样式也不少,价格也不菲,好一点的需两三百欧元。不过,欧洲人用手机却几乎不花钱。您知道是为什么吗?有兴趣的,一起来做一下功课。

开热点的作为服务器端

本文选自《北行剑客》的博客,点击查看博客原文

初始化数据:

上一页 1 2 3 4 下一页
 public static final int DEVICE_CONNECTING = 1;//有设备正在连接热点 public static final int DEVICE_CONNECTED = 2;//有设备连上热点 public static final int SEND_MSG_SUCCSEE = 3;//发送消息成功 public static final int SEND_MSG_ERROR = 4;//发送消息失败 public static final int GET_MSG = 6;//获取新消息 private TextView text_state; private WifiManager wifiManager; /** * 连接线程 */ private ConnectThread connectThread; /** * 监听线程 */ private ListenerThread listenerThread; /** * 热点名称 */ private static final String WIFI_HOTSPOT_SSID = "TEST"; /** * 端口号 */ private static final int PORT = 54321; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.create_wifi).setOnClickListener; findViewById(R.id.close_wifi).setOnClickListener; findViewById(R.id.send).setOnClickListener; text_state =  findViewById(R.id.receive); wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); /** * 先开启监听线程,在开启连接 */ listenerThread = new ListenerThread(PORT, handler); listenerThread.start(); try { Thread.sleep; } catch (InterruptedException e) { e.printStackTrace(); } // 开启连接线程 new Thread(new Runnable() { @Override public void run() { try { Log.i("ip", "getWifiApIpAddress()"   getWifiApIpAddress; //本地路由开启通信 String ip = getWifiApIpAddress(); if (ip != null) { } else { ip = "192.168.43.1"; } Socket socket = new Socket; connectThread = new ConnectThread(MainActivity.this, socket, handler); connectThread.start(); } catch (IOException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { text_state.setText; } }); } } }).start(); }

创建wifi 热点,之后开启Socket 监听线程

 /** * 创建Wifi热点 */ private void createWifiHotspot() { if (wifiManager.isWifiEnabled { //如果wifi处于打开状态,则关闭wifi, wifiManager.setWifiEnabled; } final WifiConfiguration config = new WifiConfiguration(); config.SSID = WIFI_HOTSPOT_SSID; config.preSharedKey = "123456789"; config.hiddenSSID = false; config.allowedAuthAlgorithms .set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; //通过反射调用设置热点 //192.168.43.59 // Log.i("ip", "getWifiApIpAddress()"   getWifiApIpAddress()   // "n"); try { Method method = wifiManager.getClass().getMethod( "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean enable =  method.invoke(wifiManager, config, true); if  { text_state.setText("热点已开启 SSID:"   WIFI_HOTSPOT_SSID   " password:123456789"); // 开启连接线程 new Thread(new Runnable() { @Override public void run() { try { Log.i("ip", "getWifiApIpAddress()"   getWifiApIpAddress; String ip = getWifiApIpAddress(); if (ip != null) { } else { //一般Android手机默认路由是 ip = "192.168.43.1"; } //本地路由开启通信 Socket socket = new Socket; connectThread = new ConnectThread(MainActivity.this, socket, handler); connectThread.start(); } catch (IOException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { text_state.setText; } }); } } }).start(); Thread.sleep; // listenerThread = new ListenerThread(PORT, handler); // listenerThread.start(); } else { text_state.setText; } } catch (Exception e) { e.printStackTrace(); text_state.setText; } }

服务器端获取本地路由:

 public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements { NetworkInterface intf = en.nextElement(); if (intf.getName().contains { for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d("Main", inetAddress.getHostAddress; return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e("Main", ex.toString; } return null; }

关闭热点

 /** * 关闭WiFi热点 */ public void closeWifiHotspot() { try { Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); method.setAccessible; WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager); Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method2.invoke(wifiManager, config, false); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } text_state.setText; }

ListenerThread 监听线程

import android.os.Handler;import android.os.Message;import android.util.Log;import com.example.syhuang.wifiserver.MainActivity;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;/** * 监听线程 * Created by syh on 2018/4/3 */public class ListenerThread extends Thread { private ServerSocket serverSocket = null; private Handler handler; private int port; private Socket socket; public ListenerThread(int port, Handler handler) { setName("ListenerThread"); this.port = port; this.handler = handler; try { serverSocket = new ServerSocket;//监听本机的12345端口 } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { while  { try { Log.i("ListennerThread", "阻塞"); //阻塞,等待设备连接 if (serverSocket != null) socket = serverSocket.accept(); Message message = Message.obtain(); message.what = MainActivity.DEVICE_CONNECTING; handler.sendMessage; } catch (IOException e) { Log.i("ListennerThread", "error:"   e.getMessage; e.printStackTrace(); } } } public Socket getSocket() { return socket; }}

本文由4886a威尼斯城官网发布于教育留学,转载请注明出处:数据传输Socket,欧洲人用手机几乎不花钱

关键词:

美利坚联邦合众国高校的十大,罪之艺术和措施

8) Those artsy16) types at the Rhode Island School of Design can putdown their paintbrushes and take “The Art of Sin and the Sin of Art”,which cont...

详细>>

是购买力而不是直接汇率决定生活状况,也只能

废话了这么多,主旨只有一句:凡是美国买得到的东西,大多用不着携带。那到底什么才是美国买不到的东西呢? 我...

详细>>

世界十大最不可相信的亲热,公务员插手嗅觉相

最牛的相亲 本文选自《 沙漠富翁 》的博客,点击 查看博客原文 公务员参加嗅觉相亲 北京50余名公务员在建外现代城...

详细>>

黄河登山高校在定日,小小留学生高原上捐助资

接到外孙子的短信后,阿爸罗建强和母亲张晴明急如星火。几经周折联系上了双鸭山的多少个相恋的人,这几个朋友...

详细>>