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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

applet uses socket in a separate thread :-)

Status
Not open for further replies.

IPOz

Programmer
Jul 11, 2001
109
CN
*************************** test.html ***************************
<html>
<body>
<script language=&quot;JavaScript&quot;>
function callJava(line)
{
window.status=&quot;line=&quot;+line;
}
</script>
<applet code=&quot;SocketApplet.class&quot; id=&quot;SocketApplet&quot; width=300 height=300 MAYSCRIPT>
<param name=serverip value=&quot;192.168.1.133&quot;>
</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(&quot;Send To server__HYJ&quot;);

buttonServerCommunicate.setBounds(0, 0, 95, 30);
buttonServerCommunicate.addActionListener(this);
add(buttonServerCommunicate);

textFieldInput = new TextField(&quot;Pls Input data to Server__HYJ&quot;);
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(&quot;exit init()&quot;);
}

public void actionPerformed(ActionEvent ae)
{
listOutput.removeAll();
if (Thread.currentThread().isAlive())
{
listOutput.add(&quot;thread is alive&quot;);
outputs.println(textFieldInput.getText());
}
else
listOutput.add(&quot;thread is not alive&quot;);
}

public String getRetStr() {
String ret= &quot;&quot;;
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(&quot;enter start()&quot;);

try {
String serverName=getParameter(&quot;serverIP&quot;);
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()+ &quot; -in start()&quot;);
}

listOutput.add(&quot;exit start()&quot;);
}

public void stop() {
listOutput.add(&quot;enter stop()&quot;);

threadReadSocket = null;
try {
client.close();
}
catch(IOException ioe) {
listOutput.add(&quot;IO:&quot; + ioe.getMessage());
}

listOutput.add(&quot;exit stop()&quot;);
}

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(&quot;reading from socket....&quot;);
if ((line = inputs.readLine()) != null)
{
listOutput.add(&quot;line = &quot; + line);
retStr.addElement(line); //save the received string
//call the JavaScript function
JSObject.getWindow(parent).eval(&quot;javascript:callJava(&quot;+line+&quot;);&quot;);
}
}
catch(SocketException se) {
listOutput.add(&quot;Socket exception: &quot; + se.getMessage());
}
catch(IOException ioe) {
listOutput.add(&quot;IO exception: &quot; + ioe.getMessage());
}
catch(JSException jse) {
listOutput.add(&quot;JSObject exception: &quot; + 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
 
Being lazy, i didn't read all your applet code yet. I did noticed that you are using a straight <applet> tag in IE... which means that you will be running the MS crap vm... download the HtmlConverter from java.sun.com and run your html page code that contains the applet tag on it. It will add in the necessary code to make IE use Sun's VM or download it if not present. If you still have problem... post a msg again and we can look to see if you have deadlock on your thread.

Good luck,

Dix
 
Thank you very much ! It works now :)
However after converted test.html using htmlconverter the id=&quot;SocketApplet&quot; was lost ! So i can NOT call applet from JavaScript .


Hope get more help from you !
IPO_z@cmmail.com
Garbage in,Garbage out
 
You should be able to just add the id attribute back into the applet tag. Just leave all the fluff around before and after the applet tag alone. Add the id attribute back where ever you see a <applet> tag.

Dix
 
...
<OBJECT classid=&quot;clsid:CAFEEFAC-0013-0001-0000-ABCDEFFEDCBA&quot;
WIDTH = 300 HEIGHT = 300 codebase=&quot;<PARAM NAME = CODE VALUE = &quot;SocketApplet.class&quot; >
<PARAM NAME = MAYSCRIPT VALUE = true >
<PARAM NAME = NAME VALUE=&quot;SocketApplet&quot;> <=== add this
<PARAM NAME=&quot;type&quot; VALUE=&quot;application/x-java-applet;jpi-version=1.3.1&quot;>

<PARAM NAME=&quot;scriptable&quot; VALUE=&quot;true&quot;> <== important
...

Then i can use Document.SocketApplet to use the applet now :)

Regards!
IPO_z@cmmail.com
Garbage in,Garbage out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top