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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:34:25+00:00 2026-06-17T22:34:25+00:00

The following loop is inside the main of the program. It accepts an incoming

  • 0

The following loop is inside the main of the program. It accepts an incoming connection, and has a thread do work with it.

The problem is, as soon as any thread terminates, it terminates the entire program. Here’s the code:

#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#define BUFLEN 1500
#define MAXCON 30

char *returnTimeDate(int inputchoice);
void readWriteToClient(int inputconnfd);

int main(){

    int backlog = 10;

    int fd;
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
        // Error: unable to create socket
    }

    struct sockaddr_in cliaddr;
    socklen_t cliaddrlen = sizeof(cliaddr);

    struct sockaddr_in addr;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(5001);

    if (bind(fd, (struct sockaddr *) &addr, (socklen_t) sizeof(addr)) == -1) {
        fprintf(stderr,"Bind Didn't Work\n");
    }

    if (listen(fd, backlog) == -1) {
        fprintf(stderr,"Listen Didn't Work\n");
    }

    pthread_t *threadsArray = (pthread_t *)calloc(MAXCON, sizeof(pthread_t));
    pthread_t *threadPtr = threadsArray;

    int k;
    for(k = 0; k < MAXCON; k++){
        fprintf(stderr,"Make %d\n",k);
        int connfd;
        connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen);
        if (connfd == -1) {
            fprintf(stderr,"Accept Didn't Work\n");
        }
        fprintf(stderr,"Waited\n",k);
        pthread_create( &threadPtr, NULL, readWriteToClient, (void *)connfd);
        threadPtr++;
    }

    pthread_t *threadPtrJoin = threadsArray;

    for(k = 0; k < MAXCON; k++){
        fprintf(stderr,"Join %d\n",k);
        pthread_join( *threadPtrJoin, NULL);
        threadPtrJoin++;
    }

/*  readWriteToClient(connfd);*/

    close(fd);

    return 0;

}

void readWriteToClient(int inputconnfd){

    int connfd = inputconnfd;

    while(1){

        char *dateString = "DATE\r\n";
        char *timeString = "TIME\r\n";
        char *endString = "end";

        char *bufferTime = returnTimeDate(0);
        char *bufferDate = returnTimeDate(1);

        ssize_t i;
        ssize_t rcount;
        char buf[BUFLEN];
        char *toReturn = (char *)malloc(BUFLEN*sizeof(char));
        rcount = read(connfd, buf, BUFLEN);
        if((strcmp (buf, dateString)) == 0){
            strcpy(toReturn, bufferDate);
        }
        if((strcmp (buf, timeString)) == 0){
            strcpy(toReturn, bufferTime);
        }
        if((strcmp (buf, endString)) == 0){
            goto outside;
        }
        if (rcount == -1) {
            // Error has occurred
            printf("Error: rcount -1");
        }

/*      fprintf(stderr,"I have received = %s\n",buf);*/

        if (write(connfd, toReturn, BUFLEN) == -1) {
            fprintf(stderr,"I didn't write = %s\n",buf);    
        }
    }

    outside: return;

}

char *returnTimeDate(int inputchoice){

    time_t timer;
    char *bufferTimee = (char *)malloc(25*sizeof(char));
    char *bufferDatee = (char *)malloc(25*sizeof(char));
    struct tm* tm_info;
    time(&timer);
    tm_info = localtime(&timer);
    strftime(bufferTimee, 25, "%H:%M:%S\n\0", tm_info);
    strftime(bufferDatee, 25, "%d:%m:%Y\n\0", tm_info);

    if(inputchoice == 0){
        return bufferTimee;
    }else{
        return bufferDatee;
    }

}

Why is it doing this?

  • 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-17T22:34:26+00:00Added an answer on June 17, 2026 at 10:34 pm

    There are a few things that are very wrong with your usage of pthreads.

    First of all, I think you meant this:

    int st = pthread_create(&threadPtr[k], NULL, readWriteToClient, (void *)connfd);
    if (st != 0) {
        /* handle error */
    }
    

    Note the following:

    • We’re storing the return value of pthread_create() in st and handling its error case.
    • We’re passing &threadPtr[k] (of type pthread_t *), not &threadPtr (of type pthread_t **). This was likely the cause of your issue.

    But one of the main issues I have with your code, is that you are invoking undefined behavior by passing readWriteToClient to pthread_create(). The pthread_create prototype is as follow:

    int pthread_create(pthread_t *restrict,
                       const pthread_attr_t *restrict,
                       void *(*start_routine)(void*), 
                       void *restrict arg);
    

    Even if you change &threadPtr to &threadPtr[k], you’d be calling it as follow:

    int pthread_create(pthread_t *restrict,
                       const pthread_attr_t *restrict,
                       void (*start_routine)(int),      // oops!
                       void *restrict arg);
    

    So, the pthread_create() accepts a pointer to a function of one type, but you’re passing it a pointer to a function of another type (C11, 6.7.6.3/15, emphasis mine):

    For two function types to be compatible, both shall specify compatible return types.
    Moreover, the parameter type lists, if both are present, shall agree in the number of
    parameters and in use of the ellipsis terminator; corresponding parameters shall have
    compatible types
    . […]

    The pointer to the function is implicitly converted, and your function is called anyway, but this is illegal, as per the standard (C11, 6.3.2.3/8, emphasis mine):

    A pointer to a function of one type may be converted to a pointer to a
    function of another type and back again; the result shall compare
    equal to the original pointer. If a converted pointer is used to call
    a function whose type is not compatible with the referenced type, the
    behavior is undefined
    .

    Since you are invoking undefined behavior, there’s no telling how the code will behave after you do.

    Also, keep in mind that converting an int to a void * and back is not guarantee to happen without loss of information (it’s implementation-defined), so be careful with that.

    There are a few other errors in your code that you should see if you compile with warnings enabled, something you should always do.

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

Sidebar

Related Questions

I have the following loop inside an function init(); which is executed onload, I
I have the following code inside a loop: Range(N & i + 1).Formula =
When I try to run the following SQL snippet inside a cursor loop, set
The following program has no importance of its own. It just counts the number
I'm getting two problem when I call the recursive function inside a loop. Consider
I am using the following while loop inside an foreach-loop to get categories from
I wrote the following loop since I have to repeat the same/similar code 25
Say I have the following loop: i = 0 l = [0, 1, 2,
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {
I have the following loop... for ($i = 1; $i <= 10; $i++) {

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.