• Java Socket编程之多线程实现C/S一对多(服务端无法发送数据)


    Server端:

    package com.wjy.server;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    /*
     * caution:boot the server first.
     * @author JiyuanWang
     *
     */
    public class TalkServer extends JFrame implements ActionListener,Runnable{
        private  JButton send=null;
        private  JTextField inputText=null;
        private  JTextArea showText=null;
        private  ServerSocket serverSocket=null;
        private DataOutputStream out=null;
        public TalkServer(){
        }
    public  void handle()
    {
        try {
            serverSocket=new ServerSocket(4170, 5);
            
            while (true) {
                Socket socket=serverSocket.accept();
                showText.append("Connect successd.\n");
                Thread thread=new ThreadHandle(socket);
                thread.start();
                
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub    
            /*try {
                out.writeUTF(inputText.getText());
                out.flush();//本人一直把这个程序调试不通,最后发现没加这句话导致服务端显示不了客户端的输入。
                showText.append("I said: "+inputText.getText()+"\n");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }*/
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        send=new JButton("Send");
        send.addActionListener(this);
        send.setEnabled(true);
        
        inputText=new JTextField();
        
        showText=new JTextArea();
        
        add(send,BorderLayout.NORTH);
        add(inputText, BorderLayout.SOUTH);
        add(showText, BorderLayout.CENTER);
        
        setTitle("Server");
        setBounds(1000,0,400,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        handle();
    }
    private class ThreadHandle extends Thread{
        private Socket midSocket;
        public ThreadHandle(Socket midSocket){
            this.midSocket=midSocket;
        }
        public void run() {
            
            try {
                DataInputStream dataInputStream=new DataInputStream(midSocket.getInputStream());
                DataOutputStream dataOutputStream=new DataOutputStream(midSocket.getOutputStream());
                while(true){
                showText.append("Client said: "+dataInputStream.readUTF()+"\n");
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
    }

    Client端:

    package com.wjy.client;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    /**
     * caution:boot the server first.
     * @author JiyuanWang
     *
     */
    
    public class TalkClient extends JFrame implements ActionListener,Runnable{
        private  JButton send=null;
        private  JTextField inputText=null;
        private  JTextArea showText=null;
        private  Socket socket=null;
        private  DataInputStream inputStreamReader=null;
        private  DataOutputStream outputStreamWriter=null;
        public TalkClient(){
        }
    public void handle()
    {
        try {
            socket=new Socket("127.0.0.1",4170);
            inputStreamReader=new DataInputStream(socket.getInputStream());
            outputStreamWriter=new DataOutputStream(socket.getOutputStream());
            while(true){
                showText.append("The Server said: "+inputStreamReader.readUTF()+"\n");
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try {
            outputStreamWriter.writeUTF(inputText.getText());
            outputStreamWriter.flush();   //本人一直把这个程序调试不通,最后发现没加这句话导致服务端显示不了客户端的输入。
            showText.append("I said: "+inputText.getText()+"\n");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        send=new JButton("Send");
        send.addActionListener(this);
        send.setEnabled(true);
        
        inputText=new JTextField();
        
        showText=new JTextArea();
        
        add(send,BorderLayout.NORTH);
        add(inputText, BorderLayout.SOUTH);
        add(showText, BorderLayout.CENTER);
        
        setTitle("Client");
        setBounds(0,0,400,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        handle();
    }
    }

    main函数:

    package start;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    
    import com.wjy.client.TalkClient;
    import com.wjy.server.TalkServer;
    
    public class start {
    public static void main(String[] args){
        Thread server=new Thread(new TalkServer());
        Thread clientA=new Thread(new TalkClient()); 
        Thread clientB=new Thread(new TalkClient()); 
        server.start();
        clientA.start();
        clientB.start();
    }
    }
  • 相关阅读:
    金牙与肉屑
    科学研究的动机以及雄心
    适度的自我吹嘘
    ubuntu12启用root账户
    有很多文件夹是受系统保护的
    vs2012换肤功能,vs2012主题及自定义主题
    Ubuntu navicat for mysql 安装和使用
    Asp.Net MVC4 Bundle捆绑压缩技术
    64位Windows Jmail组件报错解决方案
    C#检测上传文件的真实类型
  • 原文地址:https://www.cnblogs.com/wangjiyuan/p/socketsimplecs2.html
Copyright © 2020-2023  润新知