编写一个控制台版本的局域网聊天室实现多人聊天

11 篇文章 0 订阅
9 篇文章 0 订阅
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务器端
 * @author Administrator
 *
 */
public class Server {

	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(9999);
		System.out.println("----服务器已启动-----");
		List<Socket> list = new ArrayList<Socket>();//声明一个存放socket对象的集合
		while(true){
			Socket socket = server.accept();//侦听socket连接
			list.add(socket);//将侦听到的socket连接添加到集合中
			ThreadSocket ts = new ThreadSocket(socket,list);//启动服务器处理每个socket的线程
			ts.start();
		}

	}

}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

/**
 * 服务器处理socket线程类
 * @author Administrator
 *
 */
public class ThreadSocket extends Thread {
	
	private Socket socket;
	private List<Socket> list;

	public ThreadSocket(Socket socket, List<Socket> list) {
		this.socket = socket;
		this.list = list;
	}

	@Override
	public void run() {
		InputStream is = null;
		OutputStream os = null;
		
		try {
			while(true){
				is = socket.getInputStream();
				byte[] b = new byte[1024];
				int len = is.read(b);
				String mess = new String(b,0,len);
				//获取客户端IP地址和端口号等信息,并在服务器端打印
				System.out.println(socket.getInetAddress().getHostAddress()+":"+socket.getPort()+"\n"+mess);
				
				//遍历存放socket的集合,向所有客户端输出信息
				for (Socket so : list) {
					os = so.getOutputStream();
					os.write(mess.getBytes());
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}




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

/**
 * 客户端
 * @author Administrator
 *
 */
public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//开启客户端之前输入名字
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入网名:");
		String name = sc.next();
		
		Socket socket = new Socket("localhost",9999);
		System.out.println("-----"+name+"已上线-----");
		
		//将socket的输入输出流传到对应的线程类
		ThreadReader tr = new ThreadReader(socket.getInputStream());
		ThreadWriter tw = new ThreadWriter(socket.getOutputStream());
		//把上面的网名设置为当前线程的名字
		tr.setName(name);
		tw.setName(name);
		tr.start();
		tw.start();

	}

}


import java.io.IOException;
import java.io.InputStream;

/**
 * 处理客户端输入流的线程类
 * @author Administrator
 *
 */
public class ThreadReader extends Thread {
	
	private InputStream is;

	public ThreadReader(InputStream is) {
		this.is= is;
	}

	@Override
	public void run() {
		try {
			while(true){
				byte[] b = new byte[1024];
				int len = is.read(b);
				String mess = new String(b,0,len);
				System.out.println(mess);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}


import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 处理客户端输出流的线程类
 * @author Administrator
 *
 */
public class ThreadWriter extends Thread {

	private OutputStream os;
	
	public ThreadWriter(OutputStream os) {
		this.os = os;
	}

	@Override
	public void run() {
		Scanner sc = new Scanner(System.in);
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		try {
			
			while(true){
				System.out.println("请输入信息:");
				String mess = sc.next();
				//把当期线程的名称,日期和内容发送到服务器
				String info = sdf.format(new Date())+"  "+Thread.currentThread().getName()+"说:"+mess;
				os.write(info.getBytes());
			}
			
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值