Android连接到网络共享套接字 [英] Android connect to tethering socket

查看:113
本文介绍了Android连接到网络共享套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个android设备,其中1个充当处于共享模式的服务器,并接受与端口的连接. 另一台设备充当客户端,该客户端连接到服务器热点,并通过端口建立与服务器的连接. (一切都在中间没有路由器的情况下完成.)

I have 2 android devices, one acts as a server that is in tethering mode, and accepting connections to a port. The other device acts as a client that connects to the server hotspot and establishes a connection to the server via a port. (Everything is done without a router in the middle).

服务器代码如下:(当android服务器执行该代码时,设备处于网络共享模式)

The server code looks like this: (When the android server executes the code, the device is in tethering mode)

ServerSocket server = new ServerSocket(PORT);

while (true) {
    Socket client;
    client = server.accept();
    ClientThread com = new ClientThread(client, this);
    Thread t = new Thread(com);
    t.start();
}

客户端代码如下:(当android客户端执行此代码时,设备已连接到接入点)

The client code looks like this: (when the android client executes this code, the device is connected to access point)

Socket client = new Socket();
int serverIP = wifiManager.getDhcpInfo().serverAddress;
String stringIP = android.text.format.Formatter.formatIpAddress(serverIP);
InetAddress address = InetAddress.getByName(stringIP);
SocketAddress socketAddress = new InetSocketAddress(address, PORT);
client.connect(socketAddress);

我在客户端收到以下错误

I get the following error on the client

java.net.ConnectException: failed to connect to /192.168.43.1 (port 12345): connect failed: ENETUNREACH (Network is unreachable)
  at libcore.io.IoBridge.connect(IoBridge.java:114)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  at java.net.Socket.connect(Socket.java:842)
  at java.net.Socket.connect(Socket.java:785)
  at com.alternatewifimeshmessaging.HostManager.clientConnect(HostManager.java:283)
  at com.alternatewifimeshmessaging.HostManager.client(HostManager.java:189)
  at com.alternatewifimeshmessaging.HostManager.run(HostManager.java:59)
  at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
  at libcore.io.Posix.connect(Native Method)
  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  at libcore.io.IoBridge.connect(IoBridge.java:112)
... 8 more

服务器没有错误.

我在android清单文件中设置了以下权限:

I have the following permission set in the android manifest file:

<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>

理想情况下,我需要将一些数据从服务器传输到客户端.

Ideally I would need to transfer some data from server to the client.

我在做什么错了?

推荐答案

通过测试证明无法在网络共享设备上建立连接.

By testing it turns out the connection cannot be established on the tethering device.

但是,如果我们撤消该过程并在连接的客户端上打开服务器插槽.并从网络共享设备连接到它,它将正常工作.

But if we reverse the process and open a serversocket on the connected client. And connect to it from the tethering device, it will work.

所以逆向交流才是答案.

So reversing the communication is the answer.

这篇关于Android连接到网络共享套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