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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:50:45+00:00 2026-06-08T13:50:45+00:00

I would like to know why i am unable to send data when using

  • 0

I would like to know why i am unable to send data when using threads with sockets.

I was wondering why when i send using the first method(without threads), it works, but using the second method, it fails to send data.

This is my declaration :

int main(int argc, char *argv[]) 
{
    int sd, rc, i;
    struct sockaddr_in cliAddr, remoteServAddr;
    struct hostent *h;
    h = gethostbyname(argv[1]);
    inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));

    remoteServAddr.sin_family = h->h_addrtype;
    memcpy((char *) &remoteServAddr.sin_addr.s_addr,h->h_addr_list[0], h->h_length);
    remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);

    /* socket creation */
    sd = socket(AF_INET,SOCK_DGRAM,0);

    /* bind any port */
    cliAddr.sin_family = AF_INET;
    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    cliAddr.sin_port = htons(0);

    rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));

    socklen_t remoteServLen = sizeof(remoteServAddr);
    //this is my own class to store the pointers to the following variables
    clientInfo ci(&sd,&rc,&remoteServLen,&remoteServAddr,&cliAddr);

/FIRST METHOD/

    char *data;
    char input[MAX_MSG];

    std::cout << "Enter message to send (type /q to quit) : ";  
    std::cin >> input;
    data = input;

    rc = sendto(*(ci.getSd()), data, strlen(data)+1, 0,(struct sockaddr *) ci.getCliAddr(),*(ci.getCliLen()));

/FIRST METHOD/

/SECOND METHOD/

    pthread_t thread[2];
    int status;

    status = pthread_create (&thread[1], NULL, sendFunction,&ci);

/SECOND METHOD/

}

/This is my thread method/

void* sendFunction (void* temp)
{

    int status;
    char *data;
    char input[MAX_MSG];
    int rc;

    clientInfo *ci;
    ci = (clientInfo*)temp;

    status = pthread_detach (pthread_self ());

    while(1)
    {
        std::cout << "Enter message to send (type /q to quit) : ";  
        std::cin >> input;
        data = input;


        rc = sendto(*(ci->getSd()), data, strlen(data)+1, 0,(struct sockaddr *) ci->getCliAddr(),*(ci->getCliLen()));

        if(rc<0) 
        {
            printf("Cannot send data %s \n",data);
        }
    }//end of while loop

    return NULL;
}//end of sendFunction method

🙂 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-08T13:50:47+00:00Added an answer on June 8, 2026 at 1:50 pm

    It would have been nice if you have provided your target platform/environment. I am assuming you are using Linux, and so my best guess would be that your thread actually terminates before it has any chance to send anything. You may wonder why so because you have detached it. Well, it is a very common mistake of those who have read some abstract articles/books on POSIX threads. The truth is – it doesn’t work like that. This is explicitly stated in Linux’s man page for pthread_detach():

    The detached attribute merely determines the behavior of the system when the
    thread terminates; it does not prevent the thread from being terminated if the
    process terminates using exit(3) (or equivalently, if the main thread
    returns).
    

    I’d recommend you actually join() “sending” thread from the main thread and see if that makes it work. If not – get back with an update. Also, it never hurts to use a debugger – it will most likely show what is wrong right away.

    On a side note, threads is not a solution for C10K problem. For example, creating two threads just to have two (blocking) senders is not the way to go at all. For that purpose, operating systems provide asynchronous notification mechanisms. For example, Linux got epoll, FreeBSD (and OS X :-)) are built upon kqueue. There are also poll, select, port completion etc. There are also APIs that wrap those mechanisms around (for portability and/or simplification reasons). libevent and Boost.Asio are the most popular choices.

    Hope it helps.

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

Sidebar

Related Questions

i would like know some reference. I know i can googling it. but prefer
I would like to know how exactly model binding works in ASP.NET MVC3. Since
I'm using SQL Server 2005 and would like to know how I can get
Error : Unable to add cookies, header already sent. I would like to know
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
Would like to know how to integarate cruise control with maven? Cruise Control comes
Would like to know how to hide an div after a set of css3
I would like to know what code to use to convert a double[] array
I would like to know, how can we change the SRC url for IFRAME

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.