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

  • Home
  • SEARCH
  • 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 8403021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:13:59+00:00 2026-06-09T22:13:59+00:00

main() { unsigned int newfd; … ….. while (1) { printf(Waiting for connection\n); addrlen

  • 0
 main() {
    unsigned int newfd;
    ...
    .....
    while (1) {        
      printf("Waiting for connection\n");
      addrlen = sizeof (clientaddr);
      if ((newfd = accept(listener, (struct sockaddr *) &clientaddr, &addrlen)) < 0) {
          perror("Server-accept() error lol!");
          break;
      }
      printf("New connection from %s on socket %u\n", inet_ntoa(clientaddr.sin_addr), newfd);
      pthread_create(&threads[i++], NULL, (void*)fileTransfer_Worker, &newfd); 
      sleep(1);
   }

}

void* fileTransfer_Worker(void *desc) {
     unsigned int sock = *(unsigned int *) desc;
     printf("Waiting for data in sock %d %u\n", sock, pthread_self());
}

Output

Waiting for connection
New connection from 192.168.4.57 on socket 4
Waiting for connection
New connection from 192.168.4.57 on socket 5
Waiting for connection
Waiting for data in sock 4 3076578160
Waiting for data in sock 5 3068189552
New connection from 192.168.4.57 on socket 6
Waiting for connection
Waiting for data in sock 6 3059800944
New connection from 192.168.4.57 on socket 7
Waiting for connection
New connection from 192.168.4.57 on socket 8
Waiting for connection
New connection from 192.168.4.57 on socket 9
Waiting for connection
Waiting for data in sock 8 3051412336
New connection from 192.168.4.57 on socket 10
Waiting for data in sock 9 3034635120
Waiting for data in sock 10 3043023728
Waiting for connection
New connection from 192.168.4.57 on socket 11
Waiting for connection
New connection from 192.168.4.57 on socket 12
Waiting for connection
New connection from 192.168.4.57 on socket 13
Waiting for connection
Waiting for data in sock 13 3001080688
Waiting for data in sock 13 3026246512
Waiting for data in sock 13 3017857904
Waiting for data in sock 13 3009469296

If you look in to the output, you can notice sock 13 was displayed 4 times which should actually be for socket 7, 11, 12 and 13.

The behavior changes every time when I invoke simultaneously more client connections.

If I add sleep(1) after pthread_create() then I was able to see the expected behavior. Is the sleep after pthread_create is MUST? or how can I resolve this issue without using sleep?

help me on this. Thanks in advance

  • 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-09T22:14:01+00:00Added an answer on June 9, 2026 at 10:14 pm
    pthread_create(&threads[i++], NULL, (void*)fileTransfer_Worker, &newfd);
                                                                    ^^^^^^
    

    The problem is that you’re passing the address of a local variable to pthread_create. By the time the thread starts using it, main may have already changed it. So if the descriptor is 10 when you call pthread_create, when the thread actually starts using main already had the chance of changing it.

    You have at least 2 options to fix this:

    • Pass a separate object for each thread (perhaps one which you malloc)
    • Stuff the integer in the argument, as if it were a void *, then cast back to int in the thread function

    Although the second option is widely used, it’s also non-portable.

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

Sidebar

Related Questions

int main() { unsigned int b; signed int a; char z=-1; b=z; a=z; printf(%d
Why is this code throwing up a SIGSEGV : int main() { unsigned long
I want to know how malloc and free work. int main() { unsigned char
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
Here is very simple code, #include <iostream> using namespace std; int main() { unsigned
void foo(int,int) {} void foo(int ,float) {} void foo(float,int) {} void main() { unsigned
I have this simple program #include <stdio.h> int main(void) { unsigned int a =
Plain C, on Windows 7 & HP machine. int main(void) { unsigned int a

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.