Solved

Overcoming this runtime exception

Posted on 2003-12-02
6
322 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 193
How to convert String matching to regex in java 4 32
MySqlDump not dumping triggers 1 30
Android development question 2 36
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)
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 about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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