Solved

Overcoming this runtime exception

Posted on 2003-12-02
6
324 Views
Last Modified: 2013-12-29
Hi All,

I have JB9 personal and win2k.

It is my belief that in order to make this server  program run I need the server to be up and running before I open a client or the associated applet .  


Yet  this chat server demo  refuses to run because of this error as reported

     java.lang.RuntimeException: Syntax: ChatServer <port>

      at objectschatserver.ChatServer.main(ChatServer.java:20)

Exception in thread "main"        

There are  two classes that comprise the :

package objectschatserver;

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

public class ChatServer {
  public ChatServer (int port) throws IOException {
    ServerSocket server = new ServerSocket (port);
    while (true) {
      Socket client = server.accept ();
      System.out.println ("Accepted from " + client.getInetAddress ());
      ChatHandler c = new ChatHandler (client);
      c.start ();
    }
  }

  public static void main (String args[]) throws IOException {
    if (args.length != 1)
      throw new RuntimeException ("Syntax: ChatServer <port>");
    new ChatServer (Integer.parseInt (args[0]));
  }
}


package objectschatserver;


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

public class ChatHandler extends Thread {
  protected Socket s;
  protected DataInputStream i;
  protected DataOutputStream o;

  public ChatHandler (Socket s) throws IOException {
    this.s = s;
    i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
    o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
  }

  protected static Vector handlers = new Vector ();

  public void run () {
    String name = s.getInetAddress ().toString ();
    try {
      broadcast (name + " has joined.");
      handlers.addElement (this);
      while (true) {
        String msg = i.readUTF ();
        broadcast (name + " - " + msg);
      }
    } catch (IOException ex) {
      ex.printStackTrace ();
    } finally {
      handlers.removeElement (this);
      broadcast (name + " has left.");
      try {
        s.close ();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }

  protected static void broadcast (String message) {
    synchronized (handlers) {
      Enumeration e = handlers.elements ();
      while (e.hasMoreElements ()) {
        ChatHandler c = (ChatHandler) e.nextElement ();
        try {
          synchronized (c.o) {
            c.o.writeUTF (message);
          }
          c.o.flush ();
        } catch (IOException ex) {
          c.stop ();
        }
      }
    }
  }
}

What is it needing or can that error be compromised with something else to make it run.

Delphi3
0
Comment
Question by:delphi3
6 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9858669
Call it like:

java ChatServer 2222

to open it on port 2222
0
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 9858677
or, change the main method in ChatServer to be:

public static void main (String args[]) throws IOException
{
    int port = 2222 ;

    if( args.length > 0 )
          port = Integer.parseInt( args[ 0 ] ) ;

    new ChatServer( port );
}

then it will default to 2222, unless you pass a different port number to listen on
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9858684
There no need to throw an exception if args.length > 1 so it should be


    if (args.length < 1)
      throw new RuntimeException ("Syntax: ChatServer <port>");

Make sure you pass the port number
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:TimYates
ID: 9858688
if you are running the chatserver inside JB9, then you need to alter the "Run->Configuration", and add 2222 as application parameters :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9861116
> There no need to throw an exception if args.length > 1 so it should be

But doesn't hurt, and it is invalid syntax.

> Make sure you pass the port number

already well covered.

0
 
LVL 4

Author Comment

by:delphi3
ID: 9863504
Thanks to you all,


Tim you're the greatest.

However, I am still frustrated now that this matter above has been taken care of.   Just now  there are
further problems with this super sample example of the chat that I am working with.

Delphi3
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
check java version using powershell 13 168
How  can  i  resolve  HTTP Status 404 -? 8 41
sql import cannot be resolved jsp 3 45
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question