Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serversocket not able to read data

avishek bose
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have one server socket that will read the data from multiple clients which are writing the data to the server in ascii format at a particular port, but currently i am not able to read the data on the port.
the server socket that can accept many client connection i.e whenever a connection is available it will accept it and reads the ascii data from the client in that port.
The issue is that I am able to accept all the incoming connection but not able to read the ascii data writtern to the port.

I have implemented the ServerSocketChannel and Selector where once the ServerSocketChannel is setup I am iterating over the selection keys
ServerSocketChannel configuration

selector =;
serverSocketChannel =;
serverSocketChannel.socket().bind( new InetSocketAddress(port));
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
while (keyIter.hasNext()) {

SelectionKey key =;
now check if key.isAcceptable() then accept connection , if key.isReadable() then read the data.

the reading logic is :
int bytesRead = -1;
SocketChannel clntChan = (SocketChannel);
ByteBuffer buf = (ByteBuffer)key.attachment();

if channel is connected then
bytesRead =;

here i am getting the bytesRead value as 0

while the clients are continously writing ascii strings to the port

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic