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:
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(); ) {
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
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.
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.
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.
Re: How to make the client connect to the server at the command prompt?
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