john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Java Chat Server Beta Multithreaded v3

import java.net.*;
import java.io.*;


public class Server {
    public static void main(String[] args) throws IOException {

        int port = 8888;
        ServerSocket serverSocket = null;
        Socket clientSocket = null;

        ClientThread slave = null;

        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + port + ".");
            System.exit(1);
        }

        try {
            while (true)
            {       try {    clientSocket = serverSocket.accept();  }
                    catch (IOException e)
                        {   System.err.println("Accept failed.");
                            System.exit(1);             }

                slave = new ClientThread (clientSocket);
                slave.start ();
            }
        } finally {     serverSocket.close();   }
    }//main close
}//serverthread class close


class ClientThread extends Thread
{
    Socket clientSocket = null;

    //gives this thread a new port so the server can still listen on the original port
    public ClientThread (Socket clientSocket)
    {       this.clientSocket = clientSocket;       }


    public void run ()
    {
        BufferedReader clientIn = null;
        PrintWriter clientOut = null;
        InputStreamReader input = null;

        try {
clientIn = new BufferedReader ( new InputStreamReader(clientSocket.getInputStream() ) );
clientOut = new PrintWriter(clientSocket.getOutputStream(), true);
input = new InputStreamReader(clientSocket.getInputStream() );
            }  catch (IOException e) {
                    System.err.println("Could not I/O Client Socket");
                    System.exit(1);
            }

//clientIn, clientOut
        try {

            String inputLine, outputLine;//these var names suck
            String Username;            //outputLine will be mostly for errors

            clientOut.println("Welcome to this CHAT SERVER, What is your Chat Name?");
            Username = clientIn.readLine();

//perform the validation tests!

            do{
                do{
                    if(input.ready())   //if there's a newline
                    {   clientOut.println("you've got something to say?");
                        break;  //break
                    }else{
//                      clientOut.println("Some messages popped");
                        MyTimer.timeFor(250);//a quarter second wait
                    }                       //eventually we get from FIFO Q
                }while(true);

                inputLine  = clientIn.readLine();
                if(inputLine.equals(""))    //the only way they can enter a message or command
                {                           //is by a single return press, then they can type
                    clientOut.print("-");
                    outputLine = "Username: " + clientIn.readLine();
                    clientOut.println(outputLine);

                    if(outputLine.equals("Username: quit"))
                    {       clientOut.println("LOG OFF");
                            clientOut.close();
                            clientIn.close();
                            clientSocket.close();
                    }

                }//if close
            }while(true);


    } catch (IOException e){
            System.err.println("I/O problems");
            System.exit(1);
        }


    }//run

}//class close

class MyTimer
{

//pauses for the "duration" in milliseconds
    public static void timeFor(int duration)
    {
        long timer;
        long starttime;

            starttime = System.currentTimeMillis();
            do{
                timer = System.currentTimeMillis();
            }while( (timer - starttime) < duration);
    }
}

  • « MTEchoServer
  • ChatServer »

Published

Apr 17, 2001

Category

java-chat-university

~263 words

Tags

  • chat 19
  • java 252
  • multithreaded 5
  • server 66