Home arrow static arrow Java Programming [Archive] - Server - Applett program?!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Server - Applett program?!
2 Duke Stars available
This topic has 6 replies on 1 page.

Posts:61
Registered: 8/20/01
Server - Applett program?!   
Jul 28, 2004 8:46 AM



 
Well, I tried to programm a little server programm (client later)...
It opens up a java.awt window (success :) and opens up port 3000 and is listening...
But I have not understand what to do now - (the lines down there are copied with the date).
Any help pls :)

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Haupt extends Frame implements ActionListener
{
MenuItem menuFileExit = null;
MenuItem menuFileBlah = null;
TextField Alpha = new TextField("haloo");
TextField leer = new TextField();

private Socket client = null;
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
private ServerSocket dateServer;
Versuchen a= new Versuchen();

public class Versuchen extends Thread
{
public void Versuche()
{
try
{
//opens up a Port - 3000
dateServer = new ServerSocket(3000);
Alpha.setText(Alpha.getText() +"\n" + ("Server listening on port 3000."));
a.start();
}
catch(Exception e)
{
Alpha.setText(e.toString());
}
}

public void run()
{
Alpha.setText(Alpha.getText() + "\n" + "Waiting for connections.");
while(true)
{
try
{

Socket client = dateServer.accept();
Alpha.setText(("Accepted a connection from: "+
client.getInetAddress()));

} catch(Exception e) {}
}
}

public void Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch(Exception e1) {
try {
client.close();
}catch(Exception e) {
Alpha.setText(e.getMessage());
}
return;
}

}

public void Schliessen() {
try {
oos.writeObject(new Date());
oos.flush();
// close streams and connections
ois.close();
oos.close();
client.close();
} catch(Exception e) {}
}

}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Exit")
{
try{
client.close();
}
catch(Exception de){}
System.exit(0);
}
if(e.getActionCommand()=="Status abrufen")
{
Alpha.setText("Status kann noch nicht abgerufen werden");
}
if(e.getActionCommand()=="Listen to Port")
{
Alpha.setText("Versuch wird gestartet");

a.Versuche();
}
}

public Haupt()
{

super("ServerApplication");
super.setBackground(new Color(50,50,50));
MenuBar menuBar = new MenuBar( );
Menu menuFile = new Menu( );
MenuItem menuFileExit = new MenuItem( );
MenuItem menuFileBlah = new MenuItem( );
menuFile.setLabel("File");
menuFileBlah.setLabel("Status abrufen");
menuFileExit.setLabel("Exit");
Menu menuFile2 = new Menu( );
menuFile2.setLabel("Edit");
menuFile.add(menuFileBlah);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile2);
setMenuBar(menuBar);

menuFileExit.addActionListener(this);
menuFileBlah.addActionListener(this);
Button Reading = new Button("Listen to Port");
Reading.setSize(100,30);
Reading.setLocation(200,200);
Reading.addActionListener(this);


Alpha.setSize(480,55);
Alpha.setBackground(new Color(130,130,130));
Alpha.setLocation(10,55);
super.add(Alpha);
super.add(Reading);
super.add(leer);

}

public static void main(String[] args)
{
Haupt Fenster=new Haupt();
Fenster.setSize(500,500);
Fenster.show();

Fenster.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{

System.exit(0);
}
});
}

}

 

Posts:24,036
Registered: 2/3/03
Re: Server - Applett program?!   
Jul 28, 2004 8:50 AM (reply 1 of 6)



 
Well, I tried to programm a little server programm
(client later)...
It opens up a java.awt window (success :) and opens up
port 3000 and is listening...
But I have not understand what to do now - (the lines
down there are copied with the date).
Any help pls :)

What do you mean you don't know what to do? It's your program - what do you want it to do?

When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read and prevents accidental markup from array indices like [i].

[url=http://www-106.ibm.com/developerworks/edu/j-dw-javachat-i.html]Building a Java chat server[/url]
 

Posts:61
Registered: 8/20/01
Re: Server - Applett program?!   
Jul 28, 2004 8:56 AM (reply 2 of 6)



 
Sry, didnt know that ^- ^

I want the programm to listen to port 3000 - well it does :)
but I also want to write a Client which is shown, if you go to port 3000 and can communicate to the server.(applett)
I have written the server so far - as you can see, but I need help, that an Applett will pop up if you connect via IE to that port and if the code is correct still now.
Thx ;)
 

Posts:24,036
Registered: 2/3/03
Re: Server - Applett program?!   
Jul 28, 2004 9:00 AM (reply 3 of 6)



 
I have written the server so far - as you can see, but
I need help, that an Applett will pop up if you
connect via IE to that port and if the code is correct
still now.

Java Web Start may be an alternative, but... I'd think you'd have to have the server respond to that request with the proper protocol, sending the html with the appropriate applet tags to load your applet.
 

Posts:61
Registered: 8/20/01
Re: Server - Applett program?!   
Jul 28, 2004 9:04 AM (reply 4 of 6)



 
And which protocoll will be the right one for iE?
I'll tried the programm - IE tries to connect (and finds the port 3000) - but I'm not sure, that

a.) my programm sends anything to the IE
b.) what I should send to IE, that it looks like a html-code for it.
 

Posts:24,036
Registered: 2/3/03
Re: Server - Applett program?!   
Jul 28, 2004 9:19 AM (reply 5 of 6)



 
And which protocoll will be the right one for iE?

HTTP.

I'll tried the programm - IE tries to connect (and
finds the port 3000) - but I'm not sure, that

a.) my programm sends anything to the IE
b.) what I should send to IE, that it looks like a
html-code for it.

Take a look at this: http://tjws.sourceforge.net/

You're essentially asking how to build a tiny web server. The example (and there are other for which you can Google) may give you some ideas.
 

Posts:61
Registered: 8/20/01
Re: Server - Applett program?!   
Jul 28, 2004 9:31 AM (reply 6 of 6)



 
Well - I just edited it, and it works quite ok - but it shows in IE nothing real good:

��w Hallo

- it should only say "Hallo" :)

oos = new ObjectOutputStream(client.getOutputStream());
oos.writeChars("Hallo");
oos.flush();

any idea?!
 
This topic has 6 replies on 1 page.