我有一个套接字客户端向套接字服务器发送文本,但ReadLine似乎还没等到接收一行,然后继续。这是接收文本的服务器:
public void run() {
try {
serveurSocket = new ServerSocket(PORT_ID);
connexionSocket = serveurSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(connexionSocket.getInputStream()));
PrintWriter writer = new PrintWriter(connexionSocket.getOutputStream(), true);
messageRecu = "";
while (true) {
messageRecu = reader.readLine();
messageRecu = messageRecu.toUpperCase();
writer.println(messageRecu);
}
//reader.close();
//writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
在客户端和服务器之间建立套接字后,执行将在reader.readLine处停止,直到我通过套接字手动发送字符串。这是正常的和想要的。代码恢复并且它很好,直到它循环回到reader.ReadLine(),它将读取一个“null”行,而不是像第一次那样等待来自套接字的输入......这显然会使下一个命令陷入困境大写。那么我该如何解决这个问题?
编辑:如果可以帮助理解,我会添加客户端。
public class ClientSocket {
private Socket clientSocket;
public boolean isClosed() { return clientSocket.isClosed(); }
public boolean connectToSocket (String ip, int port) {
try {
clientSocket = new Socket(ip, port);
return true;
}
catch (IOException e) {
System.out.println(e);
return false;
}
}
public String sendToServer(String messageClient) {
String messageRecu = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
writer.println(messageClient);
messageRecu = reader.readLine();
reader.close();
writer.close();
return messageRecu;
}
catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return messageRecu;
}
}
}
按下按钮将调用“connectTosocket”以启动套接字。按下第二个按钮将使用“sendToServer”发送文本字段的内容。
服务器确实收到消息并将其返回大写但我希望套接字保持与服务器一起打开,如果我发送另一个字符串以便发生相同的序列。甚至不确定它是否可以完成:(
答案 0 :(得分:2)
根据BufferedReader#readLine
的文档,如果已到达流的末尾,则返回null。
将您的阅读循环更改为:
while ((messageRecu = reader.readLine()) != null) {
messageRecu = messageRecu.toUpperCase();
writer.println(messageRecu);
}
//Get out of the loop when the end of stream is reached.
根据Java教程的Reading from and Writing to a Socket章节。
作为旁注,while(true)
循环are not really appreciated。
答案 1 :(得分:0)
来自客户端的连接结束的“空”信号 - 这就是连接断开的原因。如果要支持多个请求,则应每次运行新的ServerSocket.accept()
并等待新客户端进行连接。
import java.net.*;
import java.io.*;
public class KKMultiServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java KKMultiServer <port number>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
boolean listening = true;
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
new KKMultiServerThread(serverSocket.accept()).start();
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
}
}
import java.net.*;
import java.io.*;
public class KKMultiServerThread extends Thread {
private Socket socket = null;
public KKMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run() {
try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
) {
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye"))
break;
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
您可以在Oracle tutorials
中详细了解套接字