一对多聊天室
Posted liuliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一对多聊天室相关的知识,希望对你有一定的参考价值。
/**
* 实现一对多聊天室的服务端
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 接受客户端发送消息的线程类
*/
class ChatReceive extends Thread{
private Socket socket;
public ChatReceive(Socket socket){
this.socket = socket;
}
@Override
public void run() {
this.receiveMsg();
}
/**
* 实现接收客户端发来的消息
*/
private void receiveMsg(){
BufferedReader br = null;
try{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String msg = br.readLine();
synchronized ("abc"){
//把读取到的数据写入公共数据区
ChatRoomServer.buf = "[" + socket.getInetAddress()+ "] " + msg;
//唤醒发送消息的线程
"abc".notifyAll();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 向客户端发送消息的线程类
*/
class ChatSend extends Thread{
private Socket socket;
public ChatSend(Socket socket){
this.socket = socket;
}
@Override
public void run() {
this.sendMsg();
}
/**
* 将公共数据区的消息发送给客户端
*/
private void sendMsg(){
PrintWriter pw = null;
try{
pw = new PrintWriter(socket.getOutputStream());
while(true){
synchronized ("abc"){
//让发送消息的线程处于等待状态
"abc".wait();
pw.println(ChatRoomServer.buf);
pw.flush();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(pw != null){
pw.close();
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class ChatRoomServer {
//定义公共数据区
public static String buf;
public static void main(String[] args) {
System.out.println("Chat Server Version 1.0");
System.out.println("Listen at 8888....");
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(8888);
while(true){
Socket socket = serverSocket.accept();
System.out.println("连接到: " + socket.getInetAddress());
new ChatReceive(socket).start();
new ChatSend(socket).start();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 实现一对多聊天室的客户端
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* 发送消息线程
*/
class Send extends Thread{
private Socket socket;
private Scanner scanner;
private String name;
public Send(Socket socket,Scanner scanner,String name){
super(name);
this.socket = socket;
this.scanner = scanner;
}
/**
* 发送消息方法
*/
private void sendMsg(){
PrintWriter pw = null;
try{
pw = new PrintWriter(socket.getOutputStream());
while(true){
System.out.print("\\n请 "+this.getName()+" 输入消息:");
String msg = scanner.nextLine();
pw.println(msg);
pw.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(pw != null){
pw.close();
}
if(scanner != null){
scanner.close();
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void run() {
this.sendMsg();
}
}
/**
* 接受消息线程
*/
class Receive extends Thread{
private Socket socket;
private String name;
public Receive(Socket socket,String name){
super(name);
this.socket = socket;
}
/**
* 接受消息方法
*/
private void receiveMsg(){
BufferedReader br = null;
try{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String msg = br.readLine();
System.out.println("\\n" + this.getName() + " 接受信息为:" + msg);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void run() {
this.receiveMsg();
}
}
public class ChatRoomClient {
public static void main(String[] args) {
Scanner scanner = null;
ServerSocket serverSocket = null;
Socket socket = null;
try{
//创建Scanner对象
scanner = new Scanner(System.in);
System.out.println("请输入:Server,<port> 或者:<ip>,<port>");
String str = scanner.nextLine();
String[] arrStr = str.split(",");
if("Server".equals(arrStr[0])){
//启动服务端
System.out.println("TCP Server Listen at " + arrStr[1] + "...");
serverSocket = new ServerSocket(Integer.parseInt(arrStr[1]));
socket = serverSocket.accept();
System.out.println("连接成功!");
}else{
//启动客户端
socket = new Socket(arrStr[0],Integer.parseInt(arrStr[1]));
System.out.println("连接成功");
}
new Send(socket,scanner,arrStr[0]).start();
new Receive(socket,arrStr[0]).start();
}catch (Exception e){
e.printStackTrace();
}finally {
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上是关于一对多聊天室的主要内容,如果未能解决你的问题,请参考以下文章
设计模式 行为型模式 实例 -- 中介者模式实例:一对多聊天