client hanging when server is recycled.

Hi,

I wrote a socket client program that connects to a socket server. This is what I'm doing:

-I'm opening a socket connection

-I'm writing an initial request to the output stream of that socket

-Then, while true, I create an input stream of that socket and get information off that stream

It all works fine, but when the socket server goes down, my input stream hangs. In other words, when the socket server goes down, my client does not come out of the while true loop.

So, how can I close my input stream? How can I know when the server socket went down?

Thanks,

RR

If you are interested in the code:

publicclass SequentialStats{

publicstaticvoid main (String[] args)

throws UnknownHostException, IOException, InterruptedException{

//create a socket

Socket sock =new Socket("es2a208d", 30100);

//open an output stream

BufferedOutputStream outStr =new BufferedOutputStream(sock.getOutputStream());

//initialization request

byte[] initRequest =newbyte[]{0, 8, 0, 1,'1','5','0','5'};

//write the request to the output stream

outStr.write(initRequest);

//flush the stream

outStr.flush();

while(true){

BufferedInputStream bis =new BufferedInputStream(sock.getInputStream());

byte[] barr =newbyte[1948];

bis.read(barr, 0, 1948);

ByteArrayInputStream bais =new ByteArrayInputStream (barr, 0, 1948);

DataInputStream distrm =new DataInputStream (bais);

System.out.println ("MsgSize = " + distrm.readShort());

System.out.println ("MsgType = " + distrm.readShort());

bais.close(); distrm.close();

}

}

}

Message was edited by:

rrvvg_1976

[2953 byte] By [rrvvg_1976a] at [2007-11-27 10:19:43]
# 1

> So, how can I close my input stream?

Socket.close() or InputStream.close()

> How can I know when the server socket went down?

Socket.setSoTimeout(). If the timeout expires, the read method you are calling will throw a SocketTimeoutException.

ejpa at 2007-7-28 16:59:22 > top of Java-index,Core,Core APIs...