当前位置 博文首页 > 广大菜鸟的博客:多线程聊天室学习改进
学习来自
https://www.bilibili.com/video/BV1Eb411v7iT?from=search&seid=1730986289471644052
知识点非常清楚,感谢这个老师
import java.io.*;
import java.net.Socket;
/**
* @author Lenovo
* @date 2020/12/2
*/
public class Client{
public void RunClient() throws IOException {
//创建一个Socket对象
Socket client = new Socket("localhost",9999);
// 创建发送的线程类对象
Send send = new Send(client);
// 创建接收的线程类对象
Receive receive = new Receive(client);
new Thread(send).start();
new Thread(receive).start();
}
public static void main(String[]args) throws IOException {
new Client().RunClient();
}
}
class Send implements Runnable{// 实现发送功能
// 从键盘获取数据
private BufferedReader br;
// 发送数据的数据流
DataOutputStream dos ;
private boolean flag = true; //是否正常
public Send(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public Send(Socket client){
this(); //调用本类无参方法
try{
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dos,client);
}
}
private String getMessage(){
String str = "";
try{
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(br);
}
return str;
}
private void send(String str){
try{
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dos);
}
}
@Override
public void run() {
while(flag){
//循环调用发送信息方法
this.send(getMessage());
}
}
}
class Receive implements Runnable{
//用于接收数据的数据流
private DataInputStream dis ;
private boolean flag =true;
public Receive(Socket client){
try{
dis = new DataInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dis,client);
}
}
private String getMessage(){
String str ="";
try{
str = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dis);
}
return str;
}
@Override
public void run() {
while(flag){
System.out.println(this.getMessage());
}
}
}
import java.io.Closeable;
import java.io.IOException;
/**
* @author Lenovo
* @date 2020/12/2
*/
public class CloseUtil {
public static void closeAll(Closeable... able){
for(Closeable c:able){
if(c!=null){
try{
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Lenovo
* @date 2020/12/2
*/
public class Server {
// 创建一个 ServerSocket类对象
ServerSocket server;
Socket socket;
// 监听客户端是否有连接
// 获取输入输出流接收数据
DataInputStream dis ;
DataOutputStream dos;
public Server() throws IOException {
server = new ServerSocket(9999);
}
public void RunServer() throws IOException {
socket = server.accept();
dis = new DataInputStream(socket.getInputStream());
dos =new DataOutputStream(socket.getOutputStream());
while(true) {
String str = dis.readUTF();
System.out.println("客户端发送的数据是" + str);
dos.writeUTF("服务器端收到了" + str);
}
// 关闭所有流
// CloseUtil.closeAll(dos,dis,socket);
}
public static void main(String[]args) throws IOException {
new Server().RunServer();
}
}
import java.io.*;
import java.net.Socket;
/**
* @author Lenovo
* @date 2020/12/2
*/
public class Client{
public void RunClient() throws IOException {
//创建一个Socket对象
Socket client = new Socket("localhost",9999);
// 创建发送的线程类对象
Send send = new Send(client);
// 创建接收的线程类对象
Receive receive = new Receive(client);
new Thread(send).start();
new Thread(receive).start();
}
}
class Send implements Runnable{// 实现发送功能
// 从键盘获取数据
private BufferedReader br;
// 发送数据的数据流
DataOutputStream dos ;
private boolean flag = true; //是否正常
public Send(){
br = new BufferedReader(new InputStreamReader