51,410
社区成员
发帖
与我相关
我的任务
分享
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class QQClient {
String name;
BufferedReader in;
PrintWriter out;
JTextField jtf;
JTextArea jta;
JComboBox jcb;
public static void main(String[] args) {
if (args.length==0 || args[0].equals("All")
||args[0].indexOf(":")!=-1){
System.out.println("Error!");
System.exit(0);
}
Socket s=null;
try {
s = new Socket("127.0.0.1",7755);
QQClient client=new QQClient(s,args[0]);
client.prepare();
client.receive();
} catch (Exception e) {
e.printStackTrace();
}
}
public QQClient(Socket s,String name) throws Exception{
this.name=name;
this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
this.out=new PrintWriter(s.getOutputStream());
JFrame frame=new JFrame("Client "+name);
frame.setSize(400,300);
JPanel panel=new JPanel();
this.jcb=new JComboBox();
jcb.addItem("All");
this.jtf=new JTextField(25);
panel.add(jcb);
panel.add(jtf);
this.jta=new JTextArea();
this.jta.setEditable(false);
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(panel,"South");
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
end();
}
});
this.jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
}
void prepare() throws Exception{
out.println("1:"+name+":");
out.flush();
}
void end(){
out.println("2:"+name+":");
out.flush();
System.exit(0);
}
void send(){
String receiver=(String)(this.jcb.getSelectedItem());
if (receiver==null) return;
String text=this.jtf.getText();
if ((text.indexOf(":"))!=-1){
this.jtf.setText("");
return;
}
out.println("3:"+receiver+":"+text);
out.flush();
this.jtf.setText("");
}
void receive(){
while(true){
try {
String str=in.readLine();
String[] info=parseString(str);
if (info[0].equals("1")){
this.jta.append(info[1]+" entered!"+"\n");
this.jcb.addItem(info[1]);
}
else if (info[0].equals("2")){
this.jta.append(info[1]+" exit!"+"\n");
this.jcb.removeItem(info[1]);
}
else{
this.jta.append(info[1]+" said:"+info[2]+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String[] parseString(String s){
String[] ss=new String[3];
int i=0;
StringTokenizer st=new StringTokenizer(s,":");
while(st.hasMoreTokens()){
ss[i]=st.nextToken();
i++;
}
return ss;
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class QQServer {
public static void main(String[] args) {
ServerSocket ss=null;
Map users=new HashMap();
try {
ss = new ServerSocket(7755);
} catch (IOException e) {
e.printStackTrace();
}
while(true){
try {
Socket s=ss.accept();
Thread t=new ServerThread(s,users);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class ServerThread extends Thread{
Socket s;
String userName;
Map users;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s,Map m){
this.s=s;
this.users=m;
try {
this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
this.out=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
String str=in.readLine();
if (str==null) break;
String[] info=parseString(str);
if (info[0].equals("1")){
this.userName=info[1];
send(str,"All");
this.users.put(userName,out);
Iterator it=users.keySet().iterator();
while(it.hasNext()){
String user=(String)(it.next());
send("1:"+user+":",userName);
}
}
else if (info[0].equals("2")){
send(str,"All");
this.users.remove(info[1]);
break;
}
else{
String text="3:"+this.userName+":"+info[2];
send(text,info[1]);
}
} catch (Exception e) {
// e.printStackTrace();
return;
}
}
}
private String[] parseString(String s){
String[] ss=new String[3];
int i=0;
StringTokenizer st=new StringTokenizer(s,":");
while(st.hasMoreTokens()){
ss[i]=st.nextToken();
i++;
}
return ss;
}
private void send(String text,String receiver) throws Exception{
PrintWriter o;
if (receiver.equals("All")){
Iterator it=users.keySet().iterator();
while(it.hasNext()){
String s=(String)(it.next());
o=(PrintWriter)(users.get(s));
o.println(text);
o.flush();
}
}
else{
o=(PrintWriter)(users.get(receiver));
o.println(text);
o.flush();
}
}
}