Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a java server (serving applets) 1

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
I am trying to modify the following code:
URL:
applet:
Server:

In the server code I have changed all 1.0 to 1.1 and before the lines:
// Check if the command was valid
if (!str.endsWith(" HTTP/1.1")) {
I put the following code segment:
System.out.println("got a string value " + str);
if(str.endsWith("/chat HTTP/1.1")){
System.out.println("found a match, now get the html code to the browser");
PrintStream outHTML = new PrintStream
(new BufferedOutputStream(s.getOutputStream()), true);
outHTML.println(&quot;<applet code=\&quot;SimpleChatApplet.class\&quot; width=350 height=225 alt=\&quot;\&quot;><param name=port value=80></applet>&quot;);
outHTML.println(&quot;\r&quot;);
outHTML.flush();
in.close();
s.close();
System.out.println(&quot;Code passed to the browser.&quot;);
continue;
}
if(str.endsWith(&quot;/SimpleChatApplet.class HTTP/1.1&quot;)){
System.out.println(&quot;Now to pass the the applet to the Browser&quot;);
FileInputStream fleApplet = new FileInputStream(new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\SimpleChatApplet.class&quot;));
BufferedOutputStream outApplet = new BufferedOutputStream(s.getOutputStream());
int b = fleApplet.read();
while(b!=-1){
outApplet.write(b);
b = fleApplet.read();
}
System.out.println(&quot;End of the loop&quot;);
outApplet.flush();
System.out.println(&quot;flush&quot;);
fleApplet.close();
System.out.println(&quot;file close&quot;);
in.close();
System.out.println(&quot;in close&quot;);
s.close();
System.out.println(&quot;Applet passed to the browser.&quot;);
continue;
}
Changed the port values to 80 so the server will serve as a web server.
Reason for this is that &quot;burrowing through firewalls&quot; is not realy burrowing through because all ports on the proxy are closed so using 6006 is not an option. Using the only available port (80) disables the web server since the chatserver and the webserver cannot both be using port 80 so the chatserver has to function as a web server.
The only thing it needs to do is pass the html code (works) and the pass the applet (gives an error client side):
(last 2 lines)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


So the client browser request &quot;http:servername/chat&quot;, the chatserver &quot;if(str.endsWith(&quot;/chat HTTP/1.1&quot;))&quot; statement is true and the chatserver prints a view lines to the output stream.
These lines are html code telling the browser to create an applet then the java plugin takes over and the java plugin request &quot;servername/SimpleChatApplet.class&quot; the chatserver picks this up (statement &quot;if(str.endsWith(&quot;/SimpleChatApplet.class HTTP/1.1&quot;))&quot; is true), opens the class file on the server and passes this file in the output stream to the client. Then the java plugin requests the applet again, chatserver does the same. Finally the plugin gives up and generates the error or crashes.

I like this application because it shows how the server pushes an update to the client in a web application (using http). This is the only example I found where the client did not submit every couple of seconds to see if there is new information but the server tells the client when there is new information. Don't need a chat application I need this to run on port 80.

Can anybody help me with this?
Thanx



Greetings, Harm Meijer
 
I gues nowhere, the code is as it is.
The chat server listens on port 80:
try {
ss = new ServerSocket(port);
} catch (IOException e) {
System.out.println(&quot;Could not listen on port: &quot; + port + &quot;, &quot; + e);
System.exit(1);
}

// Allocate a vector for all open connections to clients.
Vector connections = new Vector();
System.out.println(&quot;Ready...&quot;);

