Results 1 to 15 of 15

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

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    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:

    VB Code:
    1. import java.io.*;
    2. import java.net.*;
    3. import java.util.*;
    4.  
    5. public class Server
    6. {
    7.   // The ServerSocket we'll use for accepting new connections
    8.   private ServerSocket ss;
    9.  
    10.   // A mapping from sockets to DataOutputStreams.  This will
    11.   // help us avoid having to create a DataOutputStream each time
    12.   // we want to write to a stream.
    13.   private Hashtable outputStreams = new Hashtable();
    14.  
    15.   // Constructor and while-accept loop all in one.
    16.   public Server( int port ) throws IOException {
    17.  
    18.     // All we have to do is listen
    19.     listen( port );
    20.   }
    21.  
    22.   private void listen( int port ) throws IOException {
    23.  
    24.     // Create the ServerSocket
    25.     ss = new ServerSocket( port );
    26.  
    27.     // Tell the world we're ready to go
    28.     System.out.println( "Listening on "+ss );
    29.  
    30.     // Keep accepting connections forever
    31.     while (true) {
    32.  
    33.       // Grab the next incoming connection
    34.       Socket s = ss.accept();
    35.  
    36.       // Tell the world we've got it
    37.       System.out.println( "Connection from "+s );
    38.  
    39.       // Create a DataOutputStream for writing data to the
    40.       // other side
    41.       DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
    42.  
    43.       // Save this stream so we don't need to make it again
    44.       outputStreams.put( s, dout );
    45.  
    46.       // Create a new thread for this connection, and then forget
    47.       // about it
    48.       new ServerThread( this, s );
    49.     }
    50.   }
    51.  
    52.   // Get an enumeration of all the OutputStreams, one for each client
    53.   // connected to us
    54.   Enumeration getOutputStreams() {
    55.     return outputStreams.elements();
    56.   }
    57.  
    58.   // Send a message to all clients (utility routine)
    59.   void sendToAll( String message ) {
    60.  
    61.     // We synchronize on this because another thread might be
    62.     // calling removeConnection() and this would screw us up
    63.     // as we tried to walk through the list
    64.     synchronized( outputStreams ) {
    65.  
    66.       // For each client ...
    67.       for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {
    68.  
    69.         // ... get the output stream ...
    70.         DataOutputStream dout = (DataOutputStream)e.nextElement();
    71.  
    72.         // ... and send the message
    73.         try {
    74.           dout.writeUTF( message );
    75.         } catch( IOException ie ) { System.out.println( ie ); }
    76.       }
    77.     }
    78.   }
    79.  
    80.   // Remove a socket, and it's corresponding output stream, from our
    81.   // list.  This is usually called by a connection thread that has
    82.   // discovered that the connectin to the client is dead.
    83.   void removeConnection( Socket s ) {
    84.  
    85.     // Synchronize so we don't mess up sendToAll() while it walks
    86.     // down the list of all output streamsa
    87.     synchronized( outputStreams ) {
    88.  
    89.       // Tell the world
    90.       System.out.println( "Removing connection to "+s );
    91.  
    92.       // Remove it from our hashtable/list
    93.       outputStreams.remove( s );
    94.  
    95.       // Make sure it's closed
    96.       try {
    97.         s.close();
    98.       } catch( IOException ie ) {
    99.         System.out.println( "Error closing "+s );
    100.         ie.printStackTrace();
    101.       }
    102.     }
    103.   }
    104.  
    105.   // Main routine
    106.   // Usage: java Server <port>
    107.   static public void main( String args[] ) throws Exception {
    108.  
    109.     // Get the port # from the command line
    110.     int port = Integer.parseInt( args[0] );
    111.  
    112.     // Create a Server object, which will automatically begin
    113.     // accepting connections.
    114.     new Server( port );
    115.   }
    116. }


    VB Code:
    1. import java.io.*;
    2. import java.net.*;
    3.  
    4. public class ServerThread extends Thread
    5. {
    6.   // The Server that spawned us
    7.   private Server server;
    8.  
    9.   // The Socket connected to our client
    10.   private Socket socket;
    11.  
    12.   // Constructor.
    13.   public ServerThread( Server server, Socket socket ) {
    14.  
    15.     // Save the parameters
    16.     this.server = server;
    17.     this.socket = socket;
    18.  
    19.     // Start up the thread
    20.     start();
    21.   }
    22.  
    23.   // This runs in a separate thread when start() is called in the
    24.   // constructor.
    25.   public void run() {
    26.  
    27.     try {
    28.  
    29.       // Create a DataInputStream for communication; the client
    30.       // is using a DataOutputStream to write to us
    31.       DataInputStream din = new DataInputStream( socket.getInputStream() );
    32.  
    33.       // Over and over, forever ...
    34.       while (true) {
    35.  
    36.         // ... read the next message ...
    37.         String message = din.readUTF();
    38.  
    39.         // ... tell the world ...
    40.         System.out.println( "Sending "+message );
    41.  
    42.         // ... and have the server send it to all clients
    43.         server.sendToAll( message );
    44.       }
    45.     } catch( EOFException ie ) {
    46.  
    47.       // This doesn't need an error message
    48.     } catch( IOException ie ) {
    49.  
    50.       // This does; tell the world!
    51.       ie.printStackTrace();
    52.     } finally {
    53.  
    54.       // The connection is closed for one reason or another,
    55.       // so have the server dealing with it
    56.       server.removeConnection( socket );
    57.     }
    58.   }
    59. }

    Code for client below.
    Last edited by gjon; Jun 2nd, 2006 at 09:25 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
  •  



Click Here to Expand Forum to Full Width