菜鸟修炼,多客户端Thread

时间:2022-05-22 16:37:56

路过不要错过哦,跪求犇人意见到来!

ChatServer类,模拟多客户端的服务端,代码:

package com.bzu.test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {
public static void main(String[] args)throws IOException {
ServerSocket serverSocket=new ServerSocket(9999);
int number=1;//保存客户端个数
while(true){
Socket socket=serverSocket.accept();
System.out.println("客户端"+number+"链接成功!");
//服务端开启一个独立的线程,来对客户端读写操作
new Thread(new ServerStream(socket, number)).start();
number++;
}
}

}

ServerStream类,模拟服务端线程控制,代码:


package com.bzu.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.xml.ws.handler.MessageContext.Scope;

public class ServerStream implements Runnable {
private Socket socket=null;
private int number;

public ServerStream(Socket socket, int number) {
super();
this.socket = socket;
this.number = number;
}

@Override
public void run() {
try {
BufferedReader br=new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
while(true){
String line=br.readLine();
System.out.println("客户端"+number+":"+line);
pw.println(line.toUpperCase());
if(line.equals("bye")){
System.out.println("客户端"+number+"已经断开链接");
break;
}
}
br.close();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

ChatClient类,模拟客户端控制,代码:


package com.bzu.test;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1", 9999);
new Thread(new ClientInputStream(socket)).start();
new Thread(new ClientOutputStream(socket)).start();
}

}

ClientInputStream类,线程类:

package com.bzu.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

public class ClientInputStream implements Runnable {
private Socket socket=null;
public ClientInputStream(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
try {
String line=br.readLine();
System.out.println(line);
} catch (SocketException e) {
break;
}
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

ClientOutputStream类,模拟输出,代码:

</pre><pre name="code" class="java">package com.bzu.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientOutputStream implements Runnable {
private Socket socket=null;

public ClientOutputStream(Socket socket) {
super();
this.socket = socket;
}

@Override
public void run() {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
while(true){
String line=br.readLine();
pw.println(line);
if(line.equals("bye")){
break;
}
}
br.close();
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}