Andorid - wifi+socket

    xiaoxiao2023-03-28  7

    wifi建立连接后你可以得到自己的一个ip地址,然后使用这个ip地址就可以通信了。

    Wifi相关的类:

    mWifiManager = (wifiManager)getSystemService(Context.WIFI_SERVICE); mWifiInfo = mWifiManager.getConnectionInfo(); int ipAddr = mWifiInfo.getIpAddress()//得到手机ipaddr (ipAddr & 0xFF + ipAddr >> 8 & 0xFF + ipAddr >> 16 & 0xFF + ipAddr >> 24 & 0xFF);

    Socket相关用法:

    SocketAddress remoteAddr = new InetSocketAddress(ipAddr, port)//用wifiManager得到的ip地址和端口号; m_socket = new Socket(); m_socket.setTcpNoDelay(true); // 实时传输数据 m_socket.connect(remoteAddr, 2000); //读操作 private byte[] readBuf = new byte[MAX_PACKAGE_LEN]; DataInputStream m_read; m_read = new DataInputStream(m_soket.getInputStream); int length = m_read.read(readBuf, 0, MAX_PACKAGE_LEN); //写操作 private byte[] sendBuf = new byte[MAX_PACKAGE_LEN]; DataOutputStream m_send; m_send = new DataOutputStream(m_socket.getOutputStream()); m_send.write(sendBuf, 0, sendLength);
    转载请注明原文地址: https://ju.6miu.com/read-1209300.html
    最新回复(0)