简体   繁体   English

连接被拒绝:connect,java socket

[英]Connection refused: connect, java socket

I am new using java socket. 我是使用Java套接字的新手。 I have a problem I can´t solve. 我有一个我无法解决的问题。 I have a server with a public IP, on it java server executes, then i have an android app client. 我有一台具有公共IP的服务器,在其上执行Java服务器,然后有一个android应用程序客户端。 With the socket i get to communicate from the client to the server, the problem comes when i try to do it the opposite way. 使用套接字,我可以从客户端与服务器进行通信,而当我尝试以相反的方式进行操作时,问题就来了。 I tried sending a confirmation message from the server to the client. 我尝试将确认消息从服务器发送到客户端。 It doesn´t matter how many time i try because i always end up having this error: 尝试多少次都无所谓,因为我总是会遇到此错误:

java.net.ConnectException: Connection refused: connect java.net.ConnectException:连接被拒绝:connect

at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at server.ServerEntrada.run(ServerEntrada.java:48)
at java.lang.Thread.run(Thread.java:748)

I have router port 9000 and 9001 open. 我的路由器端口9000和9001已打开。

The client sends through the port 9000 the data, and server recives them. 客户端通过端口9000发送数据,然后服务器接收它们。 Then the server sends them through port 9001 while the app is waiting through 然后,服务器在应用程序等待通过时通过端口9001发送它们

if(password.equals("asd")){
    System.out.println("Abriendo socket");
    socketSalida = new Socket(socketEntrada.getInetAddress().getHostAddress(),puertoSalida);
    DataOutputStream datosSalida = new DataOutputStream(socketSalida.getOutputStream());
    datosSalida.writeUTF("OK");
}

The problem is here. 问题在这里。 Your server is trying to connect to your client, but it can't, as your client is a client, not a server. 您的服务器正在尝试连接到客户端,但是由于您的客户端是客户端而不是服务器,因此无法连接。

And it doesn't need to. 而且不需要。 It already has a connection to the client, the one it read the password from. 它已经与客户端建立了连接,从中读取了密码。 Change it to this: 更改为此:

if(password.equals("asd")){
    System.out.println("Abriendo socket");
    DataOutputStream datosSalida = new DataOutputStream(socketEntrada.getOutputStream());
    datosSalida.writeUTF("OK");
}

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

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