简体   繁体   中英

Socket connection from PC to device fails

I'm using sockets for a client-server application (ie the client sends data and the server received the data). My code is below. When I use the client in an Android 5.1 application on a tablet and the server in a Java application on a Windows 7 PC it works very well (ie the tablet can send data to the PC). But when I try to use the server code on the Android 5.1 application on the tablet and the client code on the PC (ie sending data from the PC to the tablet) the connection cannot be established and I'm getting the error:

java.net.ConnectException: Connection timed out: connect

The tablet and the PC are in the same network.

What is wrong?

Server:

ServerSocket ss = new ServerSocket(13005);
Socket socket = ss.accept();
ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));

Client:

socket = new Socket();
socket.connect(new InetSocketAddress("10.2.130.125", 13005));
oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));

Edit:

I have now executed netstat -nap . The output is as follows (I have altered the foreign addresses slightly). 13005 seems not to be used.

Proto Recv-Q Send-Q Local Address          Foreign Address        State
 tcp     417      0 10.2.130.125:36844     xxx.xxx.23.74:443      CLOSE_WAIT
 tcp     417      0 10.2.130.125:35996     xxx.xxx.23.110:443     CLOSE_WAIT
tcp6       1      0 ::ffff:10.2.130.125:44994 ::ffff:xxx.xx.205.170:443 CLOSE_WA
IT
tcp6       0      0 ::ffff:10.2.130.125:43995 ::ffff:xx.xx.119.188:5228 ESTABL
ISHED
tcp6       1      0 ::ffff:10.2.130.125:42353 ::ffff:xx.xx.23.110:80 CLOSE_WAI
T
tcp6       1      0 ::ffff:10.2.130.125:35722 ::ffff:xx.xx.205.170:443 CLOSE_WA
IT
tcp6       1      0 ::ffff:10.2.130.125:48101 ::ffff:xx.xx.205.110:443 CLOSE_WA
IT

I have also executed nmap -sS -Pn -p- 10.2.130.125 . The output is:

Starting Nmap 7.60 ( https://nmap.org ) at 2018-01-18 16:10 W. Europe Standard T
ime
Nmap scan report for public-docking-cx-0635.ethz.ch (10.2.130.125)
Host is up (0.059s latency).
Not shown: 65534 filtered ports
PORT   STATE  SERVICE
22/tcp closed ssh

Nmap done: 1 IP address (1 host up) scanned in 159.90 seconds

Here is the stacktrace from the client. It is just a timeout exception. From the server I don't get any exception.

java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at inf.ethz.ch.streaming.server.main.Main$1.run(Main.java:164)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Maybe the port 13005 is being used, probably the last connection was not closed.

Connect your device via USB. Then go to the folder where you have adb and run the following:

  1. adb shell
  2. netstat -nap

This will show you all the active ports in the android device. You will be able to check if port 13005 is being used.

If you have lot of connections, then is better to apply grep to step 2.

  1. netstat -na |grep 13005

Then try to use another port, for example 5001. If the port is not restricted and is free, you should be able to connect.

Are you sure that the port is available from outside? You have to open the port in your router configuration.

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