PDA

Click to See Complete Forum and Search --> : How to make the client connect to the server at the command prompt?


gjon
Jun 2nd, 2006, 12:30 AM
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:

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 );
}
}



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 );
}
}
}


Code for client below.

ComputerJy
Jun 2nd, 2006, 04:13 AM
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

gjon
Jun 2nd, 2006, 09:27 AM
CLIENT:
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 ); }
}
}


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 ) );
}
}


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

gjon
Jun 2nd, 2006, 07:59 PM
Here is the link to their training, register and log in to view.

http://www-128.ibm.com/developerworks/edu/j-dw-javachat-i.html

ComputerJy
Jun 3rd, 2006, 04:48 AM
Thanks, but almost all java books have the IM sample program as Applet and Application

CornedBee
Jun 3rd, 2006, 05:41 PM
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.

ComputerJy
Jun 3rd, 2006, 06:35 PM
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...

gjon
Jun 3rd, 2006, 06:47 PM
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.

gjon
Jun 3rd, 2006, 09:05 PM
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.

ComputerJy
Jun 4th, 2006, 04:36 AM
the next 2 lines add a "text field" and a "text area"

CornedBee
Jun 4th, 2006, 04:55 AM
it says "static public" and java has no problems with that...
Oops, confused run() and main(), not client and server.

gjon
Jun 4th, 2006, 08:30 AM
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.

ComputerJy
Jun 4th, 2006, 08:47 AM
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

ComputerJy
Jun 4th, 2006, 08:52 AM
You might find this usefull.
it's a Framed Application, you can work on making an Applet out of it

gjon
Jun 4th, 2006, 09:10 AM
Thank you so much, I will defenitely play with this today.