客户机代码段
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;
}
}