项目
目标
多人对话仿真平台
技术点
- socket
- IO
- 异常
- 多线程
- 集合
- 面向对象
- 字符串
- GUI(client)
- …
项目开发方式
迭代开发-敏捷开发
建立模型
C/S 客户端/服务器模型(console)
服务器
package com.ffyc.tcp.one;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class OneServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(9090);
System.out.println("服务器启动成功....");
while (true) {
Socket socket = server.accept(); //阻塞式接收
//处理--IO
InputStreamReader in =
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8);
char[] buffer = new char[256];
int n = in.read(buffer);
String msg = new String(buffer);
System.out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("服务器出现致命问题,即将...");
}
}
}
客户端
package com.ffyc.tcp.one;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class OneClient {
public static void main(String[] args) {
//ip --localhost 127.0.0.1
try {
Socket socket = new Socket("127.0.0.1", 9090);
OutputStreamWriter out =
new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8);
out.write("[客户端:]你吃了吗?");
out.flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("客户端连接服务器异常...");
}
}
}
运行
问题
[问题] 分配缓存空间并没有占满
while (true) {
Socket socket = server.accept(); //阻塞式接收
//处理--IO
InputStreamReader in =
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8);
char[] buffer = new char[256];
int n = in.read(buffer);
String msg = new String(buffer,0,n);
System.out.println(msg);
}
客户端图形化
目标
改造客户端 GUI
UI
移植客户端代码
问题处理
多客户端发送数据
idea并行开启多客户端
GUI关闭问题
socket创建问题
点击就创建socket对象
服务器效率
服务器问题产生
使用多线程完成和多个客户端交互。
创建线程
new Thread(() -> {
try {
InputStreamReader in =
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8);
char[] buffer = new char[256];
int n = in.read(buffer);
String msg = new String(buffer,0,n);
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
System.out.println("与客户端连接异常...");
}
}).start();
请求-响应
服务器响应问题
客户端
接收服务端数据
//接收服务器的响应
new Thread(() -> {
while (true) {
try {
InputStreamReader in = new InputStreamReader(
socket.getInputStream(), StandardCharsets.UTF_8
);
char[] buffer = new char[256];
int n = in.read(buffer);
String msg = new String(buffer, 0, n);
respTextArea.setText(msg);
} catch (IOException e) {
e.printStackTrace();
System.out.println("接收服务器响应异常....");
}
}
}).start();
服务器转发
服务器注册客户端
向其它客户端转发
while (true) {
Socket socket = server.accept(); //阻塞式接收
list.add(socket);
//创建线程
new Thread(() -> {
while (true) {
try {
//读取客户端请求
InputStreamReader in =
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8);
char[] buffer = new char[256];
int n = in.read(buffer);
String msg = new String(buffer, 0, n);
System.out.println(msg);
//响应客户端请求
for (Socket ss : list) {
OutputStreamWriter out
= new OutputStreamWriter(ss.getOutputStream(), StandardCharsets.UTF_8);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
out.write("[服务器" + sdf.format(date) + "]200:" + msg);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("与客户端连接异常...");
}
}
}).start();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("服务器出现致命问题,即将...");
}
}
System.out.println("与客户端连接异常...");
}
}
}).start();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("服务器出现致命问题,即将...");
}
}