1

I am writing dummy server for my project to accept request from client and return 1 or more responses back. On the dummy server side, it gets the request from client, and find the matched message to send back. Below is piece of code of writing part.

for (String line : response) {
        ByteBuffer msgBuf = ByteBuffer.wrap(line.getBytes());
        if (!ccc.isConnected())
            ccc.connect(new InetSocketAddress("127.0.0.1", 50051));
        logger.info("Capacity of msgBuf is : " + msgBuf.capacity());
        ccc.write(msgBuf);
        logger.info("Seding out the response : " + line);
        msgBuf.rewind();
        Thread.sleep(2000);
    }

if sever found 2 data to send back, the ccc socket will write twice. I put Thread.sleep(2000) there, coz if I don't do this, the client side will get 2 data together. But I want the server send the data one by one.

user207421
  • 305,947
  • 44
  • 307
  • 483
Bmegod
  • 35
  • 7
  • TCP is not a messaging protocol. It is a byte stream. Your code is working correctly. NB `ServerSocketChannel` can't write anything, let alone a response, let alone more than one. Rewinding an object that is about to fall out of scope is futile. [tag:serversocket] is irrelevant. – user207421 Jun 16 '17 at 09:47

0 Answers0