ChatingRoomProject/Sever_Socket.java

54 lines
1.5 KiB
Java
Raw Permalink Normal View History

2025-09-17 08:38:41 +00:00
package 大作业;
import java.io.*;
import java.net.*;
class ServerWorker extends Thread{
private Socket socket;
public BufferedReader in;
public PrintWriter out;
private ChatingRoom chatingRoom;
public ServerWorker(Socket s,ChatingRoom c){
chatingRoom=c;
try{
socket=s;
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
start();
}catch(IOException exception){
System.out.println("scoket创建异常");
System.out.println(exception);
}
}
public void run(){
try{
while (true) {
String str=in.readLine();
chatingRoom.chat_Frame.append(str+'\n');
ServerManager.getServetManager().publish(this, str);
out.println(str);
if(chatingRoom.isClose==true)
break;
}
System.out.println("关闭...");
}catch(IOException exception){
ServerManager.getServetManager().remove(this);
System.out.println(exception);
System.out.println("流异常");
}finally{
try {
socket.close();
} catch (IOException e) {
System.out.println(e);
System.out.println("socket关闭异常");
}
}
}
}
public class Sever_Socket {
static final int PORT=3030;
}