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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:53:22+00:00 2026-06-05T13:53:22+00:00

I am writing a simple socket daemon witch listens to a port and reads

  • 0

I am writing a simple socket daemon witch listens to a port and reads the incoming data. It works fine until i choose to disconnect a client from the server…then it enters in a infinte loop recv() returns the last packet never gets to -1. My question is how can i detect that the client had been disconnected and close the thread/ socket el

My thread is as follows :

void * SocketHandler(void* lp){
    int * csock = (int*)lp;
    int test = 0;

    char buffer[1024];
    int  buffer_len = 1024;
    int  bytecount,ierr;
    memset(buffer,0,buffer_len);
    while (test == 0)
    {
    if ((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){

        close(csock);
        free(csock);
        test++;
        return 0;
    }
    else
    {
        syslog(LOG_NOTICE,"%s",buffer);

    }
    }
    return 0;
};
  • 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-05T13:53:25+00:00Added an answer on June 5, 2026 at 1:53 pm

    A cleanly closed socket will end up in a ZERO read, while a broken connection is an error state returning -1. You need to catch the 0 return of your recv.

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

Sidebar

Related Questions

Hi' I'm writing a simple http port forwarder. I read data from port 80,
I'm writing a simple socket server and I want to keep track of the
I'm writing a simple socket client which uses only one socket. So I figured
I have a simple file transfer socket program where one socket sends file data
I'm currently writing a proxy application that reads from one socket and writes on
I'm writing a simple web server,code snippet: ServerSocket server = new ServerSocket(80); Socket client=server.accept();
Basically I am writing a simple program using the boost socket library... I have
I am writing a simple WebSocket application with socketio . There are 2 socket
I'm writing a very simple server that loops forever until Ctrl-C is pressed. I'd
I am writing simple Linux module mod.c. When I compile mod.c file, it creates

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.