0

i have a kind of service architecture in my server, listening on a TCP-IP-socked.

messageType = protocolHandler.getMessageType();
ITaskProtocol serviceProtocol = serverServices.get(messageType);
if (p != null) {
    String result = serviceProtocol.handleTask(protocolHandler);
}

i am storing a protocol for a specific task in a hash-map. My problem is, that the protocol that should handle the task is not finding any bytes in the inputStream. the inputStream and socked is inside the "protocolHandler".

example:

public class ProtocolHandler implements ITaskProtocolHandler {
    @Override
    public int getMessageType() throws IOException {
        return new DataInputStream(stream).readInt();
    }

However, i can see (because of debugging) that the message was sended. and also the protocol(service) was found in the map and the "handleTask(...)" method of the service was invoked. However, the service is getting no message because the bytes are lost and the protocols are waiting for each other.

My guess is that the searching for the service needs too long and the message is lost meanwhile.

Important Info: Sometimes it is working sometimes not. the client and server are running on the same pc, it's probably a threads issue.

client protocol:

clientWorker.send(ServerProtocol.MessageRequestType.JoinTopicRequest.getRequestNumber());
clientWorker.send("some xml-message");

server service-protocol:

public String handleTask(ITaskProtocolHandler protocolHandler) throws Exception {
    Message m = protocolHandler.getMessage());

whats wrong in the architecture?

thanks a lot!

headgrowe
  • 529
  • 1
  • 5
  • 17

2 Answers2

1

This is a wild guess but It might be your problem. Is your thread blocked and waiting for data?

If so, then you need to fix the order in which you are asking for the streams on your socket, you need to get FIRST the inputStream from the socket, and then ask for the outputStream, otherwise you'll end up on a thread blocked and waiting for data.

I hope this solves your problem.

Please refer to this post: Stackoverflow java socket question for a complete explanation.

Community
  • 1
  • 1
  • there are too important threads. one is the client and one the server. each is blocking if waiting for data. the streams are stored like this in the server `public ProtocolHandler(Socket socket) throws IOException { this.socket = socket; this.outputStream = socket.getOutputStream(); this.inputStream = socket.getInputStream(); }` – headgrowe May 03 '12 at 15:35
  • You must match the orders as explained on the link I added, good luck with that :) – Juan Alberto López Cavallotti May 03 '12 at 15:37
  • the order is right! i found the problem! it was a reading prob :( – headgrowe May 03 '12 at 15:57
0

problem solved! i had a reading prob... "totalRead += nRead;" was missing....

    int size = readInt();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead = 0;
    int totalRead = 0;
    byte[] data = new byte[1024];

    while (totalRead < size) {
        int read;
        if (data.length <= (size - totalRead))
            read = data.length;
        else
            read = (size - totalRead);
        if ((nRead = inputStream.read(data, 0, read)) == -1)
            throw new IOException("End of stream");
        buffer.write(data, 0, nRead);
        totalRead += nRead;
    }
headgrowe
  • 529
  • 1
  • 5
  • 17