在线聊天室:服务端
封装使用多线程实现多个客户可以正常收发多条信息
/**
* 在线聊天室:服务端
* 封装使用多线程实现多个客户可以正常收发多条信息
*
*/
public class TMultiChat{
public static void main(String[] args) throws Exception {
System.out.println("-----Server-----");
// 1、指定端口使用ServerSocket
ServerSocket socket = new ServerSocket(8888);
while (true) {
// 2、阻塞式等待连接accept
Socket server= socket.accept();
System.out.println("一个客户端建立了连接");
}
}
// 一个客户代表一个Channel
static class Channel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private Socket server;
private boolean isRunning;
public Channel(Socket server){
this.server = server;
try{
dis = new DataInputStream(server.getInputStream());
dos = new DataOutputStream(server.getOutputStream());
isRunning = true;
}catch(IOException e){
System.out.println("---1---");
release();
}
}
// 接收消息
private String receive(){
String msg = "";
try{
msg = dis.readUTF();
}catch(IOException e){
System.out.println("---2---");
release();
}
return msg;
}
// 发送消息
private void send(String msg){
try{
dos.writeUTF(msg);
dos.flush();
}catch(IOException e) {
System.out.println("---3---");
release();
}
}
// 释放资源
private void release(){
this.isRunning = false;
Utils.close(dis,dos,client);
}
@Override
public void run() {
while(isRunning){
String msg = receive();
if(!msg = ""){
send(msg);
}
}
}
}
}
在线聊天室:客户端使用多线程封装了发送端
1、发送消息
2、释放资源
3、从控制台获取消息
4、重写run
/**
* 使用多线程封装了发送端
* 1、发送消息
* 2、释放资源
* 3、从控制台获取消息
* 4、重写run
* @author fujun
*
*/
public class Send implements Runnable{
private BufferedReader console;
private DataOutputStream dos;
private Socket client;
private boolean isRunning;
public Send(Socket client){
this.client = clietn;
console = new BufferedReader(new InputStreamReader(System.in));
this.isRunning = true;
try{
dos = new DataOutputStream(client.getOutputStream());
} catch(IOException e){
System.out.println("===1===");
this.release();
}
}
@Override
public void run() {
while(isRunning){
String msg = getStrFromConsole();
if(!msg.equals("")){
send(msg);
}
}
}
// 发送消息
private void send(String msg){
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
System.out.println("===3===");
release();
}
}
/*
* 从控制台获取消息
*/
private String getStrFromConsole(){
try {
return console.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
// 释放资源
private void release(){
this.isRunning = false;
Utils.close(dos,client);
}
}
在线聊天室:客户端使用多线程封装了接收端
1、接收消息
2、释放资源
3、重写run
/**
* 使用多线程封装了接收端
* 1、接收消息
* 2、释放资源
* 3、重写run
* @author fujun
*
*/
public class Receive implements Runnable {
private DataInputStream dis;
private Socket client;
private boolean isRunning;
public Receive(Socket client) {
this.client = client;
this.isRunning = true;
try {
dis = new DataInputStream(client.getInputStream());
} catch (IOException e) {
System.out.println("===2===");
release();
}
}
// 接收消息
private String receive() {
String msg = "";
try {
msg = dis.readUTF();
} catch (IOException e) {
System.out.println("====4====");
release();
}
return msg;
}
@Override
public void run() {
while(isRunning){
String msg = receive();
if(!msg.equals("")){
System.out.println(msg);
}
}
}
// 释放资源
private void release() {
this.isRunning = false;
Utils.close(dis, client);
}
}
在线聊天室:客户端
封装实现多个客户可以正常收发多条信息
/**
* 在线聊天室:客户端
* 封装实现多个客户可以正常收发多条信息
* @author fujun
*
*/
public class TMultiClient {
public static void main(String[] args) throws Exception {
System.out.println("-----Clinet-----");
// 1、建立连接:使用Socket创建客户端 + 服务端的地址和端口
Socket client = new Socket("localhost",8888);
// 2、客户端发送消息
new Thread(new Send(client)).start();
new Thread(new Receive(client)).start();
}
}