www.1q.co.kr(park1q.com)

ID :  Password : Auto  

   ȸ¿ø:0¸í / ¼Õ´Ô:1¸í
 

 

Technote

ÀÚ·á ¹× °ü¸®ÆÁ

  • DBA Notes
  • Q & A

    ºÏ¸¶Å©
  • Asktom
       (Oracle ÀÇ ´ëÇ¥ Forum)
  • Technical Bulltin(KR)
       (±â¼úÁö¿ø°Ô½ÃÆÇ)
  • Dbazine

  •  

     


     [re] [Cool]Multithreaded Client/Server Applications
    park1q  2013-06-05 15:25:38, Á¶È¸ : 4,112, Ãßõ : 1293


    cat MultiThreadChatServer.java
    ÇÑ±Û ±úÁö´Â ¹®Á¦ ÇØ°á..
    import java.io.*;
    import java.net.*;
    public class MultiThreadChatServer{
        // Declaration section:
        // declare a server socket and a client socket for the server
        // declare an input and an output stream
        static  Socket clientSocket = null;
        static  ServerSocket serverSocket = null;
        // This chat server can accept up to 10 clients' connections
        static  clientThread t[] = new clientThread[10];
        public static void main(String args[]) {
            // The default port
            int port_number=2222;
            if (args.length < 1)
                {
                    System.out.println("Usage: java MultiThreadChatServer n"+
                                       "Now using port number="+port_number);
                } else {
                    port_number=Integer.valueOf(args[0]).intValue();
                }
            // Initialization section:
            // Try to open a server socket on port port_number (default 2222)
            // Note that we can't choose a port less than 1023 if we are not
            // privileged users (root)
            try {
                serverSocket = new ServerSocket(port_number);
            }
            catch (IOException e)
                {System.out.println(e);}
            // Create a socket object from the ServerSocket to listen and accept
            // connections.
            // Open input and output streams for this socket will be created in
            // client's thread since every client is served by the server in
            // an individual thread
            while(true){
                try {
                    clientSocket = serverSocket.accept();
                    for(int i=0; i<=9; i++){
                        if(t[i]==null)
                            {
                                (t[i] = new clientThread(clientSocket,t)).start();
                                break;
                            }
                    }
                }
                catch (IOException e) {
                    System.out.println(e);}
            }
        }
    }
    // This client thread opens the input and the output streams for a particular client,
    // ask the client's name, informs all the clients currently connected to the
    // server about the fact that a new client has joined the chat room,
    // and as long as it receive data, echos that data back to all other clients.
    // When the client leaves the chat room this thread informs also all the
    // clients about that and terminates.
    class clientThread extends Thread{
    //    DataInputStream is = null;
        BufferedReader  is = null;
        PrintStream os = null;
        Socket clientSocket = null;
        clientThread t[];
        public clientThread(Socket clientSocket, clientThread[] t){
            this.clientSocket=clientSocket;
            this.t=t;
        }
        public void run()
        {
            String line;
            String name;
            char EOF = (char)0x00;
            try{
                is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                os = new PrintStream(clientSocket.getOutputStream());
                os.println("Enter your name."+EOF);
                name = is.readLine();
                os.println("Hello "+name+" to our chat room.nTo leave enter /quit in a new line"+EOF);
                for(int i=0; i<=9; i++)
                    if (t[i]!=null && t[i]!=this)
                        t[i].os.println("*** A new user "+name+" entered the chat room !!! ***"+EOF);
                while (true) {
                    line =  is.readLine();
                    if(line.startsWith("/quit")) break;
                    for(int i=0; i<=9; i++)
                        if (t[i]!=null)  t[i].os.println("<"+name+"> "+line+EOF);
                }
                for(int i=0; i<=9; i++)
                    if (t[i]!=null && t[i]!=this)
                        t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***"+EOF );
                os.println("*** Bye "+name+" ***"+EOF);
                // Clean up:
                // Set to null the current thread variable such that other client could
                // be accepted by the server
                for(int i=0; i<=9; i++)
                    if (t[i]==this) t[i]=null;
                // close the output stream
                // close the input stream
                // close the socket
                is.close();
                os.close();
                clientSocket.close();
            }
            catch(IOException e){};
        }
    }


      ÃßõÇϱâ ÇÁ¸°Æ®   ¸ñ·Ïº¸±â

       [Etc][Cool]Multithreaded Client/Server Applications  park1q  2013-06-01
    21:57:56
       [Etc][re] [Cool]Multithreaded Client/Server Applications  park1q  2013-06-05
    15:25:38

    Copyright 1999-2026 Zeroboard

     

     
     
    [Today:4 / Total:174371]    Design by p@rk1q