看看为啥子是个死循环
大家帮我看看 为啥子是个死循环 ,while(info!=null)才执行 客户端就发了 一条字符串 ,为啥子是无限读取
服务端
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("服务器正在启动等待链接");
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("服务器接受的内容"+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(); } }