This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Serversocket not able to read data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Serversocket not able to read data " Watch "Serversocket not able to read data " New topic
Author

Serversocket not able to read data

avishek bose
Greenhorn

Joined: Oct 12, 2011
Posts: 15
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 = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind( new InetSocketAddress(port));
serverSocketChannel.configureBlocking(true);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

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

SelectionKey key = keyIter.next();
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) key.channel();
ByteBuffer buf = (ByteBuffer)key.attachment();

if channel is connected then
bytesRead = clntChan.read(buf);

here i am getting the bytesRead value as 0


while the clients are continously writing ascii strings to the port

regards
 
wood burning stoves
 
subject: Serversocket not able to read data
 
Similar Threads
NIO Socket weirdnes in Solaris 2.10
socket programming : connect server to client
java.net.BindException: Address already in use
server can not accept all the clients connection request
ServerSocketChannel.accept() seems to create a random port to send data to?