简体   繁体   English

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

[英]Exception in thread “main” java.net.ConnectException: Connection refused: connect Socket Programming Java

I recently learn about Socket Programming between client and server. 我最近了解了客户端和服务器之间的套接字编程。 So I thought of doing an exercise of connecting both client and server. 因此,我想到了进行客户端和服务器连接的练习。 However, I have encountered this error message when I try to run the code: Exception in thread "main" java.net.ConnectException: Connection refused: connect 但是,当我尝试运行代码时遇到了以下错误消息: 线程“ main” java.net.ConnectException中的异常:连接被拒绝:connect

This is my client class code: 这是我的客户类代码:

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);
    }
}

This is my server code: 这是我的服务器代码:

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();
        }
    }
}

The full error message is: 完整的错误消息是:

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)

I hope someone can help me, thanks. 希望有人能帮助我,谢谢。

There are 2 issues in your program: 您的程序中有2个问题:

  1. You use the port 80 which is part of the well-known ports or system ports (0 to 1023), so you need to launch your server with the admin rights or change it for 8080 for example. 您使用的端口80众所周知的端口系统端口 (0到1023)的一部分,因此您需要以管理员权限启动服务器或将其更改为8080
  2. You forgot to call bw.newLine() after each bw.write(sendMessage) such that it waits for ever since on the other side you call br.readLine() which means that it waits for an entire line while you don't send the end of line characters. 您忘了在每个bw.write(sendMessage) bw.newLine()之后调用bw.newLine() ,以至于它一直等待着,因为在另一侧您调用br.readLine() ,这意味着它在等待整个行的同时不发送行尾字符。

Change your code for this: 为此更改代码:

Server part: 服务器部分:

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();
            ...

Output: 输出:

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

Client part: 客户部分:

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();
        ...

Output: 输出:

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

暂无
暂无

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

相关问题 线程“主”java.net.ConnectException 中的异常:连接被拒绝:套接字编程中的线程 Java - Exception in thread "main" java.net.ConnectException: Connection refused:thread in Socket Programming Java 线程“ main”中的异常java.net.ConnectException:连接被拒绝:运行Oracle教程示例时进行连接 - Exception in thread “main” java.net.ConnectException: Connection refused: connect when running Oracle tutorial example Java 中的 ssh 异常:java.net.ConnectException:连接被拒绝:连接 - ssh Exception in Java: java.net.ConnectException: Connection refused: connect 无法创建套接字:java.net.ConnectException:连接被拒绝:连接 - Unable to Create a Socket :java.net.ConnectException: Connection refused: connect 线程“main”中的异常 com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:连接被拒绝:连接 - Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect Tomcat异常 - java.net.ConnectException:连接被拒绝:连接 - Tomcat exception - java.net.ConnectException: Connection refused: connect 如何捕获“ java.net.ConnectException:连接被拒绝:connect”异常 - How to catch a “java.net.ConnectException: Connection refused: connect” exception 线程“ main”中的异常java.net.ConnectException:连接超时:connect - Exception in thread “main” java.net.ConnectException: Connection timed out: connect 连接被拒绝:连接; 嵌套异常是 java.net.ConnectException:连接被拒绝:连接 - Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect Eclipse 4.23.0 连接拒绝主机:localhost; 嵌套异常是:java.net.ConnectException:连接被拒绝:连接 - Eclipse 4.23.0 Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM