john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Java Chat Client

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


public class Client {
    public static void main(String[] args) throws IOException, NumberFormatException {


        Socket chatSocket = null;
        PrintWriter toServerStream = null;
        BufferedReader fromServerStream = null;
        InputStreamReader serverReady = null;
        InputStreamReader clientReady = null;
        PrintWriter fileout = null;


        String old = "129.59.61.188";
        String me = "129.59.100.12";



        int portNumber;     //8888

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("enter the port you wish to use (>2000)");
        System.out.flush();
        portNumber = Integer.parseInt( stdIn.readLine() );

        try {
            chatSocket = new Socket(me, portNumber);
            toServerStream = new PrintWriter(chatSocket.getOutputStream(), true);
            fromServerStream = new BufferedReader(new InputStreamReader(chatSocket.getInputStream()));
            serverReady = new InputStreamReader(chatSocket.getInputStream());
            clientReady = new InputStreamReader(System.in);
            fileout = new PrintWriter (new FileWriter("chatlog.txt"));

        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: servername.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: servername 129.59.61.188.");
            System.exit(1);
        }

        String fromServer;
        String fromUser;
        String userName;


//WELCOME
//gets request for chat name, displays request to user,
//user types in first try, sent to server

        fromServer = fromServerStream.readLine();       //asks for Chat Name
        System.out.println(fromServer);                 //puts that on the screen
        fileout.println(fromServer);

        userName = stdIn.readLine();                    //user types in a name
        toServerStream.println(userName);               //sends it to server for validation

//if invalid the error control string will be sent followed by
//an error message for the user and a request to try again
        fromServer = fromServerStream.readLine();
        while(fromServer.equals("/RETRY NAME"))     //tests for the error control string
        {
            fromServer = fromServerStream.readLine();       //gets the error message
            System.out.println(fromServer);                 //puts that on the screen
            userName = stdIn.readLine();                    //user types in a name
            toServerStream.println(userName);               //sends it to server for validation
            fromServer = fromServerStream.readLine();       //gets the error message
        }
//otherwise it displays the "validated" message followed by the validated userName
        System.out.println(fromServer);
        userName = fromServerStream.readLine();         //validated Username returned
        System.out.println("Welcome " + userName);

//RECEIVE MODE

        while(true)
        {
            do{
                if(clientReady.ready()) //if client's typing
                    {   break;          //break the wait loop
                    }else{
                        if(serverReady.ready()) //if server has message
                        {
                            fromServer = fromServerStream.readLine();
                            System.out.println(fromServer);
                            fileout.println(fromServer);
                        }
                        else{   MyTimer.timeFor(250);   }
                    }
                }while(true);

                fromUser  = stdIn.readLine();
//SEND MODE
                if(fromUser.equals("")) //the only way they can enter a message or command
                {                           //is by a single return press, then they can type
                                            //this is to prevent incoming / outgoing mess
                    toServerStream.println("/USER SENDING");
                    System.out.println("Good job, type on...");
                    System.out.print("-");
                    fromUser = stdIn.readLine();
                    if(fromUser.equals("quit"))
                    {
                        System.out.println("Y'all come back now, ya hear!");
                        toServerStream.println("/LOG OFF");
                        break;
                    }
                    else
                    {
                        toServerStream.println(userName + ": " + fromUser);
                    }

                }//if close
                else
                {
                    System.out.println("You must type a return character alone to send a message or command");
                    System.out.println("everything else will be ignored!");


                }

    }//while close

        toServerStream.close();
        fromServerStream.close();
        stdIn.close();
        chatSocket.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);
    }
}

  • « 4 19 Server
  • Java Chat Server Slave »

Published

Apr 19, 2001

Category

java-chat-university

~374 words

Tags

  • chat 19
  • client 14
  • java 252