java聊天室【转】

客户端: import javax.swing.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.awt.*; public class ClientPoint extends JFrame implements ActionListener,KeyListener{ static int SERVERPORT=8088; static String SERVERADDRESS="127.0.0.1"; private BufferedReader cin=null; private PrintWriter cout=null; private Socket client=null; public String clientname; private JPanel jPanel1=new JPanel(); private JTextField jTextSendInfo=new JTextField(38); private JTextArea jTextGetInfo=new JTextArea(16,33); public JScrollPane areaScrollPane=new JScrollPane(jTextGetInfo); private Button Send=new Button("发送"); private Button Link=new Button("连接"); public ClientPoint(){ super("聊天室^_^"); enableEvents(AWTEvent.WINDOW_EVENT_MASK); setSize(480,465); jPanel1.add("South",jTextSendInfo); jPanel1.setBackground(new Color(75, 234, 166)); jTextGetInfo.setBackground(new Color(122,150,223)); jTextGetInfo.setFont(new java.awt.Font("Dialog", 0, 15)); jTextGetInfo.setForeground(new Color(176, 42, 0)); this.setResizable(false); Send.setBackground(Color.pink); Send.setForeground(Color.blue); Send.setBounds(new Rectangle(92, 400, 90, 37)); Link.setBackground(Color.pink); Link.setForeground(Color.blue); areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setAutoscrolls(true); jTextSendInfo.setText(""); jPanel1.add("North",areaScrollPane); jTextGetInfo.setEditable(false); jPanel1.add("South",Send); Send.setEnabled(false); jPanel1.add("South",Link); Send.addActionListener(this); jTextSendInfo.addKeyListener(this); Link.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){ if(!jTextSendInfo.getText().equals("")){ clientname=jTextSendInfo.getText(); connect(); Link.setEnabled(false); Send.setEnabled(true); jTextSendInfo.setText(""); } else JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE); }}); this.setContentPane(jPanel1); } public void connect(){ try{ client=new Socket(SERVERADDRESS,SERVERPORT); cout=new PrintWriter(client.getOutputStream()); cin=new BufferedReader( new InputStreamReader(client.getInputStream())); cout.println("欢迎["+clientname+"]进入聊天室"); cout.flush(); Receiver r=new Receiver(); r.start(); }catch(Exception e){ e.printStackTrace(); } } protected void processWindowEvent(WindowEvent e){ if(e.getID()==WindowEvent.WINDOW_CLOSING){ quit(); } super.processWindowEvent(e); } public static void main(String[] args){ ClientPoint cp=new ClientPoint(); cp.show(); } class Receiver extends Thread{ public void run(){ String msg=null; JScrollBar sb; try{ msg=cin.readLine(); while(true){ jTextGetInfo.append(msg+"/n"); sb=areaScrollPane.getVerticalScrollBar(); sb.setValue(sb.getMaximum()+50); jTextSendInfo.setEnabled(true); msg=cin.readLine(); } }catch(Exception e){ Send.setEnabled(false); } } } void quit(){ try{ cout.println("["+clientname+"]离开聊天室"); cout.flush(); cout.println("exit"); cout.flush(); System.out.print("lianjieduankai"); cin.close(); cout.close(); client.close(); }catch(Exception e){} finally{System.exit(0); } } public void actionPerformed(ActionEvent e){ if(!jTextSendInfo.getText().equals("")){ cout.println("["+clientname+"]:"+jTextSendInfo.getText()); cout.flush(); jTextSendInfo.setText(""); jTextSendInfo.setFocusable(true); } else JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE); } public void keyPressed(KeyEvent e){} public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_ENTER) { if(!jTextSendInfo.getText().equals("")) { if(cout==null) { clientname=jTextSendInfo.getText(); connect(); Link.setEnabled(false); Send.setEnabled(true); jTextSendInfo.setText(""); } else { cout.println("["+clientname+"]:"+jTextSendInfo.getText()); cout.flush(); jTextSendInfo.setText(""); jTextSendInfo.setFocusable(true); } } else { if (cout==null) { JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE); } else JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE); } } } } 服务器端: import java.net.*; import java.io.*; public class ServerPoint extends Thread{ static int SERVERPORT=8088; private Socket client; public static int i=0; public static String[] vct=new String[10]; public ServerPoint(){} public void run(){ ClientThread.log("服务器端程序启动..."); try{ ServerSocket server=new ServerSocket(SERVERPORT); while(true){ client=server.accept(); ClientThread ct=new ClientThread(client); ct.start(); } }catch(Exception e){ e.printStackTrace(); ClientThread.log("服务器端程序关闭。。。"); System.exit(0); } } public static void main(String[] args){ ServerPoint sp=new ServerPoint(); sp.start(); } public static synchronized void message(String msg){ vct[i]=msg; i=(i+1)%10; vct[i]="*"; } } class ClientThread extends Thread{ private static int ii=0; private Socket s; String msg=null; ServerSocket serverSocket=null; Socket socket=null; BufferedReader cin=null; PrintWriter cout=null; public ClientThread(Socket s){ this.s=s; } public void run(){ try{ cin=new BufferedReader( new InputStreamReader(s.getInputStream())); cout=new PrintWriter(s.getOutputStream()); SendToAll sta=new SendToAll(); sta.start(); msg=cin.readLine(); ii++; System.out.println("some connect us!聊天室里总共有"+ii+"个人"); while(!msg.equals("exit")){ ServerPoint.message(msg); msg=cin.readLine(); } if(msg.equals("exit")) { --ii; System.out.print("someone exit/n聊天室里总共有"+ii+"个人"); cin.close(); cout.close(); s.close(); } }catch(Exception e){ e.printStackTrace(); } } static void log(String strInfo){ System.out.println(strInfo); } class SendToAll extends Thread{ private int j=-1; public void run(){ while(true) { try{ sleep(500); if(j==-1){ if(!ServerPoint.vct[0].equals("*")){ cout.println(ServerPoint.vct[0]); cout.flush(); j=1; } else { cout.println(ServerPoint.vct[1]); cout.flush(); j=2; } } while(!ServerPoint.vct[j].equals("*")) { cout.println(ServerPoint.vct[j]); cout.flush(); j=(j+1)%10; } }catch(Exception e){} } } } }

你可能感兴趣的:(java聊天室【转】)