我还有一个关于我的推送服务器的问题。由于某种原因,服务器在其生命周期内只接受一个连接。即使在第一个连接关闭后,服务器也不会执行任何操作。我怀疑线程没有产生,因为它没有拒绝连接。
以下是服务器的代码:http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServer.java
我使用了这个例子,因为它正是我所需要的。我保持这段代码不变。这是我真正合作过的主题......
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 {
final PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
out.println("connected");
boolean loggedin = false;
String username="";
String password="";
String deviceid="";
while (true) {
//deal with login handshake
if ((inputLine = in.readLine()) == null) inputLine="";
if (!loggedin) {
Logs the user in...
Also does things with files and keeps reading and writing to the client...
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}
可能出现什么问题?我关闭套接字和所有流,就像我应该但即使它应该仍然有效,不应该吗?
感谢您的继续支持!
答案 0 :(得分:4)
if((inputLine = in.readLine())== null)inputLine =“”;
这行代码是A级废话。如果inputLine为null,则对等体已关闭套接字,您必须退出循环并自行关闭套接字。目前你忽略了EOS条件并永远循环。