john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Clientdemo

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


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

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

        String me = "129.59.61.188";
        String old = "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;
        boolean demo = false;


//recv mode
//the welcome stuff
        fromServer = fromServerStream.readLine();       //asks for Chat Name
        System.out.println(fromServer);                 //puts that on the screen
        fileout.println(fromServer);
        fromUser = stdIn.readLine();                    //user types it in
        toServerStream.println(fromUser);               //sends it to server for validation
        Username = fromServerStream.readLine();         //validated Username returned


        System.out.println("Your chat name is: " + Username);
        fileout.println("Your chat name is: " + Username);
        System.out.println("You must type a return character alone to send a message or command");
        System.out.println("everything else will be ignored!");
        System.out.println("The currently available commands are:");
        System.out.println("'/quit', '/demo', '/quit demo' and '/help'");
        fileout.println("You must type a return character alone to send a message or command");
        fileout.println("everything else will be ignored!");
        fileout.println("The currently available commands are:");
        fileout.println("'/quit', '/demo', '/quit demo' and '/help'");

        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{
                    if(demo == true)
                    {
                        System.out.println("Nobody: this is a demo");
                        fileout.println("Nobody: this is a demo");
                    }
                    MyTimer.timeFor(200);
                }

            }while(true);

            fromUser  = stdIn.readLine();

            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...");
                    fileout.println("Good job, type on...");
                    System.out.print("-");
                    fromUser = stdIn.readLine();
                    if(fromUser.equals("/quit"))
                    {
                        toServerStream.println("/LOG OFF");
                        fileout.println("/quit");
                        System.out.println("Y'all come back now, ya hear!");
                        fileout.println("Y'all come back now, ya hear!");
                        break;
                    }
                    else if(fromUser.equals("/help"))
                    {
                        toServerStream.println("/HELP");
                        fileout.println("/help");
                        System.out.println("You must type a return character alone to send a message or command");
                        System.out.println("everything else will be ignored! (It's SMART CHAT, duh!)");
                        System.out.println("The currently available commands are:");
                        System.out.println("'/quit', '/demo', '/quit demo' and '/help'");
                        fileout.println("You must type a return character alone to send a message or command");
                        fileout.println("everything else will be ignored! (It's SMART CHAT, duh!)");
                        fileout.println("The currently available commands are:");
                        fileout.println("'/quit', '/demo', '/quit demo' and '/help'");

                    }
                    else if(fromUser.equals("/demo"))
                    {
                        toServerStream.println("/DEMO");
                        fileout.println("/demo");
                        demo = true;
                    }
                    else if(fromUser.equals("/quit demo"))
                    {
                        toServerStream.println("/DEMO");
                        fileout.println("/quit demo");
                        demo = false;
                    }
                    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! (It's SMART CHAT, duh!)");
                }

    }//while close

        fileout.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);
    }
}

  • « Serverdemo
  • stringwork »

Published

Apr 18, 2001

Category

java-chat-university

~460 words

Tags

  • chatcrap 23
  • clientdemo 1