1

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! :D

4

1 に答える 1

4

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.

于 2012-07-26T02:52:05.547 に答える