I'm basically trying to code a socket communication between multiple clients and a server.
I am getting this error java.net.SocketException: Connection reset
. I have read some posts regarding this error, but none of the proposed solutions seems to solve my problem.
Here is the code for the Client : Client.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import javafx.scene.web.PromptData;
public class Client {
private Socket socket;
private boolean running;
public Client(Socket socket) {
this.socket = socket;
running = true;
}
public void sendToServer(String message) throws IOException {
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF(message);
System.out.println("Die Nachricht : \"" + message + "\" wurde zu dem Server geschickt.");
// outputStream.close();
}
public String waitForNewMessage() throws IOException {
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String message = inputStream.readUTF();
// inputStream.close();
return message;
}
public void stop() throws IOException {
socket.close();
running = false;
}
public boolean isRunning() {
if (running == true) {
return true;
}
return false;
}
public String promptForNewMessage() {
Scanner scanner = new Scanner(System.in);
System.out.println("Geben Sie eine Nachricht ein.");
String message = scanner.nextLine();
scanner.close();
return message;
}
public void processReceivedMessage(String message) {
System.out.println("Hier ist die Antwort des Servers : " + message);
}
public static void main(String[] args) throws IOException {
//Socket clientSocket = new Socket(args[0], Integer.parseInt(args[1]));
Socket clientSocket = new Socket("localhost",9999);
Client client = new Client(clientSocket);
while (true) {
String message = client.promptForNewMessage();
client.sendToServer(message);
String response = client.waitForNewMessage();
client.processReceivedMessage(response);
if(response.contains("\\exit")) {
System.out.println("Die Verbindung wird geschlossen");
client.stop();
break;
}
}
}
}
The code for the server : Server.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
private int port;
private boolean running;
private ServerSocket serverSocket;
private static Server instance;
private Server() throws IOException {
this.port = loadPortFromConfig("config-datei.txt");
this.running = true;
this.serverSocket = new ServerSocket(this.port);
}
public int loadPortFromConfig(String path) throws FileNotFoundException {
File config = new File(path);
Scanner in = new Scanner(config);
if (in.hasNextLine()) {
String line = in.nextLine().split(":")[1];
in.close();
return Integer.parseInt(line);
}
in.close();
return 9999;
}
public static Server getInstance() throws IOException {
if (instance == null) {
instance = new Server();
}
return instance;
}
public static void main(String[] args) throws IOException {
Server server = Server.getInstance();
if (args.length != 0) {
server.port = Integer.parseInt(args[0]);
}
System.out.println("Der Port des Servers ist : " + server.port);
while(true) {
Socket socket = null;
try {
socket = server.serverSocket.accept();
System.out.println("A new client is connected : " + socket);
System.out.println("Assigning a new thread for this client");
Thread t = new Connection(socket);
t.start();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
static class Connection extends Thread {
private Socket clientSocket;
private boolean running;
public Connection(Socket clientSocket) {
this.clientSocket = clientSocket;
this.running = true;
}
@Override
public void run() {
String fromClient = "";
while(true) {
try {
fromClient = waitForMessage();
System.out.println("Der Client hat die Nachricht : \"" + fromClient + "\" geschickt");
if(fromClient.contains("\\exit")) {
System.out.println("Client "+this.clientSocket + "sneds exit...");
System.out.println("Closing connection.");
sendToClient(fromClient);
System.out.println("Connection closed");
running = false;
break;
}
sendToClient(fromClient);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
public String waitForMessage() throws IOException {
DataInputStream inputStream = new DataInputStream(clientSocket.getInputStream());
String message = inputStream.readUTF();
// inputStream.close();
return message;
}
public void sendToClient(String message) throws IOException {
DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
outputStream.writeUTF("Die folgende Nachricht : \"" + message + "\" wurde geschickt");
// outputStream.close();
}
}
}
The error occurs in the line fromClient = waitForMessage();
in the try catch block of the server code
This code calls the method waitForMessage()
in the line : String message = inputStream.readUTF();
Do you have any recommendations ? thank you
java.net.SocketException: Connection reset
This means the OS has reseted the connection because the process on the other end is no longer running. It looks like your client crashes after processing first reply from the server.
Scanner scanner = new Scanner(System.in);
scanner.close();
Do not close Scanners
operating on System.in
. This will close System.in
and you will not be able to read anything anymore from there.