在与线程的套接字通信中出现java.net.SocketException连接重置错误。

问题描述 投票:0回答:1

我基本上是想在多个客户机和服务器之间进行socket通信,但却得到了这样的错误信息。java.net.SocketException: Connection reset. 我读了一些关于这个错误的帖子,但没有一个建议的解决方案似乎解决我的问题。

下面是客户端的代码。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;
            }

        }

    }

}

服务器的代码。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();
        }

    }

}

错误发生在以下一行 fromClient = waitForMessage(); 在服务器代码的 try catch 块中,这段代码调用方法 waitForMessage() 你有什么建议吗?String message = inputStream.readUTF();

你有什么建议吗?

java sockets java-threads connection-reset
1个回答
0
投票
java.net.SocketException: Connection reset

这意味着操作系统已经重置了连接,因为另一端的进程不再运行了。看起来你的客户端在处理完服务器的第一个回复后就崩溃了。

Scanner scanner = new Scanner(System.in);
scanner.close();

不要关闭 Scanners 经营 System.in. 这将关闭 System.in 而你将无法再从那里读到任何东西。

© www.soinside.com 2019 - 2024. All rights reserved.