• 服务器(多线程)


    package serverAndClient;
     
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    import java.util.Timer;
    import java.util.TimerTask;
     
    class Server extends Thread
    {
        private BufferedReader in = null;
        private PrintWriter out = null;
        private Socket socket = null;
        public static int count=0;
        public JabberClient dangQianClient=null;
        Scanner sc = new Scanner(System.in);
     
        class JieShou extends TimerTask
        {
     
            String temp = null;
     
            public void run()
            {
                try
                {temp = in.readLine();
                if(temp!=null)    
     
                    System.out.println(temp);
     
                } catch (Exception e)
                {System.out.println( e.getMessage());
                }
            }
        }
     
        public Server(Socket socket) throws IOException
        {
     
            this.socket = socket;
            System.out.println("有一个新的客户端连接"+socket);
            System.out.println("当前的客户端数目: "+ ++count);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()), true);
            Timer timer = new Timer();
            timer.schedule(new JieShou(), 100, 300);
            this.start();
        }
     
        public void run()
        {
            while (true)
            {
                String temp=sc.next();
                 out.println("服务器: " + temp);
            }
     
     
        }
    }
     
    public class MultiJabberServer
    {
     
        public static final int port = 8080;
     
        public static void main(String[] args) throws IOException
        {
     
            InetAddress addr=InetAddress.getLocalHost();
            System.out.println(addr.getHostAddress());
            ServerSocket serverSocket = new ServerSocket(port);
            try
            {
                while (true)
                {
     
                    Socket socket = serverSocket.accept();
                    try
                    {
                        new Server(socket);
                    } catch (Exception e)
                    {
                        socket.close();
                    }  
                }
            } finally
            {
                serverSocket.close();
                System.exit(0);
            }
        }
     
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    Windows常用内容渗透命令
    字符串编码小记
    卡巴斯基命令解析
    聚合与继承
    Maven与Eclipse整合
    maven系列三:Maven核心概念
    使用Maven构建项目
    Maven项目构建过程练习
    maven系列二:Maven入门
    使用Maven构建多模块项目
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710747.html
Copyright © 2020-2023  润新知