简体   繁体   English

Android应用程序将无法连接到无线连接到网络的计算机

[英]Android app won't connect to computer that's connected wirelessly to network

The app uses sockets to connect to the computer but will only connect if the computer is connected to the network by an ethernet cable. 该应用程序使用套接字连接到计算机,但仅在计算机通过以太网电缆连接到网络时才可以连接。 I've tried disabling the firewalls but that makes no difference. 我已经尝试禁用防火墙,但这没什么区别。

The code for the server on the computer: 计算机上服务器的代码:

int port = 7936;        

while(true){
ServerSocket server = new ServerSocket(port);
System.out.println("Waiting for client ...");
Socket client = server.accept();
System.out.println("Client from "+client.getInetAddress()+" connected");

InputStream in = client.getInputStream();

and the code for the client on the app: 以及应用程序上客户端的代码:

Socket socket = new Socket(address,7936);
OutputStream out = socket.getOutputStream();
String action = "2";
byte[] actByte = action.getBytes();
out.write(actByte);
socket.close();

Address is defined by user input and all the permissions needed have been set in the manifest xml file. 地址由用户输入定义,并且所有所需的权限都已在清单xml文件中设置。 Thanks for the help. 谢谢您的帮助。

Edit 编辑

Sorry for the delay in responding to the answers given. 很抱歉延迟回答给出的答案。 I have since been able to try the program on a different network and it works with the computer connected wirelessly so it looks like the issue was with the network rather than the code. 从那时起,我就能够在其他网络上尝试该程序,并且该程序可与无线连接的计算机一起使用,因此看起来问题出在网络而不是代码。

Thanks to everyone for answering and I'm sorry it took me so long to respond. 感谢大家的回答,很抱歉,我花了这么长时间回复。

As others have mentioned, more information is needed. 正如其他人提到的那样,需要更多信息。 When you disconnect the computer from the wired connection, I assume it is switching over to wifi and you've verified that you are online. 当您断开计算机与有线连接的连接时,我认为计算机正在切换至wifi,并且您已确认自己处于在线状态。 Your computer is likely to get a different IP address if you're DHCP as the interface changed and so wouldn't the MAC address. 如果您是DHCP的话,您的计算机可能会获得不同的IP地址,因为接口更改了,MAC地址也不会更改。

Check the address on the computer and verify you have the right address. 检查计算机上的地址,并确认您使用的地址正确。

What I'm thinking is that the socket is not bound to the IP address you think it is. 我在想的是套接字未绑定到您认为是的IP地址。 You may wish to try using the TcpListener class, that way you can bind it to the IP address (network adapter) you want. 您可能希望尝试使用TcpListener类,这样可以将其绑定到所需的IP地址(网络适配器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM