本帖最后由 段波 于 2011-12-7 12:53 编辑
public class Servicer implements Runnable{
Socket s;
public Servicer(Socket s) {
this.s = s;
}
public void run() {
try {
InputStream ips= s.getInputStream();
OutputStream ops= s.getOutputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(ips));
DataOutputStream dos= new DataOutputStream(ops);
while(true){
String str=br.readLine();
if(str.equalsIgnoreCase("quit")){
System.out.println("The Client has been out !");
break;
}
String strBack=str.toUpperCase();
dos.writeBytes(strBack);
br.close();
dos.close();
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TcpServer {
public static void main(String[] args) {
try {
ServerSocket ss= new ServerSocket(8000);
while(true){
Socket s=ss.accept();
new Thread(new Servicer(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TCPClient {
public static void main(String[] args) {
try {
Socket s= new Socket(InetAddress.getByName("172.16.9.115"),8000);
InputStream ips=s.getInputStream();
OutputStream ops= s.getOutputStream();
BufferedReader brKey= new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos= new DataOutputStream(ops);
BufferedReader brNet= new BufferedReader(new InputStreamReader(ips));
while(true){
String str= brKey.readLine();
dos.writeBytes(str+System.getProperty("line.separator"));
if(str.equalsIgnoreCase("quit")){
break;
}else
{
System.out.println(brNet.readLine());
}
}
dos.close();
brKey.close();
brNet.close();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
请问为什么客户端和服务端只能通信一次,客户端还没退出就自己死掉了呢?有时还返回null |