Java socket giving premature end of stream
I'm setting up a comet server that connects to a XMPP server. Here's how it goes down:
A client connects with the comet server and, among other things, a socket connection is opened:
try {
radio = new Socket("server", 5222);
out = new PrintWriter(radio.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(radio.getInputStream()));
} catch (UnknownHostException e) {
System.out.println("Unknown host: "+e);
error = e.toString();
} catch(IOException e) {
System.out.println("IO error: "+e);
error = e.toString();
}
Next, a thread is started, which waits for data from the socket:
public void run() {
System.out.println("Thread started.");
String data;
String error;
Client remote;
Client client;
while(!done) {
data = this.output();
remote = bayeux.getClient(remoteId);
client = bayeux.getClient(clientId);
if(data!=null) {
Map<String, Object> packet = new HashMap<String, Object>();
packet.put("xml", data);
remote.deliver(client, "/radio/from", packet, null);
}
error = this.error();
if(error!=null) {
Map<String, Object> packet = new HashMap<String, Object>();
packet.put("details", error);
remote.deliver(client, "/radio/error", packet, null);
}
/* try {
Thread.sleep(500);
}
catch (InterruptedException e ) {
System.out.println("Interrupted!"); } */
}
try {
in.close();
out.close();
radio.close();
} catch(IOException e) {
System.out.println("Error disconnectin开发者_高级运维g: "+e);
error = e.toString();
}
System.out.println("Thread stopped.");
}
public String output() {
try {
String data = in.readLine();
System.out.println("From: "+data);
if(data==null) {
System.out.println("End of stream!");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//error = "End of stream.";
//this.disconnect();
}
return data;
} catch (IOException e) {
error = e.toString();
System.out.println("IO error! "+e);
}
return null;
}
Any input received from the client is forwarded to the XMPP server:
public void input(String xml) {
System.out.println("To: "+xml);
out.println(xml);
}
So here's where the problem come in. The client opens the connection and sends the proper XML to the XMPP server to start a stream. in.readLine();
hangs, as it should, until a response is received from the server. As soon as it is received, in.readLine();
begins to return null, over and over again. This shouldn't happen; it should hang until it receives data. It seems unlikely that the server has closed out on me, it hasn't sent the </stream:stream>
to signal the end of an XMPP stream. Any ideas on what could be the problem?
Thank you for your help!
Keep in mind that the XMPP connection can and will give you incomplete stanzas, or multiple stanzas in a single read. If your COMET connection expects that what you're passing it is well-formed XML, you will have issues. As well, XMPP is not newline-terminated, so I'm not sure why you expect readLine()
to be terribly useful.
Next, are you doing synchronous I/O on two different sockets on the same thread? Sounds like a recipe for a deadlock. If you insist on going down this path (instead of just using BOSH), I'd strongly urge you to use NIO, instead of your sleep hack.
精彩评论