本帖最后由 邱成 于 2012-9-22 14:28 编辑
java socket问题,client端连接server,client端通过输入流获取server传过来的一段话,并打印,完后Client端退出JVM,但如下代码Client端不能退出,请高手帮忙分析一下。谢谢!!
Server端部分代码:
try {
ServerSocket ss = new ServerSocket(6666);
while (true) {
Socket socket = ss.accept();
OutputStream os = socket.getOutputStream();
String data = "恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!"+
"恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!"+
"恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!恭喜你,连接服务器成功!!";
System.out.println(data.length());
os.write(data.getBytes());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Client端部分代码:
BufferedInputStream bis=null;
Socket socket=null;
try {
socket=new Socket(InetAddress.getLocalHost(), 6666);
bis=new BufferedInputStream(socket.getInputStream());
byte[] buffer=new byte[1024];
int length;
while((length=bis.read(buffer, 0, buffer.length))!=-1){
String str=new String(buffer, 0, length);
System.out.println(str);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
bis.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
} |