所以我正在尝试建立一个客户端 <-> 服务器连接。我不断收到最烦人的错误,因为客户端和服务器无法连接,但在我打电话时只发送一次“cpc”
print("cpc");
这是我正在使用的代码。这包含在扩展 Thread 的类中。
BufferedReader in = null;
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
print("cpc");
try{
while ((inputLine = in.readLine()) != null) {
outputLine = process(inputLine);
if (!outputLine.equals("")){
print(outputLine);
}
}
} catch (Exception e){
e.printStackTrace();
}
错误出现在 'while ((inputLine = in.readLine()) != null) {' 的位置。
如果您需要它,这是错误。
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at javaclient.Client.run(Client.java:28)
这是 print() 代码
public void print(String s){
out.println(s);
}