您的位置:首页 > 编程语言 > Java开发

我做的小小聊天器,java swing实现

2011-07-19 08:08 666 查看
这个是我看教程做的,不足之处,多多指教
首先启动服务端,然后启动客户端,客户端可启动多个

服务端(server):
代码;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class Server extends JFrame{

    JTextArea ja = new JTextArea();
    JTextField jf = new JTextField("服务器启动");
    ServerSocket ss = null;    
    boolean bool = false;
    ArrayList<Client> clients = new ArrayList<Client>();   //用到泛型的知识   
    
    public static void main(String[] args) {
        new Server().connect();
    }
    
    Server(){                            //服务器端界面
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(d.width/2,0,d.width/4,d.height);
        setLayout(new BorderLayout());
        ja.setBackground(Color.yellow);
        jf.setBackground(Color.orange);
        setVisible(true);
        add(jf,BorderLayout.NORTH);
        add(ja,BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);    
    }
    
    public void connect(){
        try {
            ss = new ServerSocket(8880);        
            bool = true;
            while(bool){
                Socket s = ss.accept();            //阻塞式连接
                Client c = new Client(s);        //连接一个创建一个客户
                new Thread(c).start();            //线程启动
                clients.add(c);
                jf.setText("有" + clients.size() + "个用户上线了");
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    class Client implements Runnable{            
        Socket soc = null;
        boolean bl = false;
        DataInputStream dis = null;
        DataOutputStream dos = null;
        
        Client(Socket s){            
            soc = s;    
            bl = true;
        }
        
        public void send(String str){                
            try {
                dos = new DataOutputStream(soc.getOutputStream());
                dos.writeUTF(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public void run(){
            try{    
                while(bl){
                    dis = new DataInputStream(soc.getInputStream());
                    String str = new String(dis.readUTF());        //接收客户端信息
                    ja.setText(ja.getText() + str + "\n");
                    
                    for(int i = 0;i < clients.size();i ++){
                        Client c = clients.get(i);            //发送信息给所有客户端
                        c.send(str);
                        }                    
                    }                
            }catch (IOException e) {
                System.out.println("connet closed");
            }finally{
                try {
                    dis.close();
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

客户端(chatguest):
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatGuest extends JFrame{

    JTextArea jta = new JTextArea(15,40);    
    JTextField jtf = new JTextField("");
    JComboBox box = new JComboBox();
    String [] string = new String[3];
    Socket s = null;
    boolean bl = false;
    DataOutputStream dos = null;
    DataInputStream dis = null;
        
    public static void main(String[] args) {
        new ChatGuest().lanchFrame();
    }
    
    public void lanchFrame(){                        //创建用户界面
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(d.width/2,d.height/10,400,400);
        setLayout(new BorderLayout());
        setVisible(true);
        textArea();
        add(box,BorderLayout.NORTH);
        add(jta,BorderLayout.CENTER);
        add(jtf,BorderLayout.SOUTH);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);                
        connect();
        new Thread(new ReceThread()).start();
    }
    
    public void textArea(){                        //设置输入显示框
        box.setBackground(Color.orange);
        jta.setBackground(Color.yellow);
        jta.setForeground(Color.red);
        jtf.setBackground(Color.orange);
        jtf.setForeground(Color.blue);
        jtf.addKeyListener(new JtaListener());
        try {
            InetAddress as = InetAddress.getLocalHost();
            string[0] = as.getHostAddress();
            string[1] = as.getHostName();
            string[2] = System.getProperty("os.name");
            box.addItem("您的ip地址是"+ string[0]);
            box.addItem("您的爱机是"+ string[1]);
            box.addItem("您的操作系统是"+string[2]);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        
    }
    
    public void connect(){                
        try {
            s = new Socket("Localhost",8880);    //建立与服务端连接,这里ip是本机,端口:8880,可更改
            bl = true;
            jtf.setText("本机连接成功");    
        } catch (UnknownHostException e) {    
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        
    class JtaListener extends KeyAdapter{    
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEve
4000
nt.VK_ENTER)
            {
                try {
                    dos = new DataOutputStream(s.getOutputStream());
                    dos.writeUTF(jtf.getText());        //发送给服务端信息
                    
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                jtf.setText("");
                if(jta.getLineCount() == 6) {
                    jta.setText("");
                }
            }            
        }
    }
    
    class ReceThread implements Runnable{            //接收服务端信息

        public void run() {
            try {
                dis = new DataInputStream(s.getInputStream());
                while(bl){
                    String str = dis.readUTF();
                    jta.setText(jta.getText() + str +"\n" );
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                try {
                    dis.close();
                    dos.close();
                    s.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }        
                }    
            }
        }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息