Solved

Overcoming this runtime exception

Posted on 2003-12-02
6
318 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now