java简易版图形界面客户机聊天室

客户机代码段

package Week9;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class Chat02Client {
	public static void main(String[] args) {
		Start();
	}

	private static void Start() {
		Frame login = new Frame("LOGIN");
		PaintLogin(login);
	}

	private static void PaintLogin(Frame f) {
		f.setVisible(true);// 窗口可见
		f.setBounds(1000, 200, 300, 145);
		// 选择布局方式
		f.setLayout(new FlowLayout());
		f.setResizable(false); // 设置为大小不可变的
		Button port = new Button("网络端口:7539(默认端口             IP地址默认网段)");// 用以计算结果
		Button name = new Button("账户姓名:");// 用以计算结果
		TextField nameIn = new TextField(25);// 用于输入第一个数值
		// 创建按钮
		Button login = new Button("Login");// 用以发送信息给服务器
		f.add(name);
		f.add(nameIn);		
		f.add(port);
		f.add(login);// 以上均为登录界面
		nameIn.requestFocus();// 光标移动到登录名输入框
		f.addWindowListener(new WindowAdapter() {// 侦听窗口关闭就结束程序
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		login.addActionListener(new ActionListener() {
			String name = nameIn.getText();// 获取登录名称

			public void actionPerformed(ActionEvent e) {
				try {
					Frame client = new Frame(name);// 新建一个窗口
					PaintChat(client);// 窗口操作
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

			private void PaintChat(Frame client) throws UnknownHostException, IOException {
				Socket soc = new Socket(name, 7539);// name账号登录服务器
				client.setVisible(true);// 窗口可见
				client.setBounds(1000, 200, 450, 450);
				// 选择布局方式
				client.setLayout(new FlowLayout());
				client.setResizable(false); // 设置为大小不可变的
				// 创建文本域
				TextArea Pr = new TextArea(18, 60);// 显示结果
				// 创建文本框
				TextField tf1 = new TextField(60);// 用于输入第一个数值
				// 创建按钮
				Button bu = new Button("SendMessage");// 用以发送信息给服务器
				client.add(Pr);
				client.add(tf1);
				client.add(bu);// 添加按钮文本框文本域
				tf1.requestFocus();// 光标移动到第一个输入文本框
				new Acceptmsg(soc, Pr).start();//开一个接受数据的线程
				client.addWindowListener(new WindowAdapter() {// 侦听窗口关闭就结束程序
					public void windowClosing(WindowEvent e) {
						System.exit(0);
					}
				});
				bu.addActionListener(new ActionListener() {// 侦听SendMessage按钮,每次点击按钮发送信息
					public void actionPerformed(ActionEvent e) {
						tf1.requestFocus();// 光标移动到第一个输入文本框
						String s = tf1.getText();
						s = nameIn.getText() + "\n " + s + "\n";
//						Pr.append(s);
						OutputStream output;
						try {
							output = soc.getOutputStream();
							output.write(s.getBytes());
						} catch (IOException e2) {
							e2.printStackTrace();
						}
						tf1.setText(null);// 清空输入数据框
						f.addWindowListener(new WindowAdapter() {// 侦听窗口关闭就结束程序
							public void windowClosing(WindowEvent e) {
								System.exit(0);
							}
						});
					}
				});
			}
		});
	}
}

class Acceptmsg extends Thread {
	Socket soc;
	TextArea Pr;

	public Acceptmsg(Socket soc, TextArea Pr) {
		this.soc = soc;
		this.Pr = Pr;
	}

	public void run() {
		while (true)
			try {
				AcceptMSG();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
	}

	private void AcceptMSG() throws IOException {
		InputStream input = soc.getInputStream();
		byte[] a = new byte[1024];
		int lenght = input.read(a);
		String msg = new String(a, 0, lenght);		
		Pr.append(msg);
	}
}

服务器代码段

package Week9;

import java.io.*;
import java.net.*;
import java.util.HashSet;

public class Chat02Server {
	static HashSet<Socket> users = new HashSet<Socket>();

	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(7539);
		int i = 1;
		while (true) {
			Socket accept = server.accept();
			users.add(accept);
			System.out.println("已有" + i++ + "人登录服务器!");
			new Mythread(accept).start();
		}
	}
}

class Mythread extends Thread {
	static Socket accept;
	HashSet<Socket> users = new HashSet<Socket>();

	public Mythread(Socket accept) {
		Mythread.accept = accept;
		this.users = Chat02Server.users;
	}

	public void run() {
		while (true) {
			try {
				Transpond();// 起到中转消息的作用
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void Transpond() throws IOException {
		String msg = Accept();// 收到消息
		for (Socket user : users) {
			
			SetPrint(user);
			OutputStream output = user.getOutputStream();
			output.write(msg.getBytes());
			output.flush();
		}
	}

	private static void SetPrint(Socket soc) {
		System.out.println("客户机信息:" + soc);
	}

	static String Accept() throws IOException {
		InputStream input = accept.getInputStream();
		byte[] a = new byte[1024];
		int lenght = input.read(a);
		String msg = new String(a, 0, lenght);
		return msg;
	}
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值