客户端服务器应用程序中的 Java 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7872115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:46:59  来源:igfitidea点击:

Java Connection refused in Client server application

java

提问by VibeeshanRC

I am building a Client server app in java ,here is my code

我正在用 Java 构建客户端服务器应用程序,这是我的代码

Client

客户

import java.net.*;
import java.io.*;

class ClientCode{
    public static void main(String args[]) throws Exception {
        int character;
        Socket socket = new Socket("112.134.214.53", 8765);
 //i have put my public ip instead of 127.0.0.1 in order to test it is working through the internet or not
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        String string = "Hello!\n";
        byte buffer[] = string.getBytes();
        out.write(buffer);
        while ((character = in.read()) != -1){
            System.out.print((char) character);
        }
        socket.close();
    }
}

Server

服务器

import java.io.*;
import java.net.*;
public class ServerCode{
    public static void main(String[] args ){
        try{
            ServerSocket socket = new ServerSocket(8765);
            Socket insocket = socket.accept( );
            BufferedReader in = new BufferedReader (new InputStreamReader(insocket.getInputStream()));
            PrintWriter out = new PrintWriter(insocket.getOutputStream(), true);
            String instring = in.readLine();
            out.println("The server got this: " + instring);
            insocket.close();
        }
        catch (Exception e) {}
    }
}

both are running on a same machine connected to internet using my home adsl single port router.

两者都在使用我的家用 adsl 单端口路由器连接到互联网的同一台机器上运行。

Server application run fine but when my client connects to the server through public ip the problem starts ,below is the error message

服务器应用程序运行良好,但是当我的客户端通过公共 ip 连接到服务器时,问题开始了,以下是错误消息

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at ClientCode.main(ClientCode.java:6)

My Kaspersky Network monitor is showing me that my port 8765 is ok and working

我的卡巴斯基网络监视器显示我的端口 8765 正常并且工作正常

回答by Umer Hayat

There may be two possible reasons for this exception to occur:

出现这种异常的原因可能有两种:

1- There is no service listening to the port you are attempting to connect to. For debugging you can try loading http://112.134.214.53:8765using a browser and see if it connects with the server.

1- 没有服务在侦听您尝试连接的端口。对于调试,您可以尝试使用浏览器加载http://112.134.214.53:8765并查看它是否与服务器连接。

2- (If everything else is fine) This could very well be a firewall issue.

2-(如果其他一切正常)这很可能是防火墙问题。

回答by Sam

I've had this problem too. I was using Netbeans and I kept getting "java.net.ConnectException: Connection refused:" But then I tried using jGrasp and it worked... Maybe this can provide some extra troubleshooting help. Also it could be that you just need a Java policy file.

我也遇到过这个问题。我正在使用 Netbeans 并且我不断收到“java.net.ConnectException:连接被拒绝:”但后来我尝试使用 jGrasp 并且它起作用了......也许这可以提供一些额外的故障排除帮助。也可能是您只需要一个 Java 策略文件。