package hai;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Calendar;
public class Client extends Frame implements ActionListener{
private boolean login = false;
private Color bg = new Color(176, 196, 227);
private String rightNow;
private InetAddress clientAddress;
private Socket connection;
private DataInputStream in;
private DataOutputStream out;
private recThread receiver;
private Button B_login = new Button("Login");
private Button B_logout = new Button("Logout");
private Button B_send = new Button("Send");
private Label userName = new Label("User Name:");
private TextField getUserName = new TextField(); // 获取用户名
private Label svrIP = new Label("Server IP:");
private TextField getSvrIP = new TextField(); // 获取服务器 IP 地址
private Label svrPort = new Label("Server Port:");
private TextField getSvrPort = new TextField(); // 获取服务器端口
private TextArea input = new TextArea(); // 输入文本框
private TextArea output = new TextArea(); // 输出文本框
private Label user = new Label("User (0)");
private List list = new List();
// 用于接收服务器消息的线程类
private class recThread extends Thread
{
public recThread()
{
start();
}
public void run()
{
try{
while(login){
String line = in.readUTF();
rightNow = Calendar.getInstance().getTime().toLocaleString();
if(line.charAt(0) == 'Q'){
if(line.length() == 1){
// 退出
connection.close();
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nLog out.\n\n");
login = false;
list.clear();
getUserName.setEditable(true);
getSvrIP.setEditable(true);
getSvrPort.setEditable(true);
B_login.setEnabled(true);
B_send.setEnabled(false);
B_logout.setEnabled(false);
break;
}
else{
// 其他用户退出
list.remove(line.substring(2));
user.setText("User ("+list.getItemCount()+")");
output.append(rightNow+"\n"+line.substring(2)+" log out.\n\n");
}
}
else if(line.charAt(0) == 'L'){
// 其他用户登录
list.add(line.substring(2));
user.setText("User ("+list.getItemCount()+")");
output.append(rightNow+"\n"+line.substring(2)+" log in.\n\n");
}
else{
output.append(rightNow+"\n"+line+"\n\n");
}
}
}catch(SocketException se){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nConnection closed.\n\n");
login = false;
list.clear();
getUserName.setEditable(true);
getSvrIP.setEditable(true);
getSvrPort.setEditable(true);
B_login.setEnabled(true);
B_send.setEnabled(false);
B_logout.setEnabled(false);
}catch (IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
}
// 响应关闭按钮的内部类
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
// 布局设置方法,在构造方法里面调用
private void setup()
{
Panel top1 = new Panel();
top1.setLayout(new BorderLayout());
top1.add("West", userName);
userName.setBackground(bg);
top1.add("Center", getUserName);
getUserName.setBackground(bg);
Panel top2 = new Panel();
top2.setLayout(new BorderLayout());
top2.add("West", svrIP);
svrIP.setBackground(bg);
top2.add("Center", getSvrIP);
getSvrIP.setBackground(bg);
Panel top3 = new Panel();
top3.setLayout(new BorderLayout());
top3.add("West", svrPort);
svrPort.setBackground(bg);
top3.add("Center", getSvrPort);
getSvrPort.setBackground(bg);
Panel top4 = new Panel();
top4.setLayout(new BorderLayout());
top4.add("North", top1);
top4.add("Center", top2);
top4.add("East", top3);
Panel east = new Panel();
east.setLayout(new BorderLayout());
east.add("North", user);
user.setBackground(bg);
east.add("Center", list);
list.setBackground(bg);
Panel center = new Panel();
center.setLayout(new BorderLayout());
center.add("Center", output);
output.setBackground(bg);
center.add("South", input);
input.setBackground(bg);
center.add("East", east);
east.setBackground(bg);
Panel south = new Panel();
south.setLayout(new FlowLayout());
south.add(B_login);
B_login.setBackground(bg);
south.add(B_send);
B_send.setBackground(bg);
south.add(B_logout);
B_logout.setBackground(bg);
setLayout(new BorderLayout());
add("North", top4);
add("Center", center);
add("South", south);
setBackground(bg);
}
// 构造方法
public Client()throws UnknownHostException, IOException
{
super("Chat Room Client");
login = false;
clientAddress = InetAddress.getLocalHost();
byte[] ip = clientAddress.getAddress();
String strIP = (ip[0]&0xFF)+"."+(ip[1]&0xFF)+
"."+(ip[2]&0xFF)+"."+(ip[3]&0xFF);
getSvrIP.setText(strIP);
getSvrPort.setText("8888");
getUserName.setText("User");
output.setEditable(false); // 输出文本框设为只读属性
B_login.addActionListener(this);
B_send.addActionListener(this);
B_send.setEnabled(false);
B_logout.addActionListener(this);
B_logout.setEnabled(false);
addWindowListener(new WindowCloser());
getUserName.setText("User");
setup();
pack();
setSize(500, 600);
show();
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nClient started.\nMy IP Address is "+strIP+"\n\n");
}
// 登录
private void logIn()
{
if(login) return;
boolean accepted = false, refused = false;
String line= new String("");
try{
connection = new Socket(getSvrIP.getText(),
Integer.parseInt(getSvrPort.getText()));
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());
out.writeUTF("L "+getUserName.getText());
while(!accepted && !refused){
line = in.readUTF();
if(line.charAt(0) == 'R'){
refused = true;
}
if(line.charAt(0) == 'A'){
accepted = true;
}
}
if(refused){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nUser name error.\n\n");
}
if(accepted){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nSuccessfully log in.\n\n");
login = true;
// 获取当前在线用户列表
String[] str = line.split(" ");
for (int i = 1; i < str.length; i ++){
list.add(str [i]);
}
user.setText("User ("+list.getItemCount()+")");
getUserName.setEditable(false);
getSvrIP.setEditable(false);
getSvrPort.setEditable(false);
B_login.setEnabled(false);
B_send.setEnabled(true);
B_logout.setEnabled(true);
receiver = new recThread();
}
}catch(ConnectException ce){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nHost not found\n\n");
}catch(UnknownHostException uhe){
rightNow = Calendar.getInstance().getTime().toLocaleString();
output.append(rightNow+"\nHost not found\n\n");
}catch(IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
// 发送消息
private void sendMsg()
{
if(!login) return;
try{
out.writeUTF("M "+input.getText());
input.setText("");
}catch (IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
// 退出登录
private void Quit()
{
if(!login) return;
try{
out.writeUTF("Q");
}catch (IOException ioe){
output.append("Error: "+ioe+"\n\n");
}
}
// 消息处理方法
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == B_login){
logIn();
}
if(e.getSource() == B_send){
sendMsg();
}
if(e.getSource() == B_logout){
Quit();
}
}