📄 client.java
字号:
package client;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.List;
import java.util.Date;
import share.Message;
public class Client
{
int serverPort = 8888;
Socket s;
boolean status=false;
InetAddress ia;
Frame f;
TextArea ta;
TextField tf;
Button b;
List lu;
Dialog dialog;
TextField tfID;
TextField tfName;
ObjectInputStream ois;
ObjectOutputStream oos;
TextField toID;
// create GUI
public void createGUI()
{
f = new Frame("ChatRoom Client");
Panel ps = new Panel();
Panel pe = new Panel();
ta = new TextArea();
toID = new TextField(" ");
tf = new TextField();
b = new Button(" send ");
lu = new List();
// lu.addItemListener()
/*
* MontiorSend ms=new MontiorSend(ta,tf,dos); b.addActionListener(ms);
* tf.addActionListener(ms);
*/
ps.setLayout(new BorderLayout());
ps.add(toID, "West");
ps.add(tf, "Center");
ps.add(b, "East");
pe.setLayout(new BorderLayout());
pe.add(lu, "Center");
pe.add(new Label("Online(GroupID:1000)"),"North");
Button refresh = new Button("refresh");
refresh.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
refresh();
}
});
pe.add(refresh, "South");
f.add(ps, "South");
f.add(ta, "Center");
f.add(pe, "East");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setLocation(200, 200);
f.setSize(400, 300);
// f.setVisible(true);
}
public void login()
{
dialog = new Dialog(f, "login", true);
dialog.setLayout(new GridLayout(3, 2));
tfID = new TextField(" ");
tfName = new TextField(" ");
Button ok = new Button("Login");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
int id=Integer.parseInt(tfID.getText().trim());
if(id<=1000)
{
dialog.setTitle("ID must have 6 digits!");
return;
}
}
catch (NumberFormatException e1)
{
// TODO Auto-generated catch block
dialog.setTitle("ID must be a number!");
return;
}
if(status)
{
enter();
}
else
{
if(createConnection())
{
enter();
}
}
}
});
Button cancel = new Button("register");
cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
int id=Integer.parseInt(tfID.getText().trim());
if(id<=1000)
{
dialog.setTitle("ID must have 6 digits!");
return;
}
}
catch (NumberFormatException e1)
{
// TODO Auto-generated catch block
dialog.setTitle("ID must be a number!");
return;
}
if(status)
{
register();
}
else
{
if(createConnection())
{
register();
}
}
}
});
dialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
dialog.add(new Label("ID:"));
dialog.add(tfID);
dialog.add(new Label("Name:"));
dialog.add(tfName);
dialog.add(ok);
dialog.add(cancel);
dialog.setLocation(200, 200);
dialog.setSize(200, 100);
dialog.setVisible(true);
}
// create connection
public boolean createConnection()
{
try
{
System.out.println("at Client 165 line.");
s = new Socket("127.0.0.1", serverPort);
System.out.println("at Client 167 line.");
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
System.out.println("at Client 170 line.");
// System.out.println("Connectioning Successful");
//status=true;
return true;
}
catch (IOException e)
{
// e.printStackTrace();
System.out.println("Connection server fails!");
status=false;
return false;
}
}
/**
* @throws IOException
* @throws ClassNotFoundException
*/
private void enter()
{
try
{
oos.writeObject("login");
oos.writeObject(tfID.getText().trim());
oos.writeObject(tfName.getText().trim());
System.out.println("at Client 207 line.");
Boolean b = (Boolean) ois.readObject();
boolean success = b.booleanValue();
System.out.println("at Client 210 line.");
if (success)
{
dialog.setVisible(false);
f.setVisible(true);
}
else
{
System.out.println("at Client 218 line.");
//System.exit(1);
dialog.setTitle("login fails!");
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void register()
{
try
{
System.out.println("at Client 243 line");
oos.writeObject("register");
oos.writeObject(tfID.getText().trim());
System.out.println("at Client 253 line");
oos.writeObject(tfName.getText().trim());
System.out.println("at Client 255 line.");
Boolean b = (Boolean) ois.readObject();
boolean success = b.booleanValue();
System.out.println("at Client 259 line.");
if (success)
{
dialog.setTitle("register success,please login!");
}
else
{
System.out.println("at Client 266 line.");
//System.exit(1);
dialog.setTitle("register fails!");
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// read network stream
public void readStream()
{
try
{
while (true)
{
if (ois != null)
{
Object o = ois.readObject();
if (o instanceof Message)
{
Message p = (Message) o;
String fromID = p.getFromID();
// String toID = p.getToID();
Date date = p.getDate();
String content = p.getContent();
ta.append(fromID + " said to you :(" + date + ")\n");
ta.append(" " + content + "\n");
}
else if (o instanceof String[])
{
String[] userList = (String[]) o;
lu.removeAll();
for (int i = 0; i < userList.length; i++)
{
lu.add(userList[i]);
}
}
}
}
}
catch (SocketException e)
{
this.close();
this.ta.setText("Connectting Server fails!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void bindListener()
{
MontiorSend ms = new MontiorSend(ta, tf, tfID.getText().trim(),toID,oos);
b.addActionListener(ms);
tf.addActionListener(ms);
}
private void close()
{
try
{
if (ois != null)
{
ois.close();
ois = null;
}
if (oos != null)
{
oos.close();
oos = null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void refresh()
{
try
{
Message message = new Message(tfID.getText(), "server", new Date(),
"refresh");
oos.writeObject(message);
oos.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
this.close();
}
}
public static void main(String[] args)
{
Client ts = new Client();
ts.createGUI();
ts.login();
ts.bindListener();
ts.readStream();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -