Unable to establish a socket connection

S

S J Rulison

One more thing:

I'm still puzzled by this getCodeBase()getHost() method. If I'm
running the Server program from a PC named JServer and I'm loading the
Applet on a different PC, even if the applet.class file is stored on
the hard-drive of JServer, how will the getCodeBase()getHost() method
know to point to the PC named JServer? Is there something I need to
do to publish the java server application? Something that tells the
getHost() I'm JServer, a JAVA Server, and HERE I AM!

Keep in mind that JServer is nothing more than a PC running windows XP
with my java server application running on it. I don't have any type
of Windows XP Internet Server processes running on it. When I here
words like ORIGINATE or CALL BACK TO THE SERVER, the only thing that
means to me is that the java applet, Applet1.class and Applet1.html
are both stored on the hard-drive of the JServer PC. When I load
Applet1.html, I have to point the web browser to the JServer PC and I
guess that process alone could pass along the host information but if
there is something more to it than that, please let me know.

Thanks for your help.


<snippet>
public class Applet1 extends Applet
{

/*
How does the getHost() method know that I want to point to JServer as
opposed to any of the other PCs on the network?
*/

public void init() {
System.out.println("getCodeBase(): '" +
getCodeBase() + "'" );
System.out.println("getCodeBase().getHost(): '" +
getCodeBase().getHost() + "'" );

connect(getCodeBase().getHost());
}

public static void connect(String host)
{
try
{
Socket s = new Socket(host, 1427);
</snippet
 
A

Andrew Thompson

..If I type in System.out.println(getCodeBase().getHost()); in the
Applet source code, it compiles w/o any errors but when I run it I get
this runtime error message:
Check point 1
Check point 2
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)
at Applet1.<init>(Applet1.java:22)

You do not seem very experienced at debugging.

When you do not understand what is happening with the attribute
returned by a method *or series of methods*, separate them and check
what each returns after check 1a and 1b. See below.
*/
import java.applet.*;
import java.net.*;
import java.io.*;

public class Applet1 extends Applet
{
//Constructor
public Applet1()
{
{
try
{
System.out.println("Check point 1a");
System.out.println( "Code Base: '"
+ getCodeBase() + "'" );
System.out.println("Check point 1b");
System.out.println( "Host: '" +
getCodeBase().getHost() + "'" );
System.out.println("Check point 1");
Socket s = new Socket("10.44.1.250", 1427);

System.out.println("Check point 2");
System.out.println(getCodeBase().getHost() );

What does that report when delivered either off the local disk,
or alternately the server? (ShhheeeEEEeesh!)

Also, please do not post 'tab' characters to the group, do a global
replace on tabs just before posting if need be.
 
S

Steve Horsley

S said:
If I type in System.out.println(getCodeBase.getHost()); in the Applet
source code, I get this compilation error message:
Applet1:java:34: cannot find symbol symbol: variable getCodeBase


If I type in System.out.println(getCodeBase().getHost()); in the
Applet source code, it compiles w/o any errors

My mistake. Hope I didn't waste too much of your time.

Question 1:
EXACTLY how are you launchng this applet???
Are you downloading it from a web server in a browser (as we have all
assumed so far), or are you in fact opening a local file with your
browser (File->Open...)?

Question 2:
try:
System.out.println("codebase=" + getCodeBase());
System.out.println("host=" + getCodeBase().getHost());

Steve
 
S

S J Rulison

Okay. Here is the revised source code w/o the tabs and the results.


/* Program Name: Applet.java

Purpose: Demonstrate a socket connection from the APPLET side over the
network.
*/
import java.applet.*;
import java.net.*;
import java.io.*;

public class Applet1 extends Applet
{
//Constructor
public Applet1()
{
try
{

System.out.println("Check point 1a");
System.out.println( "Code Base: '"
+ getCodeBase() + "'" );
System.out.println("Check point 1b");
System.out.println( "Host: '" +
getCodeBase().getHost() + "'" );

System.out.println("Check point 1");
Socket s = new Socket("10.44.1.250", 1427);

System.out.println("Check point 2");
System.out.println(getCodeBase().getHost() );

System.out.println("Check point 3");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("Socket connection unsuccessful.");
}//End of catch block.
}//Constructor

public void init(){}
}//End of class Applet1.


Check point 1a
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)
at Applet1.<init>(Applet1.java:19)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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)
 
S

S J Rulison

One thing I forgot to mention in the last post. The results were the
same when I loaded the applet from a remote (seperate) PC or when I
loaded it from the JServer PC. Both returned a Nullpointer Exception.
 
S

S J Rulison

Question 1:
EXACTLY how are you launchng this applet???
Are you downloading it from a web server in a browser (as we have all
assumed so far), or are you in fact opening a local file with your
browser (File->Open...)?

I am opening a local file from a browser. As I stated in an earlier
post, I am simply running a java server application on a windows XP
workstation. It is not a web server


I didn't waste much time with the getCodeBase.getHost() thing.
 
A

Andrew Thompson

On 3 Dec 2004 14:07:43 -0800, S J Rulison wrote:

I see that you are posting through Google ..S J. (what do we call you?)

Can I ask you to please quote a *little* of what you are replying to,
to give a context to your replies. Look carefully at the way that
the others in this thread are replying to you. A little of what you said,
then a comment in reply.
Okay. Here is the revised source code w/o the tabs and the results.

Okay, now I am a little confused. Your results tell us a little
more than we knew, but they are *not* what I was expecting.

....
System.out.println("Check point 1a");
System.out.println( "Code Base: '"
+ getCodeBase() + "'" );

Your Stacktrace is indicating an error in this line *as well*.

That was not the behaviour I was observing here.
(I got a file path for codebase and '' for host.)
...
Check point 1a
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)

....hmmmm.

What is your exact (simple) HTML in the web page?
Either post it vebatim or link to (either it or a copy
of) it on the web.
 
S

S J Rulison

Andrew Thompson said:
On 3 Dec 2004 14:07:43 -0800, S J Rulison wrote:

I see that you are posting through Google ..S J. (what do we call you?)

My full name is Steve John Rulison. I go by Steve.

Can I ask you to please quote a *little* of what you are replying to,
to give a context to your replies. Look carefully at the way that
the others in this thread are replying to you. A little of what you said,
then a comment in reply.

The quote in my last post (post 26) came from Steve Horsley (post
22).

Let me reiterate that I am NOT repeating NOT running the java server
application on a WEB SERVER. The java server application is running
on a standard Windows XP Professional workstation that resides on our
LAN. When I load the Applet1.html into the web browser, I type
\\Jserver\JavaServer\Applet1.html into the URL address box. The
Applet1.html file resides in a shared directory called JavaServer.
Okay, now I am a little confused. Your results tell us a little
more than we knew, but they are *not* what I was expecting.

...

Your Stacktrace is indicating an error in this line *as well*.

That was not the behaviour I was observing here.
(I got a file path for codebase and '' for host.)
..

...hmmmm.

What is your exact (simple) HTML in the web page?
Either post it vebatim or link to (either it or a copy
of) it on the web.

Here is a copy of the html. As you can see it's pretty basic. This
html file resides in the same directory as the Applet1.class file on
the JServer workstation.

<HTML>

<HEAD>
<TITLE>Applet1</TITLE>
<CENTER><H1>Test Java Applet</H1></CENTER>
</HEAD>

<BODY>
<CENTER>
<APPLET CODE=Applet1.class WIDTH=600 HEIGHT=450></APPLET>
</CENTER>
</BODY>

</HTML>
 
S

Steve Horsley

I think I know the answer. Move the initialisation code from
the constructor to the init() method instead. That should fix
your null exception in getCodeBase(). I get the same exception
if I put the code in the constructor.

I think your next problem will be that your host (from
getCodeBase().getHost() will be "", because you are not loading
from a web server. (I'm using Firefox on Linux, so I can't be
sure your results will be identical there).

The security manager only allows the applet to connect to the
web server that the applet was downloaded from, and in your
case, the applet wasn't downloaded from a webserver, it came
from the "local" filesystem. So I guess your next step will
be to fire up a web server.

Steve
 
A

Andrew Thompson

I think I know the answer. Move the initialisation code from
the constructor to the init() method instead.

Ugggh... (slaps forehead). Of course!

That's exactly what I did before I first viewed the
compiled code example in a browser.
..That should fix
your null exception in getCodeBase(). I get the same exception
if I put the code in the constructor.

It would also explain the differences between Steve
(Rulison's) output and my own.

(snip rest that looks right from here)
 
S

srulison

Andrew said:
Ugggh... (slaps forehead). Of course!

That's exactly what I did before I first viewed the
compiled code example in a browser.


It would also explain the differences between Steve
(Rulison's) output and my own.


Okay Andrew & Steve here is the revised code and results after moving
the getCodeBase().getHost() statements to the init method. As you can
see that took care of the NullPointerException but I'm still not sure
what to make of the output from the getHost() method.

public class Applet1 extends Applet
{
//Constructor
public Applet1()
{}//Constructor

public void init()
{
try
{

System.out.println("Check point 1a");
System.out.println( "Code Base: '"
+ getCodeBase() + "'" );
System.out.println("Check point 1b");
System.out.println( "Host: '" +
getCodeBase().getHost() + "'" );

System.out.println("Check point 1");
Socket s = new Socket("10.44.1.250", 1427);

System.out.println("Check point 2");
System.out.println(getCodeBase().getHost() );

System.out.println("Check point 3");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("Socket connection unsuccessful.");
}//End of catch block.
}//End of init method
}//End of class Applet1.

Results:

Check point 1a
Code Base: 'file://JServer/JavaServer/'
Check point 1b
Host: ''
Check point 1
Check point 2
Check point 3
 
S

srulison

Okay Andrew & Steve here is the revised code and results after moving
the getCodeBase().getHost() statements to the init method. As you can
see that took care of the NullPointerException but I'm still not sure
what to make of the output from the getHost() method.

public class Applet1 extends Applet
{
//Constructor
public Applet1()
{}//Constructor

public void init()
{
try
{

System.out.println("Check point 1a");
System.out.println( "Code Base: '"
+ getCodeBase() + "'" );
System.out.println("Check point 1b");
System.out.println( "Host: '" +
getCodeBase().getHost() + "'" );

System.out.println("Check point 1");
Socket s = new Socket("10.44.1.250", 1427);

System.out.println("Check point 2");
System.out.println(getCodeBase().getHost() );

System.out.println("Check point 3");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("Socket connection unsuccessful.");
}//End of catch block.
}//End of init method
}//End of class Applet1.

Results:

Check point 1a
Code Base: 'file://JServer/JavaServer/'
Check point 1b
Host: ''
Check point 1
Check point 2
Check point 3


I forgot to include the results when applet was loaded on the JServer
workstation. Not a whole lot of difference.

Check point 1a
Code Base: 'file:/C:/Java%20Server/'
Check point 1b
Host: ''
Check point 1
Check point 2

Check point 3
 
S

Steve Horsley

I forgot to include the results when applet was loaded on the JServer
workstation. Not a whole lot of difference.

Check point 1a
Code Base: 'file:/C:/Java%20Server/'
Check point 1b
Host: ''
Check point 1
Check point 2

Check point 3

So you need to fire up a webserver to server your applet.

If you insist on running the applet like a local app, why not write
a local app, and forget this applet and security manager stuff altogether?

Steve
 
S

srulison

Steve said:
So you need to fire up a webserver to server your applet.

If you insist on running the applet like a local app, why not write
a local app, and forget this applet and security manager stuff altogether?

Because I am eventually planning on deploying an actual java applet to
the web where the general public will be able to view the status of
their case they have on file with the agency I work for.

It appears that I should have focused on setting up a web server first
before trying to learn the applet development side but it appeared, at
first anyway, that I would be able to learn all of the java side before
I tried to conquer setting up a web-server. The only problem was I
wanted to get other members of our staff to test the applet from their
PCs and make any suggestions they had. That's when I started getting
the security exceptions, which led me to post this topic to this news
group, etc. . . . It now appears that I should have started off with
setting the web server up first before trying to build the applet.

I know this has been a real pain in the a%*& to you guys but it has
been extremely helpful to me. Now, I know what I need to do to go
forward with this project. I fully expect to be running into
roadblocks like this from time to time before I can call myself a web
site developer but I believe it will be well worth it in the end. I
want to thank both of you for the time and effort you have both put in
one this. It has been helpful.

Steve R.
 
S

Sudsy

altogether?


Because I am eventually planning on deploying an actual java applet to
the web where the general public will be able to view the status of
their case they have on file with the agency I work for.
<snip>

Sounds like a classic case of "cart before the horse". By that I mean
it seems as though you've already decided on the architecture (applet)
before investigating the alternatives.
Sounds like a typical Struts app to me. Perhaps that's why Struts was
created in the first place?...
LCD (Lowest Common Denominator) should apply in the web world. Utilize
the core functions of HTML on the client and put all the processing
power on the server.
(But save yourself needless trips to the back-end via JavaScript
validation, where available.)
 
S

srulison

Update:
For what its worth, I setup a Windows 2000 web server and moved the
java server and applet onto it. Everything is working great on it now.
The getCodeBase().getHost() methods work so much better when the
applet is running on an actual web server. Thanks again for your help.
Steve R.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top