Java Socket网络编程--模仿写一个本机QQ聊天程序

public class Client3 extends JFrame {
    static JTextArea area;
    JTextField field;
    JButton button;
    static PrintWriter writer;
    public Client3() {
        this.setTitle("客户端");
        this.setSize(400, 500);
        area= new JTextArea(25,30);
        field = new JTextField(20);
        button=new JButton("提交");

        JScrollPane sp =new JScrollPane(area);
        JPanel panel=new JPanel();
        this.add(sp,BorderLayout.CENTER);
        panel.add(field);
        panel.add(button);
        this.add(panel, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String text = field.getText();
                writer.println(text);
                area.append("我:"+text+"\n");
                field.setText("");
            }
        });
    }

    public static void main(String[] args) throws Exception{
        Client3 c =new Client3();
        Socket socket =new Socket("127.0.0.1",9995);
        OutputStream out = socket.getOutputStream();
        BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer=new PrintWriter(out,true);
        System.out.println("成功连接服务器!");
        while(true){
            String line = reader.readLine();
            area.append("服务器:"+line+"\n");
        }
    }
}
public class Server3 extends JFrame {
    static JTextArea area;  
    JTextField field;  
    JButton button;  
    static PrintStream writer;  
    public Server3(){  
        this.setTitle("服务器");  
        this.setSize(400,500);  
        area = new JTextArea(25,30);  
        area.setEditable(false);  
        field = new JTextField(20);  
        button = new JButton("提交");  
        JPanel panel = new JPanel();  
        JScrollPane sp = new JScrollPane(area);  
        this.add(sp,BorderLayout.CENTER);  
        panel.add(field);  
        panel.add(button);  
        this.add(panel,BorderLayout.SOUTH);  
        this.setVisible(true);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  


        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String text =field.getText();
                writer.println(text);
                area.append("我:"+text+"\n");
                field.setText("");
            }
        });
    }
/*=================================================================================*/
    public static void main(String[] args) throws Exception{
        Server3 s = new Server3();
        ServerSocket server =new ServerSocket(9995);
        System.out.println("服务端准备完毕!开始监听请求!");
        Socket socket = server.accept();

        InetAddress address =socket.getInetAddress();
        String name = address.getLocalHost().getHostName();
        System.out.println(name+"已成功连接");

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer=new PrintStream(socket.getOutputStream(),true);
        while(true){
            String line=null;
            line=reader.readLine();
            if (line!=null) {
                area.append("客户端:"+line+"\n");
            }
        }

    }
}

你可能感兴趣的:(JAVA,Jave,SE基础,Java网络编程基础,QQ聊天,Socket)