자바 socket 다 중 스 레 드 채 팅 방

5309 단어
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
");// 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
"); } } } catch (IOException e) { e.printStackTrace(); } } }

좋은 웹페이지 즐겨찾기