gpt4 book ai didi

java - 处理java.net.SocketException : Socket closed when multithreading?

转载 作者:行者123 更新时间:2023-11-30 02:20:37 25 4
gpt4 key购买 nike

我正在做一个学校项目,我们应该创建一个简单的酒店预订系统,然后将其与服务器/客户端通信一起使用。由于我想稍微插入该项目并执行一个多线程程序,因此我遇到了一个套接字异常,我不知道如何处理。我到处搜索答案,我知道发生异常是因为我试图使用已关闭的套接字。但从我在 Oracle 文档中读到的内容来看,他们的示例也在这样做。

那么,这实际上是好的,只是我需要处理异常?因为我的代码运行良好,所以我只看到异常,因为我放置了 e.printStackTrace();在我的捕获中。

我的客户端类:

package client;

import java.io.*;
import java.net.Socket;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Client {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

try {

Socket client = new Socket("localhost", 6066);

//System.out.println("Just connected to " + client.getRemoteSocketAddress());

OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);

LocalDate localDate = LocalDate.now();
String date = DateTimeFormatter.ofPattern("yyy/MM/dd").format(localDate);

System.out.println(in.readUTF());
System.out.print("Namn: ");
String name = sc.nextLine();
System.out.print("Ålder: ");
String age = sc.nextLine();

out.writeUTF(name);
out.writeUTF(age);
out.writeUTF(date);

System.out.println(in.readUTF());


client.close();
}catch(IOException e) {
e.printStackTrace();
}
}

}

还有我的服务器类:

package server;

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


public class Server {

public static void main(String [] args) throws IOException {

int port = 6066;
ServerSocket server = new ServerSocket(port);
while(true) {
System.out.println("Listening for client..");

try {

Socket connectedClient = server.accept();
ClientHandle ch = new ClientHandle(connectedClient);
Thread t = new Thread((Runnable) ch);
t.start();

}catch (IOException e) {

}
}
}
}

然后是我的 ClientHandle 类,它具有服务器端的 run() :

package server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
import resources.*;

public class ClientHandle implements Runnable{

Socket connectedClient;
DataInputStream in;
DataOutputStream out;

public ClientHandle(Socket connectedClient) {
this.connectedClient = connectedClient;
try{
this.in = new DataInputStream(this.connectedClient.getInputStream());
this.out = new DataOutputStream(this.connectedClient.getOutputStream());
}catch(IOException ex) {

}
}

Hotel hotel = new Hotel();
Ticket yourTicket = new Ticket();
Server server = new Server();

@Override
public void run() {
while (true) {
try {

InetAddress host = InetAddress.getLocalHost();
System.out.println("Client " + host + " has connected.");

out.writeUTF("Välkommen till Hotel Gisslevik!\nVänligen fyll i nedan information för att slutföra din bokning.\n");

String yourName = in.readUTF();
String age = in.readUTF();
int yourAge = Integer.parseInt(age);

String date = in.readUTF();
yourTicket.setDate(date);

Person guest = new Person(yourName, yourAge);
hotel.setRooms();
Integer room = hotel.getRoom();
String rent = "J";

if (rent.indexOf("J") >= 0) {
yourTicket.setId(yourName);
if (hotel.checkIn(guest, room, yourTicket.getId(), yourTicket.getDate())) {
String yourId = yourTicket.getId();
out.writeUTF("\nDitt rum är nu bokat den " + date + ". \nBokningsnummer: " + yourId);
}
}

out.flush();
connectedClient.close();


}catch (EOFException e) {


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

如果我只是评论 e.printStackTrace();异常没有显示,但我想知道如何处理它们(如果应该处理它们)。我已经在互联网上搜索了好几天并查看了教程,但我没有找到正确的答案。

我非常感谢您提供的任何帮助。

最佳答案

Handle java.net.SocketException: Socket closed

不要关闭套接字然后继续使用它。

when multithreading?

无关紧要。

您的 while (true) 循环中有 connectedClient.close() 。解决办法:移到外面。

关于java - 处理java.net.SocketException : Socket closed when multithreading?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46950563/

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