?
Solved

Overcoming this runtime exception

Posted on 2003-12-02
6
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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