Java网络编程例子3_聊天室

今天在学习了网络编程后,老师留下了一个关于聊天室的小项目,框架基本都是老师写的,参考老师和朋友的版本,完善了下,是个不算复杂的项目;
项目的基本原理很简单:使用TCP把本地输入的内容发送到服务器端,然后把输入的内容向所有与服务器连接的客户端广播,输入输出使用的缓冲流;
下面是运行步骤:
首先,使用命令行运行ChatServer,作为服务器端;
Java网络编程例子3_聊天室_第1张图片

然后,运行3次ChatClient创建3个客户端;
Java网络编程例子3_聊天室_第2张图片
Java网络编程例子3_聊天室_第3张图片
Java网络编程例子3_聊天室_第4张图片

注意:广播会发给自己以外的与服务器端相连其他客户端;

以下是代码:
package cn.njit.chattingRoom;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient {

public static void main(String[] args) throws IOException, IOException, InterruptedException {
	System.out.println("请输入名称:");
	Scanner sc = new Scanner(System.in);
	String input = sc.next();

	String serverHost = "localhost";
	int port = 9999;
	Socket socket = new Socket(serverHost, port);
	OutputStream os = socket.getOutputStream();

	// 发送姓名
	String myName = "name:" + input + "\n";
	os.write(myName.getBytes());

	// 发消息-多线程
	ChatClientSender sender = new ChatClientSender(socket, sc);
	sender.start();

	// 接收消息
	ChatClinetReceiver receiver = new ChatClinetReceiver(socket);
	receiver.start();
	
	sender.join();
	receiver.join();

}

}
————————————————————————————————————————
package cn.njit.chattingRoom;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

/***

  • 客户端发送线程
  • @author Administrator

*/
public class ChatClientSender extends Thread {
Socket socket;
Scanner sc;

public ChatClientSender(Socket socket, Scanner sc) {
	this.socket = socket;
	this.sc = sc;
}

public void run() {
	send();
}

private void send() {
	try {
		OutputStream os = socket.getOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(os);
		String input;
		while (true) {
			input = sc.next();
			osw.write(input + "\n");
			osw.flush();
		}

	} catch (IOException e) {
		System.out.println(e.getMessage());
	}

}

}

————————————————————————————————————————————
package cn.njit.chattingRoom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class ChatClinetReceiver extends Thread {
Socket socket;
public ChatClinetReceiver(Socket socket) {
this.socket=socket;
}

public void run() {
	//send();
	receive();
	
}

private void receive() {
	try {
		InputStream is=socket.getInputStream();
		InputStreamReader isr=new InputStreamReader(is);
		BufferedReader br=new BufferedReader(isr);
		String line;
		line=br.readLine();
		
		while(null!=line) {
			System.out.println("聊天室:"+line);
			line=br.readLine();
		}
		br.close();
		
	} catch (IOException e) {
		System.out.println(e.getMessage());
	}
	
}

}

——————————————————————————————————————————
package cn.njit.chattingRoom;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ChatServer {

public static void main(String[] args) {
	int port = 9999;
	try {
		ServerSocket ss = new ServerSocket(port);
		List users=new ArrayList();
		while (true) {
			Socket cs = ss.accept();
			System.out.println("客户端已连接");
			ChatServerThread cst = new ChatServerThread(cs,users);
			cst.start();

			// ss.close();
		}
	} catch (IOException e) {
		System.out.println(e.getMessage());
	}

}

}

——————————————————————————————————————----————
package cn.njit.chattingRoom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ChatServerThread extends Thread {
Socket socket;
List users;

public ChatServerThread() {

}

public ChatServerThread(Socket socket, List users) {
	this.socket = socket;
	this.users = users;
}

public void run() {
	Chat();
}

private void Chat() {

	try {
		InputStream is = socket.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);// 转换流
		BufferedReader br = new BufferedReader(isr);// 缓冲流

		String line;
		line = br.readLine();
		// 读取消息
		while (null != line) {
			System.out.println(line);
			// 处理消息
			if (line.startsWith(Constants.tokenUserName)) {
				// 用户名消息
				String username = line.substring(Constants.tokenUserName.length());
				System.out.println(username + "已加入群聊;");
				users.add(socket);

			} else {
				// 普通消息,群发给其他人
				for (Socket s : users) {
					if (socket != s) {

						OutputStream os = s.getOutputStream();
						OutputStreamWriter osw = new OutputStreamWriter(os);
						osw.write(s.getPort() + "说:" + line + "\n");
						osw.flush();
						// osw.close();
					}
				}
			}
			line = br.readLine();

		}
		br.close();
		socket.close();

	} catch (IOException e) {
		e.printStackTrace();
	}

}

}
————————————————————————————————————————————————
package cn.njit.chattingRoom;

public interface Constants {
String tokenUserName=“name:”;
}

——————————————————————————————————-———————

end;
2018.09.30;
//https://download.csdn.net/download/airtrioa/10698622
//这是代码压缩包,代码就在上面;

你可能感兴趣的:(Java)