gpt4 book ai didi

java - "java.net.SocketException: Connection reset"在我的 Runnable 类中,为什么?

转载 作者:行者123 更新时间:2023-11-30 04:59:47 24 4
gpt4 key购买 nike

我在套接字编程应用程序中有“服务器”的代码,并且在这行代码的以下代码的第 26 行中有此异常 java.net.SocketException:

while((line=bReader.readLine())!=null){

导致此异常的问题是什么?

服务器:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable{
final public int PORT=1212;
private Socket client;
private ServerSocket server;
private BufferedReader bReader;
private PrintWriter pWriter;
public void run(){
try {
System.out.println("Start server ...");
server=new ServerSocket(PORT);
while(true){
System.out.println("Start listening on PORT "+PORT+" ...");
client=server.accept();
System.out.println("Connection with client["+client.getInetAddress().getHostName()+"]");
bReader=new BufferedReader(new InputStreamReader(client.getInputStream()));
pWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
String line="";
while((line=bReader.readLine())!=null){
RecoginzeMessage(line);
}
}

} catch (IOException e) {
e.printStackTrace();
}finally{
try {
client.close();
// server.close();
bReader.close();
pWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public void RecoginzeMessage(String msg){
String msg_params[]=msg.split(":");
switch(Integer.parseInt(msg_params[0])){
case 0:// Authetication process
System.out.println("Authentication new client");
AuthenticateUser(msg_params[1],msg_params[2]);
break;
}
}
public void AuthenticateUser(String username, String password){
if(username.equals("adham")&&password.equals("adham")){
pWriter.println("1");
pWriter.flush();
}
else{
pWriter.println("0");
pWriter.flush();
}
}
}

以及“客户端”的代码..

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client implements Runnable {
public final String SERVER_IP="127.0.0.1";
public final int PORT=1212;
private Socket client;
private BufferedReader bReader;
private PrintWriter pWriter;
Scanner sc=new Scanner(System.in);
public Client(){
if(client==null)
try {
client=new Socket(SERVER_IP,PORT);
bReader=new BufferedReader(new InputStreamReader(client.getInputStream()));
pWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
public void run(){

System.out.print("Enter username: ");
String username=sc.nextLine();

System.out.print("Enter password: ");
String password=sc.nextLine();

if(login(username,password)){
System.out.print(">logged in successfuly");
}else{
System.out.print(">Inavlied username or password");
}

}
private boolean login(String username, String password){
try{
pWriter.println("0:"+username+":"+password);
pWriter.flush();
while(true){
String line="";
while((line=bReader.readLine())!=null){
if(line.equals("1"))
return true;
else
return false;
}

}
}catch(Exception ex){
return false;
}
}
}

编辑:运行客户端线程的类

public class RunClient {    
public static void main(String args[]){
Client cThread=new Client();
cThread.run();
}
}

编辑:运行服务器线程的类

public class RunServer {
public static void main(String args[]){
Server sThread=new Server();
sThread.run();
}
}

最佳答案

客户端看起来是一个线程执行的,但只是实现了Runnable不正确,您需要创建一个 Thread并通过 Runnable到它。客户端不会无限期地执行,因此客户端隐式地完成关闭(JVM)套接字,并且服务器收到此异常以指示(客户端关闭套接字)。

关于java - "java.net.SocketException: Connection reset"在我的 Runnable 类中,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7223263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com