zoukankan      html  css  js  c++  java
  • java网络编程(三):一个类似QQ的聊天程序

    客户端:

    package QQ;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    /**
     * Created by hu on 2015/11/8.
     */
    //QQ类似聊天程序的客户端
    public class Client extends JFrame {
        static  JTextArea area;
        JTextField field;
        JButton button;
        static PrintWriter writer;
        public Client() {
            this.setTitle("客户端");
            this.setSize(400,500);
            area=new JTextArea(25,30);
            area.setEditable(false);
            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 e) {
                    String text=field.getText();
                    writer.println(text);
                    area.append("me:"+text+"
    ");
                    field.setText("");
                }
            });
        }
        public static void main(String[] args)throws Exception{
            Socket socket=new Socket("127.0.0.1",8899);
            Client client=new Client();
            OutputStream out=socket.getOutputStream();
            BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer=new PrintWriter(out,true);
    
            client.addWindowListener(new WindowListener() {
                @Override
                public void windowOpened(WindowEvent e) {
    
                }
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("closing");
                    try {
                        socket.shutdownInput();
                        socket.shutdownOutput();
                        socket.close();
                        writer.close();
                        reader.close();
                    }catch (Exception exception){
                        exception.printStackTrace();
                    }
    
                }
    
                @Override
                public void windowClosed(WindowEvent e) {
                    System.out.println("closed");
    
                }
    
                @Override
                public void windowIconified(WindowEvent e) {
    
                }
    
                @Override
                public void windowDeiconified(WindowEvent e) {
    
                }
    
                @Override
                public void windowActivated(WindowEvent e) {
    
                }
    
                @Override
                public void windowDeactivated(WindowEvent e) {
    
                }
            });
            while (true){
                   try{
                       String line=reader.readLine();
                       area.append("服务器:"+line+"
    ");
                   }catch (Exception exception){
                       System.out.println("客户端程序已经关闭!");
                       break;
                   }
            }
        }
    }
    服务端
    package QQ;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    /**
     * Created by hu on 2015/11/8.
     */
    /*
    * qq类似聊天程序的服务端
    * */
    public class Server extends JFrame{
        static  JTextArea area;
        JTextField field;
        JButton button;
        static PrintStream writer;
        public  Server(){
            //设置窗口的大小和标题
            this.setTitle("服务器");
            this.setSize(400,500);
            //设置文本区域的大小并且设置为不可编辑
            area=new JTextArea(25,30);
            area.setEditable(false);
            //设置文本输入框的大小
            field=new JTextField(20);
            //设置按钮的
            button=new JButton("提交");
            /*
            * JPanel默认布局的是FlowLayout
            * 可以new的时候就定义成其他布局:
            * JPanel p=new JPanel(new GridLayout(2,1));//两行一列
            * JPanel p=new JPanel(new BorderLayout());
            * */
            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("seven_hu:"+text+"
    ");
                    field.setText("");
                }
            });
        }
        public static void main(String[] args)throws Exception{
            Server server=new Server();
            ServerSocket serverSocket=new ServerSocket(8899);
            System.out.println("Starting listening...");
            while (true){
                Socket socket=serverSocket.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);
                String line=null;
                line=reader.readLine();
                if(line!=null){
                    area.append("client:"+line+"
    ");
                }
            }
        }
    }
     
  • 相关阅读:
    Silverlight项目开发准则
    WCF序列化65536大小限制的问题
    自定义的asp.net翻页控件
    解决Process因缓冲区满而导至进程阻塞的办法
    选择适合的Silverlight通信技术
    深入剖析WCF的可靠会话[实例篇](内含美女图片,定力差者慎入)
    25个强大的 jQuery 砌体网页设计作品
    Silverlight 4常用StringFormat格式总结
    C# 发送邮件内容嵌入图片
    如何解决Silverlight跨域访问安全性问题
  • 原文地址:https://www.cnblogs.com/hujingwei/p/4947913.html
Copyright © 2011-2022 走看看