posted 23 years ago
Thanks Sri Bala And Frank.
Here is my one of subclasses code :
public class K_Socket extends Thread implements ActionListener
{
private Socket localSocket;
private Socket sclient;
private BufferedReader in;
private BufferedReader demarsh;
private PrintWriter out;
private Socket clientSocket;
private String mesg;
private StringBuffer buf;
private StringBuffer sendBuffer;
private int setTime = 5000;
protected K_Main main;
protected K_Marshall kmar;
int port,tmp1,maxbuf,read,temp;
int bytesend = 0;
char [] buffer;
char [] mBuffer;
Timer timer;
// constructor
public K_Socket(K_Main Main)
{
port = 9999;
bytesend = 0;
maxbuf = 2000;
tmp1 = 0;
temp = 0;
buffer = new char[maxbuf];
mBuffer = new char[2000];
sendBuffer = new StringBuffer(2000);
timer = new Timer(setTime,this);
buf = new StringBuffer(2000);
main = Main;
kmar = (K_Marshall)main.getObjects(1);
}
//open the socket to remote host
public void open(String ipAddr)
{
try
{
localSocket = new Socket(ipAddr , port);
System.out.println("The Host IP address is:" + localSocket.getInetAddress());
out = new PrintWriter(localSocket.getOutputStream());
}
catch(UnknownHostException uhe)
{
System.out.println("The Host is Unknown");
}
catch(IOException ioe)
{
System.out.println("I/O Error");
}
}
//To send the data to the server
public void send()
{
out.println(sendBuffer);
out.flush();
System.out.println("Marshall message sent to server:" + sendBuffer);
bufferCleanUp();
}
//To receive the data from the server
public void receive()
{
try
{
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
// loop to read data from server
while( (tmp1 = in.read(buffer, 0, 2000)) >= 0 )
{
System.out.println("current bytereceive:" + tmp1);
if( tmp1 < 0 )<br /> {<br /> System.out.println("Can not have a negative string");<br /> return;<br /> }<br /> main.addData(buffer , tmp1);<br /> }<br /> }<br /> catch(IOException e)<br /> {<br /> System.out.println("Reading Error");<br /> }<br /> }<br /> public int returnData()<br /> {<br /> timer.start();<br /> buf.append(mBuffer , 0 , temp);<br /> mesg = buf.toString();<br /> kmar.deMarshall(mesg);<br /> return 1;<br /> }<br /> //to return the buffer to store marshall data<br /> public StringBuffer getSendBlk()<br /> {<br /> return sendBuffer;<br /> }<br /> //to cleanup the buffer<br /> public int bufferCleanUp()<br /> {<br /> sendBuffer.setLength(0);<br /> return 1;<br /> }<br /> //check to see whether any message or not at specific interval<br /> public void actionPerformed(ActionEvent evt)<br /> {<br /> try<br /> {<br /> demarsh = new BufferedReader(new InputStreamReader(localSocket.getInputStream( )));<br /> if( (temp = in.read(mBuffer, 0, 2000)) >= 0 )
{
System.out.println("The Demarshall from Server!");
}
else
{
System.out.println("No Response from the Server!");
}
}
catch(IOException me)
{
System.out.println("Reading Demarshall Error");
}
timer.stop();
System.out.println("Time out!");
}
// to close the socket
public void close()
{
try
{
localSocket.close();
}
catch(IOException ie)
{
System.out.println("Error Closing Socket");
}
}
public void run()
{
receive();
System.out.println("Run RECEIVE() again!");
}
}
I have used Timer. Thread is continuously run to listen data from the server. So I used Timer, b'cos i can't use Thread.sleep() as thread is continuously running.
Question: The use of Timer is appropriate?
Plz let me know
Thanks again,
Angela