当前位置: 首页 > 面试题库 >

线程“ main”中的异常java.net.ConnectException:连接被拒绝:connect套接字编程Java

文凯康
2023-03-14
问题内容

我最近了解了客户端和服务器之间的套接字编程。因此,我想到了进行客户端和服务器连接的练习。但是,当我尝试运行代码时遇到了以下错误消息: 线程“ main”
java.net.ConnectException中的异常:连接被拒绝:connect

这是我的客户类代码:

public class clientpart {
    public static void main(String[]args) throws UnknownHostException, IOException {
        Scanner input = new Scanner(System.in);
        int port = 8080;
        String host=null;
        String answer; String sendMessage; String receivedMessage;
        InetAddress address = InetAddress.getByName(host);
        Socket socket= new Socket(address,port);

        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);

        System.out.println("Please answered the following question: ");
        System.out.println("What is the subject code for Socket Programming?");
        answer = input.nextLine();

        sendMessage = answer;
        bw.write(sendMessage);
        bw.newLine();
        bw.flush();
        System.out.println("Message sent to server: "+sendMessage);

        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        receivedMessage = br.readLine();
        System.out.println("Message received from server : " + receivedMessage);
    }
}

这是我的服务器代码:

public class serverpart {
    public static Socket socket;
    public static void main(String[]args) throws IOException {
        int port = 8080;
        String answer; String returnedMessage; String reply;
        ServerSocket server = new ServerSocket(port);
        System.out.println("Server start at port "+port+".");

        while(true)
        {
            socket = server.accept();
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            answer = br.readLine();
            System.out.println("Message sent from client: " + answer);

            if("NET 3202".equals(answer) || "net 3202".equals(answer) || "NET3202".equals(answer) || "net3202".equals(answer)){
                reply = "Correct!";
                returnedMessage = reply;
            }
            else{
                reply = "Wrong!";
                returnedMessage = reply;
            }

            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(returnedMessage);
            bw.newLine();
            System.out.println("Message replied to client: "+returnedMessage);
            bw.flush();
        }
    }
}

完整的错误消息是:

Exception in thread "main" java.net.ConnectException: Connection refused: 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:244)
at clientpart.main(clientpart.java:13)
C:\Users\PeiErn\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

希望有人能帮助我,谢谢。


问题答案:

您的程序中有2个问题:

  1. 您使用的端口80众所周知的端口系统端口的一部分 (0到1023),因此您需要使用管理员权限启动服务器或进行更改8080
  2. 您忘了bw.newLine()每次调用后都bw.write(sendMessage)等待,因为它一直等待,因为在您呼叫的另一侧,br.readLine()这意味着它在等待整个行的同时不发送行末字符。

为此更改代码:

服务器部分:

public class serverpart {
    public static Socket socket;
    public static void main(String[]args) throws IOException {
        int port = 8080;
        ...
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(returnedMessage);
            bw.newLine();
            ...

输出:

Server start at port 8080.
Accepted
Message sent from client: net3202
Message replied to client: Correct!

客户部分:

public class clientpart {
    public static void main(String[]args) throws IOException {
        Scanner input = new Scanner(System.in);
        int port = 8080;
        ...
        bw.write(sendMessage);
        bw.newLine();
        bw.flush();
        ...

输出:

Please answered the following question: 
What is the subject code for Socket Programming?
net3202
Message sent to server: net3202
Message received from server : Correct!


 类似资料:
  • 问题内容: 我正在尝试实现TCP连接,从服务器端一切正常,但是当我运行客户端程序(从客户端计算机)时,出现以下错误: 我尝试更改套接字号以防万一,但无济于事,有谁知道导致此错误的原因和解决方法。 服务器代码: 客户代码: 问题答案: 此异常意味着你尝试连接的IP /端口上没有侦听服务: 你试图连接到错误的IP /主机或端口。 你尚未启动服务器。 你的服务器没有监听连接。 在Windows服务器上,

  • 当我在配置单元中运行以下查询时: 这怎么解决呢? 谢谢

  • 我正在尝试创建一个简单的eureka服务器,它将检测其他微服务,并且所有其他微服务都可以通过它进行对话。我使用的是spring Boot2.0.3。 java文件(EurekaServerApplication.java服务器) 我尝试将两边的所有属性移动到bootstrap.application文件中,但没有成功。 我尝试了这个解决方案:https://github.com/spring-cl

  • 我试图实现一个TCP连接,在服务器端一切正常,但当我运行客户端程序(从客户端计算机)时,我得到以下错误: 服务器代码: 客户端代码:

  • 我正在使用Spring 5网络流量、SpringBoot 2和Reactive MongoDB构建一个POC。在使用maven插件构建项目时,我得到了如下错误MongoDB连接错误:- 以下是我的申请表。属性文件:- 波姆。xml:- 请告知这里可能存在什么问题?我是否需要在我的机器上显式安装MongoDB并在运行此项目之前运行它。我相信MongoDB实例将自行运行,因为SpringBoot将在运