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");
            }
        }

    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值