Applet Issues!!

N

niallmulhare

Hey, Im Attempting to create an applet that will open a connection to my
application server, the application server WILL be on the same machine
that the applet will be loaded from, i.e. the app server is on the same
machine as the web server. here is my code (sorry for the bad standards)

public class ClientConference extends Applet {

private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;

//----------------------------------------------------------------------//

public ClientConference( String host ) {
chatServer = host;
ClientConference application;
application = new ClientConference( "149.153.130.2" );
application.runClient();
}


//----------------------------------------------------------------------//
private void runClient() {

try {
connectToServer(); // Step 1: Create a Socket to make connection
getStreams(); // Step 2: Get the input and output streams
processConnection(); // Step 3: Process connection
}

// server closed connection
catch ( EOFException eofException ) {
System.err.println( "Client terminated connection" );
}

// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}

finally {
closeConnection(); // Step 4: Close connection
}

} // end method runClient


//----------------------------------------------------------------------//

private void connectToServer() throws IOException{
client = new Socket( InetAddress.getByName(chatServer), 8000 );
}


//----------------------------------------------------------------------//

private void getStreams() throws IOException{
output = new ObjectOutputStream( client.getOutputStream() );
output.flush();

input = new ObjectInputStream( client.getInputStream() );

output.writeObject( "Client Connected" );
output.flush();
}


//----------------------------------------------------------------------//

private void processConnection() throws IOException{

do { // process messages sent from server

// read message and display it
try {
message = ( String ) input.readObject();
}

catch ( ClassNotFoundException classNotFoundException ) { }

} while ( !message.equals( "TERMINATE" ) );

}


//----------------------------------------------------------------------//

private void closeConnection(){

try {
output.close();
input.close();
client.close();
}
catch( IOException ioException ) {
ioException.printStackTrace();
}
}


//----------------------------------------------------------------------//

private void sendData( String message )
{
// send object to server
try {
output.writeObject( "CLIENT>>> " + message );
output.flush();
}

// process problems sending object
catch ( IOException ioException ) { }
}

//----------------------------------------------------------------------//



} // end class Client

If anyone could even hasard a guess at the issue I would appreceate it,
also here is output from the java console if its anyhelp.

load: ClientConference.class can't be instantiated.
java.lang.InstantiationException: ClientConference
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


Thanks in advance I really appreceate any help
Niall
 
K

klynn47

I think the problem is with the way you're trying to create an instance
of ClientConference. Because it is an applet, an instance of it will be
created and loaded by the browser. Try replacing your constructor with
an init method. You might also try creating a seperate thread to
contact the server.
 
T

Thomas Fritsch

niallmulhare said:
Hey, Im Attempting to create an applet that will open a connection to my
application server, the application server WILL be on the same machine
that the applet will be loaded from, i.e. the app server is on the same
machine as the web server. here is my code (sorry for the bad standards)

public class ClientConference extends Applet {

private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;

//----------------------------------------------------------------------//

public ClientConference( String host ) {
chatServer = host;
ClientConference application;
application = new ClientConference( "149.153.130.2" );
application.runClient();
}
[...]

//----------------------------------------------------------------------//
} // end class Client

If anyone could even hasard a guess at the issue I would appreceate it,
also here is output from the java console if its anyhelp.

load: ClientConference.class can't be instantiated.
java.lang.InstantiationException: ClientConference
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
Class.newInstance() --which is called by your browser/appletviewer--
throws a InstantiationException because your applet has no
non-argument-constructor. You definitely need that constructor:

public ClientConference()
{
// do nothing here
}
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


Thanks in advance I really appreceate any help
Niall

Furthermore you should override the applet's init() and destroy()
methods, and may be start() and stop(), too.
See http://java.sun.com/j2se/1.4.2/docs/api/java/applet/Applet.html for
information when your browser calls those 4 methods.
Then you can split up the code you had in your original constructor, and
move its parts into those methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top