We are developing a server application in Java (1.6), which is a transactional server that listens for connections through TCP sockets. Each new connection creates a new thread, which is maintained until the connection is closed. Each client sends transactions to the server to be processed, and then the response is sent back to the client.
It works great. The problem arises when we want to send many asynchronous transactions (or messages) through the same socket. I wrote a small application that sends 1000 transactions with an interval of 10 ms between each transaction. The application is asynchronous, so messages are sent and responses are in the middle.
This is the code from the part that processes incoming messages and sends them to another processed component (this component has a thread pool):
public void run() { ... ... socketBuf = new BufferedInputStream(input); baos = new ByteArrayOutputStream(); while ((bytes_read = socketBuf.read(buffer)) != -1) { if (bytes_read < 0) { log.error("Tried to read from socket, read() returned < 0, Closing socket."); return; } baos.write(buffer, 0, bytes_read); break; } if (bytes_read >= 0) { baos.flush(); byte data[] = baos.toByteArray(); if (data.length > 0) { GWTranData tData = posMessage.decode(data, false); if (tData.getMessageType() > 0) { // Send to the Pre-Online Manager to be processed PreOnlineJob newJob = new PreOnlineJob(tData); newJob.addJobStatusListener(this); GWServer.getPreOnlineInstance().addJob(newJob); } } } else { clientSocket.close(); break; } } while(true); }
The problem that we encountered when sending many transactions in a short time is that some messages are lost and do not reach the server. Through in-depth analysis, we found that when messages are sent too fast, there are more than one message in the buffer, so the data [] has two or more messages, but only one will be executed. The size of the sent message is 200 bytes, so the 512 buffer is larger than enough.
Are there any problems with how I implemented the socket? Is there a better way?
Thanks guys.
source share