john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

sunclient

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


public class client {


    public static void main(String[] args) throws IOException, NumberFormatException {
        String userName;

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

        String fromServer;
        String fromUser;

        String IPaddress;
        int portNumber = 8888;      //some arbitrary number

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

        try {   fileout = new PrintWriter (new FileWriter("chatlog.txt"));  }
        catch (IOException e){  System.err.println("Couldn't open file for chatlog.txt"); }


        screenout("enter the IP of the server you wish to use:", fileout);
        System.out.flush();
        IPaddress = stdIn.readLine();
        fileout.println(IPaddress);
        fileout.println("Port Number: " + portNumber);

        try {
                chatSocket = new Socket(IPaddress, portNumber);
                toServerStream = new PrintWriter(chatSocket.getOutputStream(), true);
                fromServerStream = new BufferedReader(new InputStreamReader(chatSocket.getInputStream()));
                serverReady = new InputStreamReader(chatSocket.getInputStream());
                clientReady = new InputStreamReader(System.in);

            } 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" + IPaddress);
                System.exit(1);
            }


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

    //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();           //Welcome message
        screenout(fromServer, fileout);

        do{
            fromServer = fromServerStream.readLine();       //asks for Chat Name or gets error message
            screenout(fromServer, fileout);                 //or prints the error message
            userName = stdIn.readLine();                    //user types in a name
            serverout(userName, fileout, toServerStream);           //sent to the Server for validation
            fromServer = fromServerStream.readLine();       //server sends back a valid message
        }                                                   //or a RETRY NAME control message
        while(fromServer.equals("/RETRY NAME"));            //tests for the control string

        //otherwise it displays the "validated" message followed by the validated userName
        screenout(fromServer, fileout);
        userName = fromServerStream.readLine();         //validated Username returned

        screenout("Welcome " + userName, fileout);
        screenout("Press return before typing in a message or command.", fileout);
        screenout("You may type /help at any time to get a list of commands", fileout);
        screenout("", fileout);

        //RECEIVE MODE

        while(true)
        {
            while(true)
            {
                if(clientReady.ready()) //if client's typing
                {   break;      }   //break the wait loop to SEND mode

                if(serverReady.ready()) //if server has message
                {
                        fromServer = fromServerStream.readLine();
                        screenout(fromServer, fileout);
                }
            }

            System.out.flush();
            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");    //control message

                screenout("Enter command or message to send...", fileout);
                System.out.print("->");
                fileout.print("->");

                fromUser = stdIn.readLine();
                serverout(fromUser, fileout, toServerStream);

                if (fromUser.equals("/quit"))
                {   break;      }

            }//if close
            else
            {
                screenout("", fileout);
                screenout("ERROR: Press return before typing in a message!", fileout);
                screenout("", fileout);
            }

        }//while close



        serverReady.close();        //server message test
        clientReady.close();        //client input test
        fileout.close();        //log file stream
        toServerStream.close();     //to Server
        fromServerStream.close();   //from Server
        stdIn.close();          //from keyboard
        chatSocket.close();     //the socket

    }

//These allow less lines of code and "transparently" logs the user actions
//and the chat session- it would be more efficient inline or as a macro

    public static void screenout(String message, PrintWriter fileout)
    {
        System.out.println(message);    //to the user's screen
        fileout.println(message);       //to the chatlog.txt file
    }

    public static void serverout(String message, PrintWriter fileout, PrintWriter toServerStream)
    {
        toServerStream.println(message);    //user input sent to the Server
        fileout.println(message);           //to the chatlog.txt file
    }


} // Class client

  • « Server
  • sunserver »

Published

Apr 23, 2001

Category

java-chat-university-v2

~461 words

Tags

  • chat 19
  • client 14
  • java 252