Java套接字未收到来自打印编写器的响应
服务器从这里开始:Java套接字未收到来自打印编写器的响应,java,Java,服务器从这里开始: public static void main(String[] args){ System.out.println("Server has started"); try { ServerSocket socket = new ServerSocket(17000); while(true){ ThreadedClass w; w = new ThreadedClass(socket.accept
public static void main(String[] args){
System.out.println("Server has started");
try {
ServerSocket socket = new ServerSocket(17000);
while(true){
ThreadedClass w;
w = new ThreadedClass(socket.accept());
Thread t = new Thread(w);
t.start();
}
} catch (IOException e) {
System.out.print("Failed");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
那么这个班,
package com.sandislandsrv.rourke750;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ThreadedClass implements Runnable{
private Socket socket;
public ThreadedClass(Socket socket){
this.socket = socket;
}
@Override
public void run() {
MysqlDataClass db = Start.getData();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
String cred = in.readLine();
String[] creds = cred.split(" ");
System.out.print(creds[0] + creds[1]);
boolean authenticate = db.getUsernamePassValid(creds[0], creds[1]);
if (!authenticate){
System.out.println("Failed to log in, bad password");
out.println("BAD");
out.close();
return;
}
out.println("GOOD");
String line;
while ((line = in.readLine()) != null){
if (line.equals("END")){
out.close();
return;
}
if (line.equals("GET")){
out.println(db.getMessages(creds[0]));
}
if (line.equals("IN")) break;
if (line.equals("REMOVE")){
line = in.readLine();
db.removeMessage(creds[0], line);
}
}
line = in.readLine();
String[] format = line.split("Theamjgfdngkngfd8998906504906595665");
String[] timeformat = format[1].split(":");
long time = System.currentTimeMillis()/1000;
if (Long.parseLong(timeformat[0]) != 0)
time += 3600 * Long.parseLong(timeformat[0]);
if (Long.parseLong(timeformat[1]) != 0)
time += 60 * Long.parseLong(timeformat[1]);
if (Long.parseLong(timeformat[2]) != 0)
time += Long.parseLong(timeformat[2]);
db.addMessage(creds[0], format[0], time);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void remove(){
}
}
然后稍后我调用这个方法
public String[] getNames(){
StringBuilder builder = new StringBuilder();
try {
Socket socket = new Socket("share.betterassociations.com", 17000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(username.getText().toString() + " " + password.getText().toString());
System.out.print("test");
String passed = input.readLine();
System.out.print("test2");
if (passed.equals("BAD")) {
System.out.print("fail");
loginerror.setVisible(true);
socket.close();
return null;
}
System.out.print("test2");
out.println("GET");
String line;
while ((line = input.readLine()) != null){
if (line.equals("END")) break;
builder.append(line);
}
out.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString().split(" ");
}
出于某种原因,它似乎在最后一个方法中被冻结,该方法使用字符串passed=input.readLine()
我不明白为什么会发生这种情况,因为我正在从服务器向客户端发送一个字符串,但客户端没有接收到它。在为服务器和客户端的输出流写入out.flush()后,添加对它的调用,如下所示
....
out.println("GOOD");
out.flush();
....
或者,在此处通过更改服务器(客户端已启用)启用自动刷新:
但根据文件:
如果启用了自动刷新,则仅当调用println、printf或format方法之一时,而不是在输出换行符时,才会执行自动刷新
所以不要被激怒我想出来了。println auto flush?@rourke750在Javadoc的哪个地方这么说?您正在从一个
OutputStream构建一个PrintWriter
。
它“从一个现有的OutputStream
创建一个新的PrintWriter,
”[我的重点]。@EJP,看起来他启用了自动刷新,但只在客户端上启用了。这可能是他和我困惑的根源。当客户端关闭套接字并且readLine()
返回null时,服务器线程中将出现NullPointerException
。您正在检测这种情况,但随后您陷入了不知道它的代码中,不断阅读,将得到另一个空值,不检查,等等。
....
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
....