How to make the client connect to the server at the command prompt?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: How to make the client connect to the server at the command prompt?

  1. #1
    Join Date
    Oct 2005
    Posts
    6

    How to make the client connect to the server at the command prompt?

    I found this code on IBM's website, it was a training session on servers and clients using java.
    The code compiles fine and the server seems to start up properly when I use java Server 5000. I think whats happening is the server is running and listening for a connection on port 5000.

    When I try to run the client I get the following error.
    Exception in thread "main" java.lang.NoSuchMethodError: main

    I see a start() method but no main. As far as I know, applications should all have main, it seems as if the person who wrote this kinda confused applets with application. Not that I would really know what happened.

    If you have time, could you tell me if there's an easy fix for this? I would love to have this client/server working if it isn't too much trouble. As I have looked all over the net for a free client/server applet that will actually let me see the java code and none of the free ones do allow getting to their source.
    Most of them allow you to customize them somewhat but also have built in advertising that can't be removed.
    This is the closest I have come to finding one that lets me look under the hood. But alas it doesn't work out of the box and I don't know what to do to fix it.
    Heres the code: Server:
    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    public class Server
    {
      // The ServerSocket we'll use for accepting new connections
      private ServerSocket ss;
     
      // A mapping from sockets to DataOutputStreams.  This will
      // help us avoid having to create a DataOutputStream each time
      // we want to write to a stream.
      private Hashtable outputStreams = new Hashtable();
     
      // Constructor and while-accept loop all in one.
      public Server( int port ) throws IOException {
     
        // All we have to do is listen
        listen( port );
      }
     
      private void listen( int port ) throws IOException {
     
        // Create the ServerSocket
        ss = new ServerSocket( port );
     
        // Tell the world we're ready to go
        System.out.println( "Listening on "+ss );
     
        // Keep accepting connections forever
        while (true) {
     
          // Grab the next incoming connection
          Socket s = ss.accept();
     
          // Tell the world we've got it
          System.out.println( "Connection from "+s );
     
          // Create a DataOutputStream for writing data to the
          // other side
          DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
     
          // Save this stream so we don't need to make it again
          outputStreams.put( s, dout );
     
          // Create a new thread for this connection, and then forget
          // about it
          new ServerThread( this, s );
        }
      }
     
      // Get an enumeration of all the OutputStreams, one for each client
      // connected to us
      Enumeration getOutputStreams() {
        return outputStreams.elements();
      }
     
      // Send a message to all clients (utility routine)
      void sendToAll( String message ) {
     
        // We synchronize on this because another thread might be
        // calling removeConnection() and this would screw us up
        // as we tried to walk through the list
        synchronized( outputStreams ) {
     
          // For each client ...
          for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {
     
            // ... get the output stream ...
            DataOutputStream dout = (DataOutputStream)e.nextElement();
     
            // ... and send the message
            try {
              dout.writeUTF( message );
            } catch( IOException ie ) { System.out.println( ie ); }
          }
        }
      }
     
      // Remove a socket, and it's corresponding output stream, from our
      // list.  This is usually called by a connection thread that has
      // discovered that the connectin to the client is dead.
      void removeConnection( Socket s ) {
     
        // Synchronize so we don't mess up sendToAll() while it walks
        // down the list of all output streamsa
        synchronized( outputStreams ) {
     
          // Tell the world
          System.out.println( "Removing connection to "+s );
     
          // Remove it from our hashtable/list
          outputStreams.remove( s );
     
          // Make sure it's closed
          try {
            s.close();
          } catch( IOException ie ) {
            System.out.println( "Error closing "+s );
            ie.printStackTrace();
          }
        }
      }
     
      // Main routine
      // Usage: java Server <port>
      static public void main( String args[] ) throws Exception {
     
        // Get the port # from the command line
        int port = Integer.parseInt( args[0] );
     
        // Create a Server object, which will automatically begin
        // accepting connections.
        new Server( port );
      }
    }

    Code:
    import java.io.*;
    import java.net.*;
     
    public class ServerThread extends Thread
    {
      // The Server that spawned us
      private Server server;
     
      // The Socket connected to our client
      private Socket socket;
     
      // Constructor.
      public ServerThread( Server server, Socket socket ) {
     
        // Save the parameters
        this.server = server;
        this.socket = socket;
     
        // Start up the thread
        start();
      }
     
      // This runs in a separate thread when start() is called in the
      // constructor.
      public void run() {
     
        try {
     
          // Create a DataInputStream for communication; the client
          // is using a DataOutputStream to write to us
          DataInputStream din = new DataInputStream( socket.getInputStream() );
     
          // Over and over, forever ...
          while (true) {
     
            // ... read the next message ...
            String message = din.readUTF();
     
            // ... tell the world ...
            System.out.println( "Sending "+message );
     
            // ... and have the server send it to all clients
            server.sendToAll( message );
          }
        } catch( EOFException ie ) {
     
          // This doesn't need an error message
        } catch( IOException ie ) {
     
          // This does; tell the world!
          ie.printStackTrace();
        } finally {
     
          // The connection is closed for one reason or another,
          // so have the server dealing with it
          server.removeConnection( socket );
        }
      }
    }
    Thanks for your time.
    Last edited by gjon; 06-02-2006 at 10:31 AM.

  2. #2
    Join Date
    Oct 2005
    Posts
    6
    Wups, I didn't post the client code. I have to sort through my mess and find it.
    To be updated....

  3. #3
    Join Date
    Oct 2005
    Posts
    6
    CLIENT:
    Code:
     
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
     
    public class Client extends Panel implements Runnable
    {
      // Components for the visual display of the chat windows
      private TextField tf = new TextField();
      private TextArea ta = new TextArea();
     
      // The socket connecting us to the server
      private Socket socket;
     
      // The streams we communicate to the server; these come
      // from the socket
      private DataOutputStream dout;
      private DataInputStream din;
     
      // Constructor
      public Client( String host, int port ) {
     
        // Set up the screen
        setLayout( new BorderLayout() );
        add( "North", tf );
        add( "Center", ta );
     
        // We want to receive messages when someone types a line
        // and hits return, using an anonymous class as
        // a callback
        tf.addActionListener( new ActionListener() {
          public void actionPerformed( ActionEvent e ) {
            processMessage( e.getActionCommand() );
          }
        } );
     
        // Connect to the server
        try {
     
          // Initiate the connection
          socket = new Socket( host, port );
     
          // We got a connection!  Tell the world
          System.out.println( "connected to "+socket );
     
          // Let's grab the streams and create DataInput/Output streams
          // from them
          din = new DataInputStream( socket.getInputStream() );
          dout = new DataOutputStream( socket.getOutputStream() );
     
          // Start a background thread for receiving messages
          new Thread( this ).start();
        } catch( IOException ie ) { System.out.println( ie ); }
      }
     
      // Gets called when the user types something
      private void processMessage( String message ) {
        try {
     
          // Send it to the server
          dout.writeUTF( message );
     
          // Clear out text input field
          tf.setText( "" );
        } catch( IOException ie ) { System.out.println( ie ); }
      }
     
      // Background thread runs this: show messages from other window
      public void run() {
        try {
     
          // Receive messages one-by-one, forever
          while (true) {
     
            // Get the next message
            String message = din.readUTF();
     
            // Print it to our text window
            ta.append( message+"\n" );
          }
        } catch( IOException ie ) { System.out.println( ie ); }
      }
    }
    Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.net.*;
     
    public class ClientApplet extends Applet
    {
      public void init() {
        String host = getParameter( "192.168.1.47" );
        int port = Integer.parseInt( getParameter( "5000" ) );
        setLayout( new BorderLayout() );
        add( "Center", new Client( host, port ) );
      }
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    6
    Here is the link to their training, register and log in to view.

    http://www-128.ibm.com/developerwork...avachat-i.html

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2003, 10:54 AM
  2. Replies: 5
    Last Post: 12-05-2002, 07:23 PM
  3. Replies: 1
    Last Post: 05-13-2002, 08:31 AM
  4. Replies: 0
    Last Post: 12-17-2000, 01:02 PM
  5. Combine Client Side & Server Side?
    By Jeff in forum ASP.NET
    Replies: 2
    Last Post: 04-20-2000, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center