看看為啥子是個死循環(huán)
大家?guī)臀铱纯?為啥子是個死循環(huán) ,while(info!=null)才執(zhí)行 ?客戶端就發(fā)了 一條字符串 ,為啥子是無限讀取
服務(wù)端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
?
public class Server {
public static void main(String[] args)
{
try {
ServerSocket sersoc=new ServerSocket(9999);
System.out.print("服務(wù)器正在啟動等待鏈接");
Socket soc=sersoc.accept();
InputStream is=soc.getInputStream();
BufferedReader isr=new BufferedReader(new InputStreamReader(is));
String info=null;
info=isr.readLine();
while(info!=null)
{
System.out.print("服務(wù)器接受的內(nèi)容"+info+"\n");
}
soc.shutdownInput();
sersoc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
?
}
?
?
}
客戶端: import?java.io.IOException; import?java.io.OutputStream; import?java.io.PrintWriter; import?java.net.Socket; import?java.net.UnknownHostException; ? public?class?client?{ public?static?void?main(String[]?args) { Socket?soc=null; try?{ soc?=?new?Socket("localhost",9999); OutputStream?os=soc.getOutputStream(); PrintWriter?osw=new?PrintWriter(os); osw.write("我是客戶端?"); osw.flush(); soc.shutdownOutput(); soc.close(); }?catch?(UnknownHostException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } ? }
2015-05-19
while(info!=null)
{
System.out.print("服務(wù)器接受的內(nèi)容"+info+"\n");
info=isr.readLine();
}
2015-05-13
你把while改成If 就可以了。while肯定死循環(huán) 因為file!=null 為真 就相當(dāng)于 while(true);