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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:54:10+00:00 2026-05-27T14:54:10+00:00

I am trying to make a server which can send data to more than

  • 0

I am trying to make a server which can send data to more than one client for which as a client is connected it creates a thread which continuously send data to client as it recieves some control info from client side.To stop transmission server receives another control request from client ,to receive request to stop there is another thread created by data sending thread.
For single client my code is working finely but for more than one client it only send data to newly arrived client.
Can you suggest me some idea to make it work.

     for(i=0;i<N;i++){  // main accept() loop
             sin_size = sizeof client_addr;
             connected[i] = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
             if (connected[i] == -1) {
                perror("accept");
                continue;
             }
             inet_ntop(client_addr.ss_family,get_in_addr((struct sockaddr *)&client_addr),s1[i], sizeof s1[i]);
             printf("server: got connection from %s\n%d waiting for data request from client:.......\n", s1[i],connected[i]);
             fflush(stdout);

             ta1->sockid=connected[i];
             ta1->count=i;

             if(pthread_create(&tid1[i],NULL,service,(void*)ta1)){
                 fprintf(stderr,"error in creating tid1");
                 exit(1);
                }

            }

    for(i=0;i<N;i++){
             pthread_join(tid1[i],NULL);
                    }
    close(connected[1]);
    return 0;
    }//end of main


    void* service(void* argu)
     {
      struct thargu *ta=(struct thargu *)argu;
      char control;

       printf("coming %d\n",ta->sockid);
      if(recv(ta->sockid,&control,1,0)==-1)
       {perror("error in recv of service");exit(1);}
      printf("recv control at %dsockid\n",ta->sockid);
      if(pthread_create(&tid2[ta->count],NULL,stoptx,(void*)ta)==-1)
      {perror("error in creating tid2");exit(1);}

      while(1)
      {
       if(control=='a')
        {
   pthread_mutex_lock(&data_lock);

   if(send(ta->sockid,&t1,sizeof(struct transfer),0)==-1)
     {perror("error in send of service");exit(1);}

   control=v;
   v='\0';

   pthread_mutex_unlock(&data_lock);
   printf("data to %d\n",ta->sockid);
   fflush(stdout);
   sleep(4);
  }

if((control=='b') && (ta->count==k))
  {k=200; break;}

     control='a';
    }//end of while
      printf("\ncoming\n");
     pthread_join(tid2[ta->count],NULL);
     pthread_exit(NULL);
    }//end of service

    void* stoptx(void* sock)
        {
        struct thargu *ta=(struct thargu *)sock;
        char b;

        pthread_mutex_lock(&data_lock2);

       if(recv(ta->sockid,&v,1,0)==-1)
       {perror("error in recv of stoptx");return 0;}
        k=ta->count;

        pthread_mutex_unlock(&data_lock2);
          printf("inside stoptx %d\n",ta->sockid);
         pthread_exit(NULL);
         }//end of stoptx
  • 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-27T14:54:10+00:00Added an answer on May 27, 2026 at 2:54 pm

    Since you don’t show all the code, I have to guess… And my guess is that it’s because you only have one ta1 in the main function. It is a pointer to some structure, and you send the pointer to the thread. But since there is only one pointer, that you send to both threads, then the variable ta in both threads point to the same structure. So when the second connection is made, the information for the first thread is overwritten with the information for the second.

    To solve this, you need to make an array, like the connected array.

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

Sidebar

Related Questions

I'm trying to make a program (client) which kan send a message to a
I'm trying to make a client/server Java App. Both client and server will be
So I'm trying to make a client for my server in C#, that accepts
I am trying to make our SQL Server Integration Services packages as portable as
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make a connection to a SQL Server Express DB on localhost,
I'm trying to make a dump of a MySQL table on the server and
Delphi 2010, dbExpress, and SQL Server 2005 DB I am trying to make a
I'm trying for a week to make my XSP server work with simple static
I am trying to make an app that will pass data between two servers

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.