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)
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