I am writing a server part of a large Java application for communicating with client over TCP/IP using Java sockets. The client (written in PHP) connects to the server, sends a query in XML format, then the server sends back a response. The query-response can repeat multiple times in a single connection.
The server side is quite simple. It’s supposed to allow multiple client connections, so there’s a thread listening and spawning a session for each accepted connection. The session consists of an object containing two LinkedBlockingQueues for transmit and receive, two threads for transmitting and receiving messages using those queues and a processing thread.
The problem is that any messages are actually transmited only after the socket is closed. Response messages find their way into the message queue and PrintStream.println() method without a problem, but wireshark reports the transmission only when the client closes the connection on its side. Creating PrintStream with auto flush enabled or using flush() don’t work. Closing the socket on the server side does not work too, the server still functions and receives messages.
Also with current implementation of the client receiving the queries on the server side works fine, the same goes with echo -e "test" | socat - TCP4:192.168.37.1:1337 from a local Linux virtual machine, but when I telnet to the server and try to send something then server does not receive anything until I close the telnet client, the same problem as described above.
The relevant server code (the entire application is too big to paste everything and I’m working with a lot of other people’s code):
package Logic.XMLInterfaceForClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.HashSet;
import java.util.concurrent.LinkedBlockingQueue;
import Data.Config;
import Logic.Log;
public class ClientSession {
/**
* @author McMonster
*
*/
public class MessageTransmitter extends Thread {
private final Socket socket;
private final ClientSession parent;
private PrintStream out;
/**
* @param socket
* @param parent
*/
public MessageTransmitter(Socket socket, ClientSession parent) {
this.socket = socket;
this.parent = parent;
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
out = new PrintStream(socket.getOutputStream(), true);
while (!socket.isClosed()) {
try {
String msg = parent.transmit.take();
// System.out.println(msg);
out.println(msg);
out.flush();
}
catch(InterruptedException e) {
// INFO: purposefully left empty to suppress spurious
// wakeups
}
}
}
catch(IOException e) {
parent.fail(e);
}
}
}
/**
* @author McMonster
*
*/
public class MessageReceiver extends Thread {
private final Socket socket;
private final ClientSession parent;
private BufferedReader in;
/**
* @param socket
* @param parent
*/
public MessageReceiver(Socket socket, ClientSession parent) {
this.socket = socket;
this.parent = parent;
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (!socket.isClosed()) {
String message = "";
String line;
while ((line = in.readLine()) != null) {
message = message + line + "\n";
}
if(message != "") {
parent.receive.offer(message.toString());
}
}
}
catch(IOException e) {
parent.fail(e);
}
}
}
public final LinkedBlockingQueue<String> transmit = new LinkedBlockingQueue<>();
public final LinkedBlockingQueue<String> receive = new LinkedBlockingQueue<>();
private final XMLQueryHandler xqh;
private final Socket socket;
private String user = null;
private HashSet<String> privileges = null;
/**
* @param socket
* @param config
* @throws IOException
* @throws IllegalArgumentException
*/
public ClientSession(Socket socket, Config config)
throws IOException,
IllegalArgumentException {
// to avoid client session without the client
if(socket == null) throw new IllegalArgumentException("Socket can't be null.");
this.socket = socket;
// we do not need to keep track of the two following threads since I/O
// operations are currently blocking, closing the sockets will cause
// them to shut down
new MessageReceiver(socket, this).start();
new MessageTransmitter(socket, this).start();
xqh = new XMLQueryHandler(config, this);
xqh.start();
}
public void triggerTopologyRefresh() {
xqh.setRefresh(true);
}
public void closeSession() {
try {
xqh.setFinished(true);
socket.close();
}
catch(IOException e) {
e.printStackTrace();
Log.write(e.getMessage());
}
}
/**
* Used for reporting failures in any of the session processing threads.
* Handles logging of what happened and shuts down all session threads.
*
* @param t
* cause of the failure
*/
synchronized void fail(Throwable t) {
t.printStackTrace();
Log.write(t.getMessage());
closeSession();
}
synchronized boolean userLogin(String login, HashSet<String> privileges) {
boolean success = false;
if(!privileges.isEmpty()) {
user = login;
this.privileges = privileges;
success = true;
}
return success;
}
public synchronized boolean isLoggedIn() {
return user != null;
}
/**
* @return the privileges
*/
public HashSet<String> getPrivileges() {
return privileges;
}
}
I can understand why no messages are received at the server until the socket is closed – that seems to be as designed. in.readLine() will only return null when the end of the stream is reached which, with TCP socket stream, means when the socket is closed. If you want your readLine() loop to return before that, the code in the loop will have to detect the end of your message using whatever protocol you are using on top of TCP to define messages.