java socket多线程聊天室
SERVER
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; public class Server extends JFrame implements Runnable,ActionListener{ JPanel jp; JButton jb,jb2; ServerSocket ss; Socket s; ArrayList al=new ArrayList(); HuThread ht; JTextArea jta; JScrollPane jsp; boolean hufan=true; public static void main(String[] args) { new Server(); } public Server(){ jp=new JPanel(); jb=new JButton("启动服务器"); jb2=new JButton("关闭服务器"); jb.addActionListener(this); jb2.addActionListener(this); jta=new JTextArea(); jsp=new JScrollPane(jta); jp.add(jb); jp.add(jb2); this.add(jsp); this.add(jp,"North"); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void run() { while(hufan){ try { s=ss.accept(); al.add(s); ht=new HuThread(s,al); Thread tt=new Thread(ht); tt.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==jb){ try { ss=new ServerSocket(5555); hufan=true; jta.setText("服务器已经启动"); Thread t=new Thread(this); t.start(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }else if(e.getSource()==jb2){ try { hufan=false; ss.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } jta.setText("服务器已经关闭"); } } } class HuThread implements Runnable{ Socket s; ArrayList al; public HuThread(Socket s,ArrayList al){ this.s=s; this.al=al; } public void run() { try{ BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); while(true){ String info=br.readLine(); for(int i=0;i<al.size();i++){ Socket s2=(Socket)al.get(i); PrintWriter pw=new PrintWriter(s2.getOutputStream()); pw.println(info); pw.flush(); } } }catch(Exception e){ e.printStackTrace(); } } }
CLIENT
import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class Client extends JFrame implements ActionListener,Runnable{ JTextArea jta; JTextField jtf; JPanel jp; JButton jb; BufferedReader in; PrintWriter out; JScrollPane jsp; public static void main(String args[]){ Client c = new Client(); // c.receive(); } public Client(){ jp=new JPanel(); jta=new JTextArea(); jta.setEditable(false); jsp=new JScrollPane(jta); jb=new JButton("发送"); jb.addActionListener(this); jtf=new JTextField(20); jp.add(jtf); jp.add(jb); this.add(jsp,"Center"); this.add(jp,"South"); try { Socket s = new Socket("127.0.0.1",5555); System.out.println("客户连接服务器成功"); out = new PrintWriter(s.getOutputStream()); in = new BufferedReader(new InputStreamReader(s.getInputStream()));//包装成字符流 } catch (Exception e) { e.printStackTrace(); } Thread t=new Thread(this); t.start(); this.setSize(400,300); this.setLocation(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } //消息接收 // private void receive(){ // try { // while(true){ // String str = in.readLine();//读BufferedReader // if(str==null){ // return ; // } // jta.append("说"+str+"\r\n");//加载到JTextArea中,显示 // } // } catch (IOException e) { // e.printStackTrace(); // } // } @Override public void actionPerformed(ActionEvent arg0) { String text=jtf.getText(); jtf.setText(""); out.println(text); out.flush(); } @Override public void run() { try { while (true) { String str = in.readLine(); if (str != null) { jta.append("说" + str + "\r\n"); } } } catch (IOException e) { e.printStackTrace(); } } }