zoukankan      html  css  js  c++  java
  • Java——socketser与cli

    package 网络编程;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    public class 界面ser extends JFrame implements ActionListener//搞一手动作监听
    {
        JPanel mb=null; JTextArea ta=null; JButton an=null; JTextField tf=null;
        JScrollPane gd=null; PrintWriter prin=null;//定义在这的 全局调用
        public static void main(String[] args) 
        {
            界面ser ser=new 界面ser();
        }
        public 界面ser()
        {
        
            ta=new JTextArea();
            gd=new JScrollPane(ta);
            this.add(gd);
            tf=new JTextField(20);
            an=new JButton("发送");
            an.addActionListener(this);    //这句是添加按键自己 this做监听,我是这么理解的,this关键字我一直搞不太明白,有懂得可以分享下哈
            mb=new JPanel(); mb.add(tf); mb.add(an); //add tf  add an 要放到tf an创建之后
            this.add(mb,"South"); 
            
            this.setTitle("服务器");
            this.setSize(600,500);
            this.setLocation(500,600);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            
            try{//socket通信有关部分全都要放到try catch中,因为它可能会出现异常
                ServerSocket ser=new ServerSocket(9999);
                Socket serl=ser.accept();
                InputStreamReader mes=new InputStreamReader(serl.getInputStream());
                BufferedReader mess=new BufferedReader(mes);
                prin=new PrintWriter(serl.getOutputStream(),true);//true是开启自动刷新
                while(true)//接收部分,直接扔到死循环里,接受就显示
            {
                    String xinxi=mess.readLine();
                    ta.append("B:"+xinxi+"
    ");//TextField上添加信息不覆盖
                    
            }
            }catch(Exception e){}
            
        }
        public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource()==an)//如果按键被按下
            {
                String xinxi=tf.getText();//从TextField中读取
                ta.append("A:"+xinxi+"
    ");//添加上信息,不覆盖,这个是在TextArea上显示
                prin.println(xinxi);         //这个是发送到客户端
                tf.setText("");                 //发送之后,清空TextField
            }
        }
    }
    package 网络编程;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    public class 界面cli extends JFrame implements ActionListener
    {
        JPanel mb=null; JTextArea ta=null; JButton an=null; JTextField tf=null;
        JScrollPane gd=null; PrintWriter prin=null;
        public static void main(String[] args) 
        {
            界面cli cli=new 界面cli();
        }
        public 界面cli()
        {
            
            ta=new JTextArea();//文本区域是不用添加到面板上的,直接this.add即可
            gd=new JScrollPane(ta);
            this.add(gd);
            tf=new JTextField(20);
            an=new JButton("发送");
            an.addActionListener(this);//添加监听
            mb=new JPanel(); mb.add(tf); mb.add(an); 
            this.add(mb,"South"); 
            
            this.setTitle("客户端");
            this.setSize(600,500);
            this.setLocation(500,600);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            
            try{
                Socket serl=new Socket("127.0.0.1",9999);//本机IP 端口要与服务端相同
                InputStreamReader mes=new InputStreamReader(serl.getInputStream());
                BufferedReader mess=new BufferedReader(mes);
                prin=new PrintWriter(serl.getOutputStream(),true);
                while(true)
            {
                    String xinxi=mess.readLine();
                    ta.append("A:"+xinxi+"
    ");//ta  TextArea
                    
            }
            }catch(Exception e){}
            
        }
        public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource()==an)
            {
                String xinxi=tf.getText();
                ta.append("B:"+xinxi+"
    ");//添加上信息,不覆盖,这个是在TextArea上显示
                prin.println(xinxi);         //这个是发送到server端
                tf.setText("");                 //发送之后,清空TextField
            }
        }
    }
  • 相关阅读:
    python+Appium自动化:BSTestRunner执行测试用例,生成测试报告
    python3基础:介绍几个函数的用法
    python3基础:格式化输出
    python+Appium自动化:框架设计
    python+Appium自动化:PageObject设计模型
    python+Appium自动化:logging配置代码分离
    python+Appium自动化:日志logging模块
    python+Appium自动化:yaml配置capability
    Monkey基本命令
    Monkey介绍
  • 原文地址:https://www.cnblogs.com/JLU-Dragon/p/11930840.html
Copyright © 2011-2022 走看看