马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//客户端
public class Client {
public static void main(String[] args) throws IOException {
Socket clientSocket = new Socket("localhost", 8889);
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
ps.println("美羊羊,今天晚上有时间吗,来我家我教你java~");
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Server Message:");
while (br.readLine() != null) {
System.out.println(br.readLine());
}
ps.close();
br.close();
clientSocket.close();
}
}
//服务端
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(8889);
Socket clientSocket = socket.accept();
System.out.println("客户端来连接了...");
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Client Message:" + '\n' + br.readLine());
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
ps.println("Server Message:" + '\n' + "晚上我有时间,不见不散哦~");
br.close();
ps.close();
socket.close();
}
}
为什么客户端写入必须使用PrintStream流 否则程序将进入线程堵塞,有大佬能解答一下吗 |