程序一;
客户端
public class client2 {
public static void main(String argv[]) throws UnknownHostException, IOException{
String str[]={"4","5","6"};
Socket socket=new Socket("localhost",8080);
DataInputStream input=new DataInputStream(socket.getInputStream());
DataOutputStream output=new DataOutputStream(socket.getOutputStream());
for(String str1:str){
output.writeUTF(str1);
String yourStr=input.readUTF();
System.out.println(yourStr);
}
socket.close();
}
}
服务器端
public class Server2 {
@SuppressWarnings("Deprecated")
public static void main(String argv[]) throws UnknownHostException, IOException{
String str[]={"1","2","3"};
ServerSocket server=new ServerSocket(8080);
Socket socket=server.accept();
System.out.println("succeed");
DataInputStream input=new DataInputStream(socket.getInputStream());
DataOutputStream output=new DataOutputStream(socket.getOutputStream());
for(String str1:str){
String yourStr=input.readUTF();
System.out.println(yourStr);
output.writeUTF(str1);
}
socket.close();
server.close();
}
}
这里并没有使用socket.shutdownOutput();程序正常运行
程序二
服务器端;
public class Server {
public static void main(String argv[]) throws IOException{
ServerSocket serverSocket=new ServerSocket(8888);
System.out.println("succeed,wait client connects");
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader reader=new BufferedReader(isr);
String info=null;
while((info=reader.readLine())!=null){
System.out.println("我是服务器,客户端说"+info);}
socket.shutdownInput();
OutputStream out=socket.getOutputStream();
PrintWriter writer=new PrintWriter(out);
writer.write("welcome!");
writer.flush();
socket.close();
serverSocket.close();
}
}
客户端;
public class Client {
public static void main(String[]argv) throws UnknownHostException, IOException{
Socket socket=new Socket("localhost",8888);
OutputStream stream=socket.getOutputStream();
PrintWriter writer=new PrintWriter(stream);
writer.println("用户名:我;密码:19");
writer.flush();
// 此处
socket.shutdownOutput();
InputStream st=socket.getInputStream();
InputStreamReader read=new InputStreamReader(st);
BufferedReader bu=new BufferedReader(read);
String info=null;
while((info=bu.readLine())!=null){
System.out.println(info);
}
socket.close();
}
}
这里如果去掉客户端的 socket.shutdownOutput();程序将不能运行
我不是很明白,请问有谁知道这是为什么吗?
摩诃迦叶
相关分类