接上个贴子的问题,我重写了一个简单的server和client,考虑到再internet上应用采用unicode作为传输编码。
当再server端按 enter键正常close掉socket,按ctrl+c相当于关机等操作,
则在client端分别引起EOFExceptin 和IOException 以区别这两种情况,
我想这样能解决你的问题了吧。
当再server端按 enter键正常close掉socket,按ctrl+c相当于关机等操作,
则在client端分别引起EOFExceptin 和IOException 以区别这两种情况,
我想这样能解决你的问题了吧。
import java.io.*;
public class SimpleClient {
public static void main(String args[])
throws IOException {
int c;
Socket s1;
InputStream s1In;
DataInputStream dis;
// Open your connection to a server, at port 5432
// localhost used here
s1 = new Socket("127.0.0.1",5432);
// Get an input file handle from the socket and
// read the input
s1In = s1.getInputStream();
dis = new DataInputStream(s1In); while (true){
try {
String st = new String (dis.readUTF());
System.out.println(st);
}catch (EOFException ee){
System.out.println("End of Stream");
break;
}catch (IOException e){
System.out.println("IOException, May be server closed");
break;
}
}
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}