Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:41:12+00:00 2026-06-07T19:41:12+00:00

I have a small server application which receives connections from multiple clients. The clients

  • 0

I have a small server application which receives connections from multiple clients.
The clients will connect, send a message and disconnect, there is no response sent back.

I’m using a ServerSocketChannel to listen for connections.
I get notified of new connections using a selector, When this happens I register the SocketChannel with the selector for SelectionKey.OP_READ

The part I’m unsure of is when I get notified data is available for reading.
My SocketChannels are in nonblocking mode.
I call channel.read(buffer) on the SocketChannel to read into the byte buffer
From the javadoc for read, I should repeatadly call this until I get a -1 indicating the end of the stream. However, I’m not sure if I should be waiting for the selector to notify me again before calling read again.
That is:

  • I get notified by the selector that data is available.
  • I call read
  • Should I then call read again until -1 is returned or should I let the selector notify me then call read again

Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:41:14+00:00Added an answer on June 7, 2026 at 7:41 pm

    From the javadoc for read, I should repeatadly call this until I get a -1 indicating the end of the stream.

    It doesn’t say any such thing.

    If you’re in non-blocking mode, you can read until it returns -1 or zero. If it returns zero, there is no more data pending in your socket receive buffer, so you should return to the select loop. If it returns -1, that’s it, finished, done, finito, close the socket channel and proceed accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small client server application in which i wish to send an
I have to develop a small application that will connect to a Linux server
I have a small application which doesn't seem to work. Please help. Server: var
Hi i have written a server application which accepts a name from the client
We have a small console application (under 200kb) that will be distributed to clients
I have a server application that needs to find and exchange small amounts of
I have a small app that downloads some files from a remote (HTTP) server
I have written a small client server socket application. It is a proof of
I have an application running in IIS which connects to a SQL Server 2008
I have written a small custom web server application in C running on Linux.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.