다 중 스 레 드 를 이용 하여 채 팅 방 의 기능 을 실현 하 다.
//
class ClientChatThread extends Thread{
private Socket client;
public ClientChatThread(Socket client) {
super();
this.client = client;
}
//
private void sendAll(String str){
for(ClientChatThread ct:CustomThreadPoolAutoReplyTcpServer.pool){
try {
DataOutputStream dos = new DataOutputStream(ct.client.getOutputStream());
dos.writeUTF(str);
} catch (Exception e) {
System.out.println(" ["+ct.client.getPort()+"] , ");
}
}
}
//
@Override
public void run() {
try {
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
DataInputStream dis = new DataInputStream(client.getInputStream());
while (true) {
// try ,
try {
//
String str = dis.readUTF();//
System.out.println(str);
//
//
sendAll(str);
} catch (Exception e) {
System.out.println(" [" + client.getPort() + "] ");
//
CustomThreadPoolAutoReplyTcpServer.pool.remove(this);
System.out.println(" :"+CustomThreadPoolAutoReplyTcpServer.pool.size());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//
public class CustomThreadPoolAutoReplyTcpServer {
public static Vector<ClientChatThread> pool=new Vector<>();// ,
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6667);
System.out.println(" 6666 , ...");
while (true) {
//
Socket client = ss.accept();
System.out.println(" [" + client.getPort() + "] ");
ClientChatThread ct=new ClientChatThread(client);
pool.add(ct);//
System.out.println(" :"+pool.size());
ct.start();//
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
class ReceiverServerThread extends Thread{
private Socket client;
public ReceiverServerThread(Socket client) {
super();
this.client = client;
}
@Override
public void run() {
DataInputStream dis =null;
try {
dis = new DataInputStream(client.getInputStream());
} catch (Exception e) {
e.printStackTrace();
return;
}
while(true){
String response;
try {
response = dis.readUTF();
System.out.println(response);//
} catch (IOException e) {
System.out.println(" !");
return;
}
}
}
}
public class MultiplyPeopleTcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println(" :");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
Socket client=new Socket("127.0.0.1",6667);
System.out.println(" 127.0.0.1 ");
//
new ReceiverServerThread(client).start();
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
String str="";
while(!"quit".equals(str=sc.nextLine())){
dos.writeUTF(name+":"+str);//
}
client.close();//
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.