学习来自
https://www.bilibili.com/video/BV1Eb411v7iT?from=search&seid=1730986289471644052
知识点非常清楚,感谢这个老师
1、最简单的一对一学起
1.1、客户端
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());
}
}
}
1.2、关闭类
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();
}
}
}
}
}
1.3、服务类
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();
}
}
2、多对多改进
2.1、客户端类
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(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());
}
}
}
2.2、启动客户端类(可以重复多个这个类,实现多个客户端)
import java.io.IOException;
/**
* @author Lenovo
* @date 2020/12/2
*/
public class StartClient {
public static void main(String[]args) throws IOException {
new Client().RunClient();
}
}
2.3、实现关闭流类
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();
}
}
}
}
}
2.4、服务端输入输出类
/**
* @author Lenovo
* @date 2020/12/2
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
/**
* 每个客户端都是一条管道
* 1、输入流
* 2、输出流
* 3、接收数据
* 4、发送数据
*/
public class MyChannel implements Runnable{
private DataOutputStream dos;
private DataInputStream dis;
private boolean flag = true;
public MyChannel(Socket client) {
try {
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dis,dos);
}
}
//接收数据
private String receive(){
String str= "";
try{
str = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dis);
Server.list.remove(this);
}
return str;
}
// 发送
private void send(String str){
if(str!=null && str.length()!=0){
try {
dos.writeUTF(str);
dos.flush();
}
catch (IOException e) {
e.printStackTrace();
flag = false;
CloseUtil.closeAll(dis);
Server.list.remove(this);
}
}
}
// 转发
private void sendOther(){
String str = this.receive();
List<MyChannel> list = Server.list;
for(MyChannel other:list){
//判断是不是自己
if(other==this){
continue; //不发给自己
}
other.send(str);
}
}
@Override
public void run() {
while(flag){
// 调用发送数据的方法
// send(receive());
sendOther();
}
}
}
2.5、服务类
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lenovo
* @date 2020/12/2
*/
public class Server {
// 创建一个 ServerSocket类对象
ServerSocket server;
// 创建就一个集合对象,存储每个连接进来的客户端
public static List<MyChannel>list = new ArrayList<MyChannel>();
public void RunServer() throws IOException {
server = new ServerSocket(9999);
while(true) {
Socket socket = server.accept();
//创建线程类的对象,每一个用户存入集合
MyChannel channel = new MyChannel(socket);
// 添加到集合中去
list.add(channel);
//启动线程
new Thread(channel).start();
}
// 关闭所有流
// CloseUtil.closeAll(dos,dis,socket);
}
public static void main(String[]args) throws IOException {
new Server().RunServer();
}
}