Solved

Overcoming this runtime exception

Posted on 2003-12-02
6
326 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

730 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