简体   繁体   中英

Client - Server Connection Refused - Java

I am learning networking programming. I have tried to implement a client server connection. However, I am encountering the error: "Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)"

I have tried changing the port from port 4999 to port 8080

Client Code:

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

public class Client {

  public static void main(String[] args) throws IOException {
     Socket socket = new Socket("localhost", 8080);
    
     PrintWriter pr = new PrintWriter(socket.getOutputStream()); 
     pr.println("is it working ");
     pr.flush();
    
     InputStreamReader in = new InputStreamReader(socket.getInputStream());
     BufferedReader bf = new BufferedReader(in);
    
     String str = bf.readLine();
     System.out.println("server : " + str);

   }
 }

Server Code:

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

public class Server {

  public static void main(String[] args) throws IOException{
     ServerSocket ss = new ServerSocket(8080);
     Socket socket = ss.accept();
     System.out.println("client connected");
    
     InputStreamReader in = new InputStreamReader(socket.getInputStream());
     BufferedReader bf = new BufferedReader(in);
    
     String str = bf.readLine();
     System.out.println("client : " + str);
    
     PrintWriter pr = new PrintWriter(socket.getOutputStream());
     pr.println("yes");
     pr.flush();
   }
}

Full Error:

~/test$ javac Client.java
~/test$ javac Server.java
~/test$ java Server
client connected
client : GET / HTTP/1.1
~/test$ java Client
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at java.base/java.net.Socket.connect(Socket.java:558)
    at java.base/java.net.Socket.<init>(Socket.java:454)
    at java.base/java.net.Socket.<init>(Socket.java:231)
    at Client.main(Client.java:7)

I do not know why connection is being refused

Thank you for any help

The connection is refused because the server has terminated.
The code in Server.java reads one line, sends the response ( "yes" ) and then terminates.
Your output shows it printing "GET / HTTP/1.1" suggesting that something else (not your Client class) has hit port 8080, with a HTTP request (which is not unexpected for port 8080).
The server responds and then terminates, which returns you to the shell prompt.
Then when you run java Client there is no server running, and you see the connection error.
If you want the server to read multiple requests instead of terminating after one, you'll need a loop around everything from the bf.readLine to the pr.flush .

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