Results 1 to 15 of 15

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

  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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    The ServerThread class isn't the client side program.
    This entire program just waits for a connection on the specified port. and when a connection is established it prints incoming streams as UTF
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

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

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

    CLIENT:
    VB Code:
    1. import java.applet.*;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.io.*;
    5. import java.net.*;
    6.  
    7. public class Client extends Panel implements Runnable
    8. {
    9.   // Components for the visual display of the chat windows
    10.   private TextField tf = new TextField();
    11.   private TextArea ta = new TextArea();
    12.  
    13.   // The socket connecting us to the server
    14.   private Socket socket;
    15.  
    16.   // The streams we communicate to the server; these come
    17.   // from the socket
    18.   private DataOutputStream dout;
    19.   private DataInputStream din;
    20.  
    21.   // Constructor
    22.   public Client( String host, int port ) {
    23.  
    24.     // Set up the screen
    25.     setLayout( new BorderLayout() );
    26.     add( "North", tf );
    27.     add( "Center", ta );
    28.  
    29.     // We want to receive messages when someone types a line
    30.     // and hits return, using an anonymous class as
    31.     // a callback
    32.     tf.addActionListener( new ActionListener() {
    33.       public void actionPerformed( ActionEvent e ) {
    34.         processMessage( e.getActionCommand() );
    35.       }
    36.     } );
    37.  
    38.     // Connect to the server
    39.     try {
    40.  
    41.       // Initiate the connection
    42.       socket = new Socket( host, port );
    43.  
    44.       // We got a connection!  Tell the world
    45.       System.out.println( "connected to "+socket );
    46.  
    47.       // Let's grab the streams and create DataInput/Output streams
    48.       // from them
    49.       din = new DataInputStream( socket.getInputStream() );
    50.       dout = new DataOutputStream( socket.getOutputStream() );
    51.  
    52.       // Start a background thread for receiving messages
    53.       new Thread( this ).start();
    54.     } catch( IOException ie ) { System.out.println( ie ); }
    55.   }
    56.  
    57.   // Gets called when the user types something
    58.   private void processMessage( String message ) {
    59.     try {
    60.  
    61.       // Send it to the server
    62.       dout.writeUTF( message );
    63.  
    64.       // Clear out text input field
    65.       tf.setText( "" );
    66.     } catch( IOException ie ) { System.out.println( ie ); }
    67.   }
    68.  
    69.   // Background thread runs this: show messages from other window
    70.   public void run() {
    71.     try {
    72.  
    73.       // Receive messages one-by-one, forever
    74.       while (true) {
    75.  
    76.         // Get the next message
    77.         String message = din.readUTF();
    78.  
    79.         // Print it to our text window
    80.         ta.append( message+"\n" );
    81.       }
    82.     } catch( IOException ie ) { System.out.println( ie ); }
    83.   }
    84. }

    VB Code:
    1. import java.applet.*;
    2. import java.awt.*;
    3. import java.io.*;
    4. import java.net.*;
    5.  
    6. public class ClientApplet extends Applet
    7. {
    8.   public void init() {
    9.     String host = getParameter( "192.168.1.47" );
    10.     int port = Integer.parseInt( getParameter( "5000" ) );
    11.     setLayout( new BorderLayout() );
    12.     add( "Center", new Client( host, port ) );
    13.   }
    14. }

    Sorry about that. Now when I run an html file with this applet I just get the x in the corner.
    Thanks for looking.

  4. #4

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

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

    Here is the link to their training, register and log in to view.

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

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    Thanks, but almost all java books have the IM sample program as Applet and Application
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

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

    The client program has the main method as non-static. Add the static keyword and it will work.

    I had exactly the same problem a few days ago due to a typo.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    Quote Originally Posted by CornedBee
    The client program has the main method as non-static. Add the static keyword and it will work.

    I had exactly the same problem a few days ago due to a typo.
    it says "static public" and java has no problems with that...
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

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

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

    I am going to try to see if I can't get the client to work as an applet. The client connects to the server, but I don't have a way to send any text to it or see anything happening in a command window. I've been learning how to use applets, but not applications, didn't realize the client was just an app with no gui.

  9. #9

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

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

    Ok, I see the clientapplet is the applet.
    Why did the writer use layout when theres nothing to be layed out?
    setLayout( new BorderLayout() );

    I should add a panel and put a text area on it, then I think I need to start working with streamreaders. Ok, that's the next thing I'll have to do some searching for on the net; some examples of clients actually displaying the text to/from the server.

  10. #10
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    the next 2 lines add a "text field" and a "text area"
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

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

    Quote Originally Posted by ComputerJy
    it says "static public" and java has no problems with that...
    Oops, confused run() and main(), not client and server.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

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

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

    I don't understand, I don't get how it can use setlayout, and be adding textfields and text areas, but not be a gui? Err maybe I should say, I don't get anything on my applet. If I run it in dos, it just gives me a message about being connected but doesn't let a prompt come up for me to do anything else.
    I get a red x when I try to run it as an applet when using the clientapplet class. The server shows it gets connected when i use the command prompt but nothing when i run the clientapplet as an applet.
    I'm gonna keep plugging away at it. Maybe it's not finding the class for some reason. I am using code and codebase... oooooh wait.... nah, that doesn't matter, as long as its in the same folder, all i need is code, not codebase.

  13. #13
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    Quote Originally Posted by gjon
    I don't understand, I don't get how it can use setlayout, and be adding textfields and text areas, but not be a gui? Err maybe I should say, I don't get anything on my applet. If I run it in dos, it just gives me a message about being connected but doesn't let a prompt come up for me to do anything else.
    I get a red x when I try to run it as an applet when using the clientapplet class. The server shows it gets connected when i use the command prompt but nothing when i run the clientapplet as an applet.
    I'm gonna keep plugging away at it. Maybe it's not finding the class for some reason. I am using code and codebase... oooooh wait.... nah, that doesn't matter, as long as its in the same folder, all i need is code, not codebase.
    I'm sorry, when You said only an "x" appears in the corent I thought, there is no GUI Compoenents in your Applet, but after reviewing your code (I deleted that post)

    I'll look in my library, I think I have a client-server application sample
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  14. #14
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    You might find this usefull.
    it's a Framed Application, you can work on making an Applet out of it
    Attached Files Attached Files
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  15. #15

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

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

    Thank you so much, I will defenitely play with this today.

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