Hi,
I originally had this class as an applet, but had some Java Security issues with connecting the client to the server. I then followed the step-by-step instructions on the Sun site to convert the applet to a Java Bean. When this class was an applet it ran fine. But when I am using it as a bean (bridged to an ActiveX control) it doesn't seem to go into the run() method at all, I am using Visual Basic 6.0 as the visual development tool.
I decided not to make the applet trusted because I could not find a simple way to do so. And since the application is to be run within a VB application anyways I decided on making it a bean.
Here is the code:
// messagingClient Class
// UNDER DEVELOPMENT -- To Become Applet at a later time
// Currently Reads All Messages From Server, sends acknowledgement that messages are received
// then loops back and obtains new set of messages
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
public class messagingClient extends Panel implements Runnable, Serializable
{
static final long serialVersionUID = 4325646018653553462L;
int xpos;
int ypos;
int textwidth;
String clienttype;
String callcenterid;
Vector messages;
String server;
int port;
Socket connection;
String message;
String endmsg;
int appWidth;
int appHeight;
transient Thread me;
public void writeObject(ObjectOutputStream out) throws IOException
{
out.defaultWriteObject();
}
public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
in.defaultReadObject();
me = new Thread(this);
initVars();
start();
}
public void start()
{
if(me==null)
{
me = new Thread(this);
initVars();
me.start();
}
}
public void setcallcenterid(String callcenterid)
{
this.callcenterid = callcenterid;
}
public String getcallcenterid()
{
return this.callcenterid;
}
public void setappWidth(int appWidth)
{
this.appWidth = appWidth;
}
public int getappWidth()
{
return this.appWidth;
}
public void setappHeight(int appHeight)
{
this.appHeight = appHeight;
}
public int getappHeight()
{
return this.appHeight;
}
public void setclienttype(String clienttype)
{
this.clienttype = clienttype;
}
public String getclienttype()
{
return this.clienttype;
}
public void setserver(String server)
{
this.server = server;
}
public String getserver()
{
return this.server;
}
public void setport(int port)
{
this.port = port;
}
public int getport()
{
return this.port;
}
private void initVars()
{
if(clienttype==null)
clienttype = "USER";
if(callcenterid==null)
callcenterid = "OH0001";
if(appWidth==0)
appWidth = 400;
if(appHeight==0)
appHeight = 14;
if(server==null)
server = "p3000883";
if(port==0)
port = 1550;
messages = new Vector();
connection = null;
message = "MESSAGES AWAIT!";
endmsg = "---- END OF MESSAGES ----";
setBackground(Color.black);
xpos = appWidth + 10;
ypos = 12; // position to look right
}
public messagingClient()
{
//DECLARE DEFAULT VALUES
if(clienttype==null)
clienttype = "USER";
if(callcenterid==null)
callcenterid = "OH0001";
if(appWidth==0)
appWidth = 400;
if(appHeight==0)
appHeight = 14;
if(server==null)
server = "p3000883";
if(port==0)
port = 1550;
messages = new Vector();
connection = null;
message = "MESSAGES AWAIT!";
endmsg = "---- END OF MESSAGES ----";
setBackground(Color.black);
xpos = appWidth + 10;
ypos = 12; // position to look right
start();
}
public void paint(Graphics g)
{
g.setColor(Color.black); // Background colour again
g.fillRect(0,0,appWidth + 1, appHeight + 1);
g.setColor(Color.white); // Text colour here
try
{
g.setFont(new Font("Arial",0,10)); // change font or font size (Fontname, 0, Size)
}
catch(NullPointerException _ex) { }
g.drawString(message, xpos, ypos);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
//connect to Server
try
{
connection = new Socket(server, port);
// System.out.println("Connected To Server"
ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
//SEND CLIENT PARAMETERS TO SERVER --- SERVER ALWAYS EXPECTS 2 PARAMETERS FROM CLIENTS
try
{
output.flush();
output.writeObject((String)clienttype);
output.flush();
output.writeObject((String)callcenterid);
output.flush();
}
catch(IOException e)
{
//client closed connection early
}
//READ SERVER MESSAGES
while(true) //CONNECTION IS OPEN
{
ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
while(true) //MESSAGES ARE BEING SENT
{
try
{
message = (String)input.readObject();
//If end of messages occurs, break from reading loop
if(message.equals(endmsg))
break;
messages.add(message);
//System.out.println(message);
}
catch(ClassNotFoundException e)
{
//System.out.println("Unknown Object Sent By Server"
}
} //END WHILE(TRUE) MESSAGES ARE BEING SENT
//**** SCROLLING MESSAGES FROM VECTOR ACROSS APP ****
for(int msgnum=0; msgnum < messages.size(); msgnum++)
{
message = (String) messages.elementAt(msgnum);
textwidth = 7 * message.length();
xpos = appWidth + 10;
while(xpos > (textwidth-2*textwidth))
{
xpos--;
try
{
Thread.sleep(10);
}
catch (InterruptedException _ex) { }
repaint();
}
}
xpos = appWidth + 10;
messages.removeAllElements();
//**** AFTER COMPLETED SENDING ONE 'CYCLE', OUTPUT TO SERVER THAT ****
//**** CLIENT IS READY FOR THE NEXT 'CYCLE' ****
ObjectOutputStream ops = new ObjectOutputStream(connection.getOutputStream());
String ready = "CLIENT READY";
ops.writeObject((String)ready);
output.flush();
} //END WHILE(TRUE) CONNECTION IS OPEN
}
catch(UnknownHostException e)
{
//System.out.println("Cannot Find Server"
//System.out.println(e);
//System.exit(1);
}
catch(IOException e)
{
//System.out.println("Server Found, but cannot connect to Server"
//System.out.println("Server software not running or maximum connections to Server have been reached"
//System.out.println(e);
//System.exit(1);
}
} // END RUN
}
I originally had this class as an applet, but had some Java Security issues with connecting the client to the server. I then followed the step-by-step instructions on the Sun site to convert the applet to a Java Bean. When this class was an applet it ran fine. But when I am using it as a bean (bridged to an ActiveX control) it doesn't seem to go into the run() method at all, I am using Visual Basic 6.0 as the visual development tool.
I decided not to make the applet trusted because I could not find a simple way to do so. And since the application is to be run within a VB application anyways I decided on making it a bean.
Here is the code:
// messagingClient Class
// UNDER DEVELOPMENT -- To Become Applet at a later time
// Currently Reads All Messages From Server, sends acknowledgement that messages are received
// then loops back and obtains new set of messages
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
public class messagingClient extends Panel implements Runnable, Serializable
{
static final long serialVersionUID = 4325646018653553462L;
int xpos;
int ypos;
int textwidth;
String clienttype;
String callcenterid;
Vector messages;
String server;
int port;
Socket connection;
String message;
String endmsg;
int appWidth;
int appHeight;
transient Thread me;
public void writeObject(ObjectOutputStream out) throws IOException
{
out.defaultWriteObject();
}
public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
in.defaultReadObject();
me = new Thread(this);
initVars();
start();
}
public void start()
{
if(me==null)
{
me = new Thread(this);
initVars();
me.start();
}
}
public void setcallcenterid(String callcenterid)
{
this.callcenterid = callcenterid;
}
public String getcallcenterid()
{
return this.callcenterid;
}
public void setappWidth(int appWidth)
{
this.appWidth = appWidth;
}
public int getappWidth()
{
return this.appWidth;
}
public void setappHeight(int appHeight)
{
this.appHeight = appHeight;
}
public int getappHeight()
{
return this.appHeight;
}
public void setclienttype(String clienttype)
{
this.clienttype = clienttype;
}
public String getclienttype()
{
return this.clienttype;
}
public void setserver(String server)
{
this.server = server;
}
public String getserver()
{
return this.server;
}
public void setport(int port)
{
this.port = port;
}
public int getport()
{
return this.port;
}
private void initVars()
{
if(clienttype==null)
clienttype = "USER";
if(callcenterid==null)
callcenterid = "OH0001";
if(appWidth==0)
appWidth = 400;
if(appHeight==0)
appHeight = 14;
if(server==null)
server = "p3000883";
if(port==0)
port = 1550;
messages = new Vector();
connection = null;
message = "MESSAGES AWAIT!";
endmsg = "---- END OF MESSAGES ----";
setBackground(Color.black);
xpos = appWidth + 10;
ypos = 12; // position to look right
}
public messagingClient()
{
//DECLARE DEFAULT VALUES
if(clienttype==null)
clienttype = "USER";
if(callcenterid==null)
callcenterid = "OH0001";
if(appWidth==0)
appWidth = 400;
if(appHeight==0)
appHeight = 14;
if(server==null)
server = "p3000883";
if(port==0)
port = 1550;
messages = new Vector();
connection = null;
message = "MESSAGES AWAIT!";
endmsg = "---- END OF MESSAGES ----";
setBackground(Color.black);
xpos = appWidth + 10;
ypos = 12; // position to look right
start();
}
public void paint(Graphics g)
{
g.setColor(Color.black); // Background colour again
g.fillRect(0,0,appWidth + 1, appHeight + 1);
g.setColor(Color.white); // Text colour here
try
{
g.setFont(new Font("Arial",0,10)); // change font or font size (Fontname, 0, Size)
}
catch(NullPointerException _ex) { }
g.drawString(message, xpos, ypos);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
//connect to Server
try
{
connection = new Socket(server, port);
// System.out.println("Connected To Server"
ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
//SEND CLIENT PARAMETERS TO SERVER --- SERVER ALWAYS EXPECTS 2 PARAMETERS FROM CLIENTS
try
{
output.flush();
output.writeObject((String)clienttype);
output.flush();
output.writeObject((String)callcenterid);
output.flush();
}
catch(IOException e)
{
//client closed connection early
}
//READ SERVER MESSAGES
while(true) //CONNECTION IS OPEN
{
ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
while(true) //MESSAGES ARE BEING SENT
{
try
{
message = (String)input.readObject();
//If end of messages occurs, break from reading loop
if(message.equals(endmsg))
break;
messages.add(message);
//System.out.println(message);
}
catch(ClassNotFoundException e)
{
//System.out.println("Unknown Object Sent By Server"
}
} //END WHILE(TRUE) MESSAGES ARE BEING SENT
//**** SCROLLING MESSAGES FROM VECTOR ACROSS APP ****
for(int msgnum=0; msgnum < messages.size(); msgnum++)
{
message = (String) messages.elementAt(msgnum);
textwidth = 7 * message.length();
xpos = appWidth + 10;
while(xpos > (textwidth-2*textwidth))
{
xpos--;
try
{
Thread.sleep(10);
}
catch (InterruptedException _ex) { }
repaint();
}
}
xpos = appWidth + 10;
messages.removeAllElements();
//**** AFTER COMPLETED SENDING ONE 'CYCLE', OUTPUT TO SERVER THAT ****
//**** CLIENT IS READY FOR THE NEXT 'CYCLE' ****
ObjectOutputStream ops = new ObjectOutputStream(connection.getOutputStream());
String ready = "CLIENT READY";
ops.writeObject((String)ready);
output.flush();
} //END WHILE(TRUE) CONNECTION IS OPEN
}
catch(UnknownHostException e)
{
//System.out.println("Cannot Find Server"
//System.out.println(e);
//System.exit(1);
}
catch(IOException e)
{
//System.out.println("Server Found, but cannot connect to Server"
//System.out.println("Server software not running or maximum connections to Server have been reached"
//System.out.println(e);
//System.exit(1);
}
} // END RUN
}