*************************** test.html ***************************
<html>
<body>
<script language="JavaScript">
function callJava(line)
{
window.status="line="+line;
}
</script>
<applet code="SocketApplet.class" id="SocketApplet" width=300 height=300 MAYSCRIPT>
<param name=serverip value="192.168.1.133">
</applet>
</body>
</html>
*************************** applet ***************************
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.Vector;
import netscape.javascript.*;
public class SocketApplet extends Applet implements ActionListener
{
ReceiveThread threadReadSocket = null;
Button buttonServerCommunicate;
TextField textFieldInput;
List listOutput;
Vector retStr = new Vector();
Socket client = null;
BufferedReader inputs = null;
PrintStream outputs = null;
public void init() {
setLayout( null );
addNotify();
resize(379, 189);
buttonServerCommunicate = new Button("Send To server__HYJ"
buttonServerCommunicate.setBounds(0, 0, 95, 30);
buttonServerCommunicate.addActionListener(this);
add(buttonServerCommunicate);
textFieldInput = new TextField("Pls Input data to Server__HYJ"
textFieldInput.setEditable(true);
add(textFieldInput);
textFieldInput.setBounds(100, 0, 279, 30);
listOutput = new List(2, false);
add(listOutput);
listOutput.setBounds(0, 40, 379, 149);
super.init();
listOutput.removeAll();
listOutput.add("exit init()"
}
public void actionPerformed(ActionEvent ae)
{
listOutput.removeAll();
if (Thread.currentThread().isAlive())
{
listOutput.add("thread is alive"
outputs.println(textFieldInput.getText());
}
else
listOutput.add("thread is not alive"
}
public String getRetStr() {
String ret= "";
if (retStr.size() > 0) {
ret = (String)retStr.elementAt(0);
retStr.removeElementAt(0);
}
return ret;
}
public void setStr(String hello) {
outputs.println(hello);
}
public void start() {
listOutput.add("enter start()"
try {
String serverName=getParameter("serverIP"
client = new Socket(serverName,8002);
inputs = new BufferedReader(new InputStreamReader(client.getInputStream()));
outputs = new PrintStream(client.getOutputStream());
threadReadSocket= new ReceiveThread(this);
threadReadSocket.start();
}
catch(Exception e){
listOutput.add(e.getMessage()+ " -in start()"
}
listOutput.add("exit start()"
}
public void stop() {
listOutput.add("enter stop()"
threadReadSocket = null;
try {
client.close();
}
catch(IOException ioe) {
listOutput.add("IO:" + ioe.getMessage());
}
listOutput.add("exit stop()"
}
public void destroy() {
}
public void paint() {
}
//inner class
class ReceiveThread extends Thread
{
Applet parent;
public ReceiveThread(Applet parent) {
this.parent= parent;
}
public void run() {
String line;
while(threadReadSocket == Thread.currentThread()) {
try
{
listOutput.add("reading from socket...."
if ((line = inputs.readLine()) != null)
{
listOutput.add("line = " + line);
retStr.addElement(line); //save the received string
//call the JavaScript function
JSObject.getWindow(parent).eval("javascript:callJava("+line+""
}
}
catch(SocketException se) {
listOutput.add("Socket exception: " + se.getMessage());
}
catch(IOException ioe) {
listOutput.add("IO exception: " + ioe.getMessage());
}
catch(JSException jse) {
listOutput.add("JSObject exception: " + jse.getMessage());
}
} //while
}
}
}
It works however the IE will be frozen when pressing CTRL+F5 to reload the page
Regards! IPO_z@cmmail.com
Garbage in,Garbage out
<html>
<body>
<script language="JavaScript">
function callJava(line)
{
window.status="line="+line;
}
</script>
<applet code="SocketApplet.class" id="SocketApplet" width=300 height=300 MAYSCRIPT>
<param name=serverip value="192.168.1.133">
</applet>
</body>
</html>
*************************** applet ***************************
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.Vector;
import netscape.javascript.*;
public class SocketApplet extends Applet implements ActionListener
{
ReceiveThread threadReadSocket = null;
Button buttonServerCommunicate;
TextField textFieldInput;
List listOutput;
Vector retStr = new Vector();
Socket client = null;
BufferedReader inputs = null;
PrintStream outputs = null;
public void init() {
setLayout( null );
addNotify();
resize(379, 189);
buttonServerCommunicate = new Button("Send To server__HYJ"
buttonServerCommunicate.setBounds(0, 0, 95, 30);
buttonServerCommunicate.addActionListener(this);
add(buttonServerCommunicate);
textFieldInput = new TextField("Pls Input data to Server__HYJ"
textFieldInput.setEditable(true);
add(textFieldInput);
textFieldInput.setBounds(100, 0, 279, 30);
listOutput = new List(2, false);
add(listOutput);
listOutput.setBounds(0, 40, 379, 149);
super.init();
listOutput.removeAll();
listOutput.add("exit init()"
}
public void actionPerformed(ActionEvent ae)
{
listOutput.removeAll();
if (Thread.currentThread().isAlive())
{
listOutput.add("thread is alive"
outputs.println(textFieldInput.getText());
}
else
listOutput.add("thread is not alive"
}
public String getRetStr() {
String ret= "";
if (retStr.size() > 0) {
ret = (String)retStr.elementAt(0);
retStr.removeElementAt(0);
}
return ret;
}
public void setStr(String hello) {
outputs.println(hello);
}
public void start() {
listOutput.add("enter start()"
try {
String serverName=getParameter("serverIP"
client = new Socket(serverName,8002);
inputs = new BufferedReader(new InputStreamReader(client.getInputStream()));
outputs = new PrintStream(client.getOutputStream());
threadReadSocket= new ReceiveThread(this);
threadReadSocket.start();
}
catch(Exception e){
listOutput.add(e.getMessage()+ " -in start()"
}
listOutput.add("exit start()"
}
public void stop() {
listOutput.add("enter stop()"
threadReadSocket = null;
try {
client.close();
}
catch(IOException ioe) {
listOutput.add("IO:" + ioe.getMessage());
}
listOutput.add("exit stop()"
}
public void destroy() {
}
public void paint() {
}
//inner class
class ReceiveThread extends Thread
{
Applet parent;
public ReceiveThread(Applet parent) {
this.parent= parent;
}
public void run() {
String line;
while(threadReadSocket == Thread.currentThread()) {
try
{
listOutput.add("reading from socket...."
if ((line = inputs.readLine()) != null)
{
listOutput.add("line = " + line);
retStr.addElement(line); //save the received string
//call the JavaScript function
JSObject.getWindow(parent).eval("javascript:callJava("+line+""
}
}
catch(SocketException se) {
listOutput.add("Socket exception: " + se.getMessage());
}
catch(IOException ioe) {
listOutput.add("IO exception: " + ioe.getMessage());
}
catch(JSException jse) {
listOutput.add("JSObject exception: " + jse.getMessage());
}
} //while
}
}
}
It works however the IE will be frozen when pressing CTRL+F5 to reload the page
Regards! IPO_z@cmmail.com
Garbage in,Garbage out