If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Threaded View

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