Also hat endlich alles geklapt.. hatte die tuturial von java.sun schon gefunden. Ich frag mich nur wie man es macht das da steht "von einer vertrauenswürdigen quelle :)" aber das is nebensache. Ich hab das script hinbekommen. Der user startet das applet und das applet wartet auf daten. Ich kann sie per PHP, c++ einfach über normale Socket verbindung ansprechen. Aber ich liege vor dem problem das wenn der user den Browser Minimiert oder ein andere fenster im fullscreen aufmacht abeitet das applet nicht, und wenn er es wieder öffnet wird der server neugestartet aber der allte ist ja noch offen (dies trit nur beim Mozilla auf). WIe kann ich es denn machen das das applet immer leuft solange das fenster nicht endgültig geschlossen wird. Hier mal der code:
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
public class TCPApplet extends Applet
{
// applet parameters names
private static final String PARAM_PORT = "listening_port";
private static final String PARAM_TIMEOUT = "socket_timeout";
private static final String PARAM_IP = "listening_ip";
private static final String PARAM_ID = "ID";
// applet parameters
private String listenPort;
private String listenIp;
private String socketID;
private int socketTimeOut;
private class PortListnerThread implements Runnable
{
public void run()
{
ServerSocket socket = null;
Socket connectionSocket = null;
String tmp = null;
String tmpid;
Graphics g = getGraphics();
try
{
socket = new ServerSocket(Integer.parseInt(listenPort));
g.drawString("Waiting for data...", 20, 60);
socket.setSoTimeout(0);
while (true)
{
connectionSocket = socket.accept();
BufferedReader incoming = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
tmp = incoming.readLine();
tmpid = tmp.substring(0,8);
g.setColor(Color.white);
g.fillRect(20,70,200,50);
g.setColor(Color.black);
g.drawString("Daten: "+tmp.substring(8), 20, 80);
g.drawString("SocketID: "+tmpid, 20, 100);
g.drawString("Remot: "+connectionSocket.getInetAddress(), 20, 120);
}
}
catch (Exception exc)
{
g.drawString("ERROR #1: "+exc.getMessage(), 20, 120);
}
finally
{
if (connectionSocket != null)
{
try
{
connectionSocket.close();
}
catch (Exception exc)
{
}
}
if (socket != null)
{
try
{
socket.close();
}
catch (Exception exc)
{
}
}
}
}
}
public void init()
{
listenPort = getParameter(PARAM_PORT);
listenIp = getParameter(PARAM_IP);
socketID = getParameter(PARAM_ID);
try
{
socketTimeOut = Integer.parseInt(getParameter(PARAM_TIMEOUT));
}
catch (Exception exc)
{
// default socket timeout: 10 seconds
socketTimeOut = 15000;
}
// lookup local ip address and add it to the script url
String ipAddress = null;
if (listenIp == null)
{
try
{
// this will only work if the applet is signed! Else 127.0.0.1 will be returned
// furthermore, on networks, the internal network address will be returned
InetAddress ia = InetAddress.getLocalHost();
ipAddress = ia.getHostAddress();
}
catch (Exception exc)
{
// unable to determine local ip address
ipAddress = "127.0.0.1";
}
}
else
{
ipAddress = listenIp;
}
}
public void paint(Graphics g)
{
g.drawString("Connection to "+listenIp+" on Port: "+listenPort, 20, 20);
g.drawString("Connection timeout is: "+(socketTimeOut/1000)+"sek", 20, 40);
checkPort();
}
private void checkPort()
{
// open a socket listner thread for the server to connect to
Thread listner = new Thread(new PortListnerThread());
listner.start();
}
}