Hey please I am trying to create a chat system but my main server routine in my server class keeps give error saying its out of bound. I think it is the port number but dont know how to fix it. Is there anyway I can hard code the port number instead of asking for it from the command line.

import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
// The ServerSocket we'll use for accepting new connections
private ServerSocket ss;
private Hashtable outputStreams = new Hashtable();
public Server( int port ) throws IOException {
listen( port );
}
private void listen( int port ) throws IOException {
ss = new ServerSocket( port );
System.out.println( "Listening on "+ss );
while (true) {
// Grab the next incoming connection
Socket s = ss.accept();
// Tell the world we've got it
System.out.println( "Connection from "+s );
DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
outputStreams.put( s, dout );
new ServerThread( this, s );
}
}
Enumeration getOutputStreams() {
return outputStreams.elements();
}
// Send a message to all clients (utility routine)
void sendToAll( String message ) {
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 ); }
	}
	}
	}
	void removeConnection( Socket s ) {
	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();
	}
	}
	}
	static public void main( String args[] ) throws Exception {
	// Get the port # from the command line
	int port = Integer.parseInt( args[0] );
	new Server( port );
	}
}
import java.io.*;
import java.net.*;

public class ServerThread extends Thread
{
  // The Server that spawned us
  private Server server;

  // The Socket connected to our client
  private Socket socket;

  // Constructor.
  public ServerThread( Server server, Socket socket ) {

    // Save the parameters
    this.server = server;
    this.socket = socket;

    // Start up the thread
    start();
  }

  // This runs in a separate thread when start() is called in the
  // constructor.
  public void run() {

    try {

      DataInputStream din = new DataInputStream( socket.getInputStream() );

      // Over and over, forever ...
      while (true) {

        // ... read the next message ...
        String message = din.readUTF();

        // ... tell the world ...
        System.out.println( "Sending "+message );

        // ... and have the server send it to all clients
        server.sendToAll( message );
      }
    } catch( EOFException ie ) {

      // This doesn't need an error message
    } catch( IOException ie ) {

      // This does; tell the world!
      ie.printStackTrace();
    } finally {

      server.removeConnection( socket );
    }
  }
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class Client extends Panel implements Runnable
{
  private TextField tf = new TextField();
  private TextArea ta = new TextArea();

  private Socket socket;
  private DataOutputStream dout;
  private DataInputStream din;

  // Constructor
  public Client( String host, int port ) {

    // Set up the screen
    setLayout( new BorderLayout() );
    add( "North", tf );
    add( "Center", ta );
   tf.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        processMessage( e.getActionCommand() );
      }
    } );

    // Connect to the server
    try {

      // Initiate the connection
      socket = new Socket( "127.0.0.1", 4444 );

      // We got a connection!  Tell the world
      System.out.println( "connected to "+socket );

      din = new DataInputStream( socket.getInputStream() );
      dout = new DataOutputStream( socket.getOutputStream() );

      // Start a background thread for receiving messages
      new Thread( this ).start();
    } catch( IOException ie ) { System.out.println( ie ); }
  }

  // Gets called when the user types something
  private void processMessage( String message ) {
    try {

      // Send it to the server
      dout.writeUTF( message );

      // Clear out text input field
      tf.setText( "" );
    } catch( IOException ie ) { System.out.println( ie ); }
  }

  // Background thread runs this: show messages from other window
  public void run() {
    try {

      // Receive messages one-by-one, forever
      while (true) {

        // Get the next message
        String message = din.readUTF();

        // Print it to our text window
        ta.append( message+"\n" );
      }
    } catch( IOException ie ) { System.out.println( ie ); }
  }
}
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class ClientApplet extends Applet
{
  public void init() {
    String host = getParameter( "host" );
    int port = Integer.parseInt( getParameter( "port" ) );
    setLayout( new BorderLayout() );
    add( "Center", new Client( host, port ) );
  }
}

There is more to that exception than just "out of bounds". It tells you exactly where it happens, it might help if you tell us that as well.

The error is in quote below and it is on line 59 of the server class.
"exception in thread main java.lang.arrayindexoutofboundsexception 0"

And what is line 59 of the server class? Seeing as how there is no package statement in the posted code (you do have one, right?) I can only assume that the entire code is not posted so line 59 means less than nothing to me, and I wouldn't sit here and count lines in the first place.

I am using textpad and you dont have to count the lines because the code I posted already have line numbers attached to it. The server class is the Public Class Server which is the first wrap code. The line that is giving error is stated below:
"static public void main( String args[] ) throws Exception { int port = Integer.parseInt( args[0] );
new Server( port );"

This part of the code is on line 59 of the first wrapped code

Well, when you execute the command are you providing any arguments?
i.e. java <class> <arg0> <arg1>
If not, then of course args[0] is not going to exist. You need to check the length of the args array before you blindly start indexing into it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.