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 6065363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:22:54+00:00 2026-05-23T09:22:54+00:00

I am using a loop to read message out from a c Berkeley socket

  • 0

I am using a loop to read message out from a c Berkeley socket but I am not able to detect when the socket is disconnected so I would accept a new connection. please help

while(true) {
            bzero(buffer,256);
            n = read(newsockfd,buffer,255);
            printf("%s\n",buffer);        
}
  • 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-05-23T09:22:55+00:00Added an answer on May 23, 2026 at 9:22 am

    The only way you can detect that a socket is connected is by writing to it.

    Getting a error on read()/recv() will indicate that the connection is broken, but not getting an error when reading doesn’t mean that the connection is up.

    You may be interested in reading this:
    http://lkml.indiana.edu/hypermail/linux/kernel/0106.1/1154.html

    In addition, using TCP Keep Alive may help distinguish between inactive and broken connections (by sending something at regular intervals even if there’s no data to be sent by the application).

    (EDIT: Removed incorrect sentence as pointed out by @Damon, thanks.)

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

Sidebar

Related Questions

I have a string with possible command line arguments (using an Read-Eval-Print-Loop program) and
I just designed a simple For Loop using window form application. I would like
first time posting here. I'm using a loop to create 3 buttons, but my
I am using a Socket to receive data via TCP, and TextReader.ReadLine to read
I've read similar questions on SO but can't seem to figure out this issue,
I want to be able to read and process messages from MSMQ. The queues
The following code sends messages from child processes to their parents using socket pairs.
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I am using XmlReader in .NET to parse an XML file using a loop:
I am running a windows service and using a loop and Thread.Sleep to repeat

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.