Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: simple Java chatroom

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple Java chatroom

    Quite simple I want to create a chatroom that accepts multiple clients all of which can assign their own ID. Whenever they input anything it is sent to all users. Currently I have a echo client server where the client inputs something and it is echoed back. My first question is how do I allow a user to give himself a username? obviously I need a variable somewhere that accepts the name what class do you recommend I put this in? and how would I get the name itself Something like if (theInput.equalsIgnoreCase("username" : ""))

    Then all that I need to do is echo what the client says to all clients. I'm at a loss of how to do this so any advice would be much appreciated. Although I've found some tutorials and example code online I don't understand it and I don't feel comfortable using it if I don't understand even if it does work. Thanks

    Here is my code: EchoClient
    '// echo client
    import java.util.Scanner;
    import java.io.*;
    import java.net.*;
    public class EchoClient {
    public static void main(String[] args) throws IOException { 
        Socket echoSocket = null;
        //PrintWriter socketOut = null;
        try {
            echoSocket = new Socket("127.0.0.1", 4444); // connect to self at port 4444
            System.out.println("Connected OK");
            Scanner socketIn = new Scanner(echoSocket.getInputStream());  // set up input from socket
            PrintWriter socketOut = new PrintWriter(echoSocket.getOutputStream(), true);    // set up output to socket
            Scanner kbdIn = new Scanner(System.in);     // Scanner to pick up keyboard input
            String serverResp = "";
            String userInput = kbdIn.nextLine();        // get input from the user
     
            while (true) {
                socketOut.println(userInput);                   // send user input to the socket
                serverResp =  socketIn.nextLine();     // get the response from the socket
                System.out.println("echoed back: " + serverResp);   // print it out
                if (serverResp.equals("Closing connection")) { break; } //break if we're done
                userInput = kbdIn.nextLine();   // get next user input
            }
            socketOut.close();
            kbdIn.close();
            socketIn.close();
        }
        catch (ConnectException e) {
            System.err.println("Could not connect to host");
            System.exit(1);
        }
        catch (IOException e) {
            System.err.println("Couldn't get I/O for connection");
            System.exit(1);
        }
        echoSocket.close();
    } 
    }'
     
    EchoServer
     
    // multiple (threaded) server
    import java.net.ServerSocket;
    import java.net.Socket;
    public class EchoServerMult {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(4444);   // create server socket on this machine
        System.err.println("Started server listening on port 4444");
        while (true) {        // as each connection is made, pass it to a thread
            //new EchoThread(serverSocket.accept()).start();  // this is same as next 3 lines
            Socket x = serverSocket.accept();   // block until next connection
            EchoThread et = new EchoThread(x);
            et.start();  
            System.err.println("Accepted connection from client");
        }
    }
    }
     
    EchoThread
     
    // thread to handle one echo connection
    import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    public class EchoThread extends Thread {
    private Socket mySocket = null;
     
    public EchoThread(Socket socket) {       // constructor method
        mySocket = socket;
    }
    public void run() {
        try {
            PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true);
            Scanner in = new Scanner(mySocket.getInputStream());
            String inputLine;
            while (true) {
                inputLine = in.nextLine();      
                if (inputLine.equalsIgnoreCase("Bye")) {
                    out.println("Closing connection");
                    break;      
                } else {
                    out.println(inputLine);
                }
            }
            out.close();
            in.close();
            mySocket.close(); 
        } catch (Exception e) {
            System.err.println("Connection reset");   // bad error (client died?)
        }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: simple Java chatroom

    how do I allow a user to give himself a username?
    Have a menu item that calls a listener and prompt the user using a JOptionPane class method.

    echo what the client says to all clients.
    Have a list of clients that are connected, iterate through the list and send the message to everyone in the list.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need some simple Java Help
    By traewall11 in forum Object Oriented Programming
    Replies: 11
    Last Post: September 26th, 2012, 05:11 PM
  2. Simple I/O Java Error
    By Prox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2011, 04:48 PM
  3. eclipse java simple example
    By aswra1 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 24th, 2011, 10:21 AM
  4. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM
  5. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM

Tags for this Thread