在与线程的套接字通信中获取 java.net.SocketException 连接重置错误
Getting java.net.SocketException Connection reset error in socket communication with threads
我基本上是在尝试编写多个客户端和服务器之间的套接字通信。
我收到此错误 java.net.SocketException: Connection reset
。我已经阅读了一些关于此错误的帖子,但是 none 所提议的解决方案似乎解决了我的问题。
这是客户端的代码: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();
}
}
}
错误发生在服务器代码的try catch块中的fromClient = waitForMessage();
行
此代码在行中调用方法 waitForMessage()
:String message = inputStream.readUTF();
你有什么建议吗?谢谢
java.net.SocketException: Connection reset
这意味着 OS 已重置连接,因为另一端的进程不再是 运行。看起来您的客户端在处理来自服务器的第一个回复后崩溃了。
Scanner scanner = new Scanner(System.in);
scanner.close();
不要关闭在 System.in
上运行的 Scanners
。这将关闭 System.in
,您将无法再从那里阅读任何内容。
我基本上是在尝试编写多个客户端和服务器之间的套接字通信。
我收到此错误 java.net.SocketException: Connection reset
。我已经阅读了一些关于此错误的帖子,但是 none 所提议的解决方案似乎解决了我的问题。
这是客户端的代码: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();
}
}
}
错误发生在服务器代码的try catch块中的fromClient = waitForMessage();
行
此代码在行中调用方法 waitForMessage()
:String message = inputStream.readUTF();
你有什么建议吗?谢谢
java.net.SocketException: Connection reset
这意味着 OS 已重置连接,因为另一端的进程不再是 运行。看起来您的客户端在处理来自服务器的第一个回复后崩溃了。
Scanner scanner = new Scanner(System.in);
scanner.close();
不要关闭在 System.in
上运行的 Scanners
。这将关闭 System.in
,您将无法再从那里阅读任何内容。