Results 1 to 3 of 3
  1. #1
    agfre44_9873 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Server.java Errors trying to build a chat

    I'm new to this stuff. Im trying to build a chat room. a have a file called server.java. I keep coming across this error when running javac Server.java...

    Note: Server.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    I don't think I have the path set right. Im having problems with all the other chat files. Mostly error....

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Server.main(Server.java:92)

    Again, I really don't know what's wrong. I intall the vrtual machine under Program File-> java. in the java folder I have jdk1.6.0_13 folder, jre1.6.o folder, and jre6 folder. How to I SET PATH and what do I set it with? Is Server.java, Client.java, ClientApplet.java, ServerThread.java suppose to be inside the java folder?

    Here's the code for Server.java. If anyone likes the rest contact me. I'll send them to you.

    import java.io.*;
    import java.net.*;
    import java.util.*;
    public class Server
    {
    // The ServerSocket we'll use for accepting new connections
    private ServerSocket ss;

    // A mapping from sockets to DataOutputStreams. This will
    // help us avoid having to create a DataOutputStream each time
    // we want to write to a stream.
    private Hashtable outputStreams = new Hashtable();


    // Constructor and while-accept loop all in one.
    public Server( int port ) throws IOException {
    // All we have to do is listen
    listen( port );
    }

    private void listen( int port ) throws IOException {
    // Create the ServerSocket
    ss = new ServerSocket( port );
    // Tell the world we're ready to go
    System.out.println( "Listening on "+ss );
    // Keep accepting connections forever
    while (true) {
    // Grab the next incoming connection
    Socket s = ss.accept();
    // Tell the world we've got it
    System.out.println( "Connection from "+s );
    // Create a DataOutputStream for writing data to the
    // other side
    DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
    // Save this stream so we don't need to make it again
    outputStreams.put( s, dout );
    // Create a new thread for this connection, and then forget
    // about it
    new ServerThread( this, s );
    }
    }
    // Get an enumeration of all the OutputStreams, one for each client
    // connected to us
    Enumeration getOutputStreams() {
    return outputStreams.elements();
    }
    // Send a message to all clients (utility routine)
    void sendToAll( String message ) {
    // We synchronize on this because another thread might be
    // calling removeConnection() and this would screw us up
    // as we tried to walk through the list
    synchronized( outputStreams ) {
    // For each client ...
    for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {


    // ... get the output stream ...
    DataOutputStream dout = (DataOutputStream)e.nextElement();
    // ... and send the message
    try {
    dout.writeUTF( message );
    } catch( IOException ie ) { System.out.println( ie ); }
    }
    }
    }
    // Remove a socket, and it's corresponding output stream, from our
    // list. This is usually called by a connection thread that has
    // discovered that the connectin to the client is dead.
    void removeConnection( Socket s ) {
    // Synchronize so we don't mess up sendToAll() while it walks
    // down the list of all output streamsa
    synchronized( outputStreams ) {
    // Tell the world
    System.out.println( "Removing connection to "+s );
    // Remove it from our hashtable/list
    outputStreams.remove( s );
    // Make sure it's closed
    try {
    s.close();
    } catch( IOException ie ) {
    System.out.println( "Error closing "+s );
    ie.printStackTrace();
    }
    }
    }


    // Main routine
    // Usage: java Server <port>
    public static void main( String args[] ) throws Exception {
    // Get the port # from the command line
    int port = Integer.parseInt( args[0] );
    // Create a Server object, which will automatically begin
    // accepting connections.
    new Server( port );
    }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Hi,
    My first question here is "Have you passed any argument to this class?"
    without passing argument if u try to use,then only u will get "ArrayindexOutOfBoundsException".
    -Regards
    Ramya

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Read the error messages. You are indexing beyond the end of an array in line 92 (because you haven't passed any arguments, or checked for this case).

    Do as the message says and compile with -Xlint:unchecked to find out the other bad stuff you're doing.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 01:22 AM
  2. [SOLVED] UDP chat client server
    By Koren3 in forum Networking
    Replies: 2
    Last Post: 04-25-2009, 02:51 AM
  3. Multi-user chat server and client
    By 435.mahesh in forum Java Software
    Replies: 6
    Last Post: 04-25-2009, 01:45 AM
  4. Replies: 1
    Last Post: 03-23-2009, 04:08 PM
  5. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 03:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •