import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatRoomServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(4800);
Socket s = ss.accept();
BufferedReader brs = new BufferedReader(new InputStreamReader(
System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
String str1, str2;
while (!(str2 = brs.readLine()).equals("bye")) {
System.out.println("服务器" + str2);
bw.write(str2);
bw.flush();
}
while (!(str1 = br.readLine()).equals("bye")) {
System.out.print("客户端" + str1);
}
bw.close();
brs.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatRoomClient {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 4800);
BufferedReader brs = new BufferedReader(new InputStreamReader(
System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String str1, str2;
while (!(str1 = brs.readLine()).equals("bye")) {
System.out.println("客户端" + str1);
bw.write(str1);
// str1 = brs.readLine();
}
while (!(str2 = br.readLine()).equals("bye")) {
System.out.println("服务器" + str2);
}
brs.close();
bw.close();
br.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
这是聊天的小程序,为什么服务器端老是readLine不到东西? |