• Socket 实现简单的多线程服务器程序


    **********服务器端*************

    public class ServerSocket{

        public static void main(String[] args) throws Exception {
             try{
                 ServerSocket ss=new ServerSocket(8888);
                 System.out.println("启动服务器...");
                 //记录客户端的链接数量
                 int count=0;
                 //循环侦听等待客户端的链接
                 while(true){
                    //侦听并接受到此套接字的连接。
                     Socket s=ss.accept();
                    //创建一个线程
                     ServerThread mt=new ServerThread(s);
                     mt.start();
                     count++;
                     System.out.println("客户端的连接数量:"+count);
                     System.out.println("客户端:"+s.getInetAddress().getHostName()+"已连接到服务器");
                 }
             }catch(IOException e){
                 e.printStackTrace();
                
             }
        }
    }

    class ServerThread extends Thread {
        Socket s=null;
        public ServerThread(Socket s){
            this.s=s;
        }
        
        public void run(){
            BufferedReader br=null;
            BufferedWriter bw=null;
            try{
                //读取服务器的输入流,也就是从客户端来的消息
                 br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                 //读取客户端发送来的消息
                 String mess=br.readLine();
                 System.out.println("客户端:"+mess);
                 //服务器的输出流,也就是向客户端返回的消息
                 bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                 bw.write("您已成功链接服务器"+" ");
                 bw.flush();
            }catch(IOException e){
                e.printStackTrace();
            }finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
        }
    }

    **************客户端*********************

    public class Client{

        public static void main(String[] args) {
            try {
                Socket s=new Socket("127.0.0.1", 8888);
                
                //读取服务器返回的消息
                BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                String mess=br.readLine();
                System.out.println("服务器:"+mess);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

  • 相关阅读:
    每个女孩都希望她的追求者是一个M/M/1排队系统
    一门考试结业后
    编程实现贝叶斯分类
    大禹治水的新闻采阅系统(草稿版)
    正则表达式之获取匹配,非获取匹配,正向预查,负向预查
    frame或者iframe的contentwindow属性
    installanywhere's LAX Properties
    查看域名下主机信息
    在Java中Vector和ArrayList的区别
    php SNMP函数时出错
  • 原文地址:https://www.cnblogs.com/Downtime/p/7922864.html
Copyright © 2020-2023  润新知