共计 4189 个字符,预计需要花费 11 分钟才能阅读完成。
/**
* 实现一对多聊天室的服务端
*
*/
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();
}
}
}
}
}
正文完