Java不能同时读取和写入套接字
我正在尝试使用java套接字构建一个简单的多客户端聊天应用程序。我这样做的方式是让一个客户机类连接到一个服务器类,该服务器类等待客户机连接,并创建一个新线程来处理读取和写入套接字连接的客户机。客户端还读取和写入到该线程的套接字连接。但是,当客户端写入套接字的输出流时,服务器不会响应。这里贴出了一个类似的问题: 这里的一个答案是,只要在单独的线程上读取套接字,就可以同时读取和写入套接字 这是我的客户端应用程序:Java不能同时读取和写入套接字,java,multithreading,sockets,Java,Multithreading,Sockets,我正在尝试使用java套接字构建一个简单的多客户端聊天应用程序。我这样做的方式是让一个客户机类连接到一个服务器类,该服务器类等待客户机连接,并创建一个新线程来处理读取和写入套接字连接的客户机。客户端还读取和写入到该线程的套接字连接。但是,当客户端写入套接字的输出流时,服务器不会响应。这里贴出了一个类似的问题: 这里的一个答案是,只要在单独的线程上读取套接字,就可以同时读取和写入套接字 这是我的客户端应用程序: public class Client { Socket socke
public class Client {
Socket socket;
public static void main(String[] args) {
new Client();
}
public Client() {
try {
socket = new Socket("localhost", 4444);
new Thread() {
@Override
public void run() { //read from the input stream
try(
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
String line;
while( (line = in.readLine()) != null ) {
System.out.println("Server said: " + line);
}
} catch(IOException e) {
}
}
}.start();
//write to output stream
try(
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner userInput = new Scanner(System.in);
){
System.out.println("Enter Something: ");
if(userInput.hasNextLine()) {
out.println(userInput.nextLine());
}
} catch (IOException e) {
}
} catch(IOException e) {
}
}
}
public class Server {
ServerSocket ss;
public static void main(String[] args) {
new Server();
}
public Server() {
System.out.println("Server Running...");
try {
ss = new ServerSocket(4444);
while(true) {
Socket socket = ss.accept();
new Thread() { //create new thread connection to client
@Override
public void run() {
new Thread() { //thread that reads inputstream
@Override
public void run() {
try(
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
String line;
while( (line = in.readLine()) != null ) {
System.out.println("Client said: " + line);
//The problem seems to lie here.
}
} catch(IOException e) {
}
}
}.start();
//write to outputstream
try (
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
) {
String sendToClient = "Hey, my name is Server007 B)";
out.println(sendToClient);
} catch(IOException e) {
}
}
}.start();
}
} catch (IOException e) {}
}
}
和我的服务器应用程序:
public class Client {
Socket socket;
public static void main(String[] args) {
new Client();
}
public Client() {
try {
socket = new Socket("localhost", 4444);
new Thread() {
@Override
public void run() { //read from the input stream
try(
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
String line;
while( (line = in.readLine()) != null ) {
System.out.println("Server said: " + line);
}
} catch(IOException e) {
}
}
}.start();
//write to output stream
try(
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner userInput = new Scanner(System.in);
){
System.out.println("Enter Something: ");
if(userInput.hasNextLine()) {
out.println(userInput.nextLine());
}
} catch (IOException e) {
}
} catch(IOException e) {
}
}
}
public class Server {
ServerSocket ss;
public static void main(String[] args) {
new Server();
}
public Server() {
System.out.println("Server Running...");
try {
ss = new ServerSocket(4444);
while(true) {
Socket socket = ss.accept();
new Thread() { //create new thread connection to client
@Override
public void run() {
new Thread() { //thread that reads inputstream
@Override
public void run() {
try(
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
String line;
while( (line = in.readLine()) != null ) {
System.out.println("Client said: " + line);
//The problem seems to lie here.
}
} catch(IOException e) {
}
}
}.start();
//write to outputstream
try (
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
) {
String sendToClient = "Hey, my name is Server007 B)";
out.println(sendToClient);
} catch(IOException e) {
}
}
}.start();
}
} catch (IOException e) {}
}
}
我将运行服务器,然后运行客户端,在客户端输出
Server said: Hey, my name is Server007
Enter something:
Hello! <- enter anything
但是服务器没有打印“客户端说:你好!”就像我预料的那样。我希望我把问题讲清楚了,谢谢。好的,我想好了,我会回答我自己的问题,以防有人犯同样的错误。PrintWriter构造函数应如下所示:
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
不是这个:
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
或者,我可以这样做:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
我一定是混淆了BufferedWriter和PrintWriter:p