// Loop forever, accepting connections from clients
while (true) {

// Accept a new connection
Socket s = null;

When I do a request (with IE 6) the statement:
if(str.endsWith(&quot;/chat HTTP/1.1&quot;)){
is true and I print some html code to the outputstream of s (socket):
PrintStream outHTML = new PrintStream
(new BufferedOutputStream(s.getOutputStream()), true);
outHTML.println(&quot;<applet code=\&quot;SimpleChatApplet.class\&quot; width=350 height=225 alt=\&quot;\&quot;><param name=port value=80></applet>&quot;);
outHTML.println(&quot;\r&quot;);
outHTML.flush();
in.close();
s.close();

This works, but when the next request comes in (/SimpleChatApplet.class HTTP/1.1) the chatserver has to serve the applet:
if(str.endsWith(&quot;/SimpleChatApplet.class HTTP/1.1&quot;)){
System.out.println(&quot;Now to pass the the applet to the Browser&quot;);
FileInputStream fleApplet = new FileInputStream(new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\SimpleChatApplet.class&quot;));
BufferedOutputStream outApplet = new BufferedOutputStream(s.getOutputStream());
int b = fleApplet.read();
while(b!=-1){
outApplet.write(b);
b = fleApplet.read();
}
System.out.println(&quot;End of the loop&quot;);
outApplet.flush();
System.out.println(&quot;flush&quot;);
fleApplet.close();
System.out.println(&quot;file close&quot;);
in.close();
System.out.println(&quot;in close&quot;);
s.close();
System.out.println(&quot;Applet passed to the browser.&quot;);
continue;
}

If I need to create http headers I don't know how to create them, sending the info to the browser is no problem but sending the applet to the browser plugin does not work.



Greetings, Harm Meijer
 
You were correct I needed the response headers. And I needed to serve the helper class as well:

if(str.endsWith(&quot;/SimpleChatApplet.class HTTP/1.1&quot;)){
System.out.println(&quot;Now to pass the the applet to the Browser&quot;);
FileInputStream fleApplet = new FileInputStream(new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\SimpleChatApplet.class&quot;));
BufferedOutputStream outApplet = new BufferedOutputStream(s.getOutputStream());
byte[] btHeader = new String(&quot;HTTP/1.1 200 Java Echo Service\rContent-Type:application/octet-stream;Content-Length:&quot; + (new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\SimpleChatApplet.class&quot;)).length() + &quot;\r\n\r\n&quot;).getBytes();
System.out.print(&quot;response header: &quot; + &quot;HTTP/1.1 200 Java Echo Service\rContent-Type:application/octet-stream;Content-Length:&quot; + (new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\SimpleChatApplet.class&quot;)).length() + &quot;\r\n\r\n&quot;);
outApplet.write(btHeader);
System.out.println(&quot;starting to write the applet&quot;);
int b = fleApplet.read();
while(b!=-1){
outApplet.write(b);
//System.out.print((char) b); //only do this if you do not pass a binary file
b = fleApplet.read();
}
System.out.println(&quot;End of the loop&quot;);
outApplet.flush();
System.out.println(&quot;flush&quot;);
fleApplet.close();
System.out.println(&quot;file close&quot;);
in.close();
System.out.println(&quot;in close&quot;);
s.close();
System.out.println(&quot;Applet passed to the browser.&quot;);
continue;
}
if(str.endsWith(&quot;/ServerConnection.class HTTP/1.1&quot;)){
System.out.println(&quot;Now to pass the the helper class the Browser&quot;);
FileInputStream fleApplet = new FileInputStream(new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\ServerConnection.class&quot;));
BufferedOutputStream outApplet = new BufferedOutputStream(s.getOutputStream());
byte[] btHeader = new String(&quot;HTTP/1.1 200 Java Echo Service\rContent-Type:application/octet-stream;Content-Length:&quot; + (new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\ServerConnection.class&quot;)).length() + &quot;\r\n\r\n&quot;).getBytes();
System.out.print(&quot;response header: &quot; + &quot;HTTP/1.1 200 Java Echo Service\rContent-Type:application/octet-stream;Content-Length:&quot; + (new File(&quot;C:\\j2sdk1.4.0_03\\bin\\chat\\ServerConnection.class&quot;)).length() + &quot;\r\n\r\n&quot;);
outApplet.write(btHeader);
System.out.println(&quot;starting to write the helper class&quot;);
int b = fleApplet.read();
while(b!=-1){
outApplet.write(b);
//System.out.print((char) b); //only do this if you do not pass a binary file
b = fleApplet.read();
}
System.out.println(&quot;End of the loop&quot;);
outApplet.flush();
System.out.println(&quot;flush&quot;);
fleApplet.close();
System.out.println(&quot;file close&quot;);
in.close();
System.out.println(&quot;in close&quot;);
s.close();
System.out.println(&quot;Helper class passed to the browser.&quot;);
continue;
}


Still don't have it working 100% but at least the applet starts.



Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top