简体   繁体   中英

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

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. You may wish to try using the TcpListener class, that way you can bind it to the IP address (network adapter) you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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