62,621
社区成员
发帖
与我相关
我的任务
分享
package client;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class TalkRoomClient extends Thread{
JFrame frame;
JTextArea jta;
JTextField jtf;
JComboBox jcb;
JDialog jd;
String name;
Socket s;
BufferedReader in;
PrintWriter out;
public TalkRoomClient(){super();}
public TalkRoomClient(String n,String ipad){
this.name=n;
String ip="localhost";
if(ipad!=null)ip=ipad;
if(name.length()==0)name="Never";
frame=new JFrame("聊天室->"+name);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
jta=new JTextArea();
jta.setEditable(false);
jta.setLineWrap(true);
jta.setBackground(Color.WHITE);
jta.setFont(Font.getFont("bold"));
jta.setForeground(Color.BLACK);
JScrollPane jsp=new JScrollPane(jta);
final JScrollBar jsb=jsp.getVerticalScrollBar();
frame.add(jsp,"Center");
jtf=new JTextField(20);
jcb=new JComboBox();
jcb.addItem("All");
JPanel jp=new JPanel();
jp.add(jcb);
jp.add(jtf);
frame.add(jp,"South");
frame.setVisible(true);
try{
s=new Socket(ip,9000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
out.println(name);
out.flush();
}catch(IOException e){
e.printStackTrace();
}
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text=jtf.getText();
jtf.setText("");
if(text.length()==0){
new JOptionPane().showMessageDialog(frame, "不允许消息为空!");
}else if(text.contains("hehe")){
new JOptionPane().showMessageDialog(frame, "聊天室中禁用敏感词汇!");
}else{
String to=(String) jcb.getSelectedItem();
if(to.equals("All")){
out.println(name+"$"+to+"$"+text);
out.flush();
}else{
jta.append(name+":>"+text+"\n");
out.println(name+"$"+to+"$"+text);
out.flush();
}
}
jsb.setValue(jta.getHeight());
}
});
}
public static void main(String args[]) throws Exception{
final JFrame jf=new JFrame("选择服务器");
jf.setLocation(400,200);
jf.setSize(250,210);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp=new JPanel();
final JTextField ip=new JTextField(20);
ip.setText("localhost");
JButton jb=new JButton("确定");
JLabel j1=new JLabel("服务器地址");
JLabel j2=new JLabel("聊天昵称");
final JTextField nicheng=new JTextField(20);
jp.add(j1);
jp.add(ip);
jp.add(j2);
jp.add(nicheng);
jp.add(jb);
jf.add(jp,BorderLayout.CENTER);
jf.setVisible(true);
jb.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent arg0){
String ipaddr=ip.getText();
Thread trc=new TalkRoomClient(nicheng.getText(),ipaddr);
trc.start();
jf.dispose();
}
});
}
public void run(){
try{
while(true){
String text=in.readLine();
StringTokenizer st=new StringTokenizer(text,":");
String type=st.nextToken();
String tx=st.nextToken();
if(type.equals("Add")){
System.out.println(tx);
StringTokenizer ipadr=new StringTokenizer(tx,"/");
String user = ipadr.nextToken();
if(!(this.name.equals(user))){
this.jcb.addItem(user);
}
this.jta.append(user+" "+ipadr.nextToken()+" 上线了!\n");
}
if(type.equals("Del")){
jcb.removeItem(tx);
jta.append(tx+" 下线了!\n");
}
if(type.equals("Text")){
StringTokenizer ss=new StringTokenizer(tx,"$");
String from=ss.nextToken();
String to=ss.nextToken();
String txt=ss.nextToken();
jta.append(from+":>"+txt+"\n");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
package server;
import java.io.*;
import java.net.*;
import java.util.*;
public class Talking extends Thread{
Socket s;
Map sockets;
String name;
public Talking(String n, Socket s,Map m) {
super();
this.s=s;
this.sockets=m;
this.name=n;
}
public void run(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
String text=in.readLine();
if(text==null)return;
StringTokenizer st=new StringTokenizer(text,"$");
String from=st.nextToken();
String to=st.nextToken();
if(to.equals("All")){
Collection values=sockets.values();
Iterator it=values.iterator();
while(it.hasNext()){
Socket s1=(Socket) it.next();
PrintWriter out=new PrintWriter(s1.getOutputStream());
out.println("Text:"+text);
out.flush();
}
}else{
Socket s2=(Socket) sockets.get(to);
PrintWriter out=new PrintWriter(s2.getOutputStream());
out.println("Text:"+text);
out.flush();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
this.sockets.remove(name);
Collection value=sockets.values();
Iterator it=value.iterator();
while(it.hasNext()){
Socket s2=(Socket) it.next();
PrintWriter pw=new PrintWriter(s2.getOutputStream());
pw.println("Del:"+name);
pw.flush();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
package server;
import java.io.*;
import java.net.*;
import java.util.*;
public class TalkRoomServer {
public static void main(String args[]) throws Exception{
ServerSocket ss=new ServerSocket(9000);
Map sockets=new HashMap();
while(true){
Socket s=ss.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream());
String name=in.readLine();
String IP=s.getInetAddress().toString();
System.out.println(name+":"+IP);
Collection valuse=sockets.values();
Iterator it=valuse.iterator();
while(it.hasNext()){
Socket s1=(Socket) it.next();
PrintWriter pw=new PrintWriter(s1.getOutputStream());
pw.println("Add:"+name+IP);
pw.flush();
}
sockets.put(name, s);
Set names=sockets.keySet();
it=names.iterator();
while(it.hasNext()){
String loginedUser=(String) it.next();
out.println("Add:"+loginedUser+IP);
out.flush();
}
Thread tt=new Talking(name,s,sockets);
tt.start();
}
}
}