Socket单方面向ServerSocket提供流可以ServerSocket可以拿流 双向通信就不行了 没反应截屏我贴出来
public class Client1 {
public static void main(String[] args) {
//创建客户端的Socket通信
try {
Socket socket=new Socket("localhost", 8888);
OutputStream os=socket.getOutputStream();//获取输出流
PrintWriter pw=new PrintWriter(os);//包装为打印流
pw.write("用户名:Admin;密码:123");
pw.flush();
//故去输入流
InputStream is=socket.getInputStream();//字符流
InputStreamReader isr=new InputStreamReader(is);//字符流
BufferedReader br=new BufferedReader(isr);//添加缓冲区
String info="";
while((info=br.readLine())!=null){//循环输出客户端的数据
System.out.println("我是服务器:客户端说"+info);
}
br.close();
isr.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Server1 {
public static void main(String[] args) {
try {
ServerSocket serverSocket=new ServerSocket(8888);
System.out.println("********服务器将打开*********");
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();//字符流
InputStreamReader isr=new InputStreamReader(is);//字符流
BufferedReader br=new BufferedReader(isr);//添加缓冲区
String info="";
while((info=br.readLine())!=null){//循环输出客户端的数据
System.out.println("我是服务器:客户端说"+info);
}
//服务器响应用户端的请求
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("欢迎admin登录");
pw.flush();
//关闭Socket
pw.close();
os.close();
br.close();
isr.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}