zoukankan      html  css  js  c++  java
  • 继续写java和socket

    上回书说道,java中通过socket链接的两台计算机可以通过两端的两个Socket对象来读取和发送来自流的信息所以对于客户端和服务端只需要封装一个相同的收发信息的窗口就好

    代码如下

    package testpackage;
    
    import java.net.Socket;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    
    public class Server extends Thread{
        Socket client=null;
        TalkFrame tf=new TalkFrame();
        String s;
        BufferedReader is =null;//input from client
        PrintWriter os=null;
        public Server(Socket client){
            this.client=client;
            tf.setVisible(true);
            try {
                is = new BufferedReader(new InputStreamReader(client.getInputStream()));
                os=new PrintWriter(client.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                
            }
            tf.send.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    String str=tf.talkbox.getText();
                    s=s+"
    ME:"+str;
                    tf.showbox.setText(s);
                    os.println(str);
                    os.flush();
                }
                
            });
            
        }
        public void run() {
            String readline;
            try {
                readline = is.readLine();
                while(!readline.equals("bye")) {
                    s=s+"
    HE:"+readline;
                    tf.showbox.setText(s);
                    readline = is.readLine();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    }

    界面如下:

    package testpackage;
    
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.*;
    
    public class TalkFrame extends JFrame {
        public JPanel mainPanel=new JPanel();
        public JTextArea showbox=new JTextArea();
        public JPanel bottom=new JPanel();
        public JButton send=new JButton("send");
        public JTextArea talkbox=new JTextArea();
        public TalkFrame() {
            this.add(mainPanel);
            mainPanel.add(showbox);
            showbox.setAutoscrolls(true);
            showbox.setPreferredSize(new Dimension(500,500));
            showbox.setEditable(false);
            mainPanel.add(bottom);
            bottom.add(talkbox);
            bottom.add(send);
            talkbox.setPreferredSize(new Dimension(300,20));
            mainPanel.setPreferredSize(new Dimension(500,550));
            FlowLayout fl=new FlowLayout();
            fl.setAlignment(FlowLayout.LEADING);
            fl.setAlignOnBaseline(true);
            this.setLayout(fl);
            this.pack();
            //this.setVisible(true);
        }
        public static void main(String a[]) {
            new TalkFrame().setVisible(true);
        }
    }

    对于接受和链接socket的方法 上篇已经提到过,只要吧两个socket用这个类封装一下就行了:https://www.cnblogs.com/mayeye/p/9571740.html

  • 相关阅读:
    Atom+latex+中文环境
    pytorch中,不同的kernel对不同的feature map进行卷积之后输出某一个channel对应的多个feature map如何得到一个channel的feature map
    Ubuntu16.04上添加用户以及修改用户所属的组
    shell批处理文件,并将运算结果返回
    pytorch如何能够保证模型的可重复性
    Linux用管道命令对文件的移动
    python中调用多线程加速处理文件
    Python中random模块在主函数中设置随机种子是否对于调用的函数中的随机值产生影响?
    pytorch统计模型参数量
    pytorch使用tensorboardX进行网络可视化
  • 原文地址:https://www.cnblogs.com/mayeye/p/9574263.html
Copyright © 2011-2022 走看看