C结构帮助 - 我丢失/ BSD套接字

时间:2012-10-23 11:25:00

标签: c sockets struct

我是C的新手,我有点失落。

那我在做什么?

  • 多线程Web服务器。

我想做什么?

  • 将包含数据的结构传递给线程函数。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <netdb.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip_fw.h>
#include <pthread.h>
#define MYPORT 8080
#define BACKLOG 36600

struct thread_data {
    int new_fd;
    struct address_data adata;
};
struct address_data {
    unsigned long s_addr;
};
//---------------------------httpRequest Method------------------------ 
void* httpRequest(void* data) 
{ 
    struct thread_data me;
    me = *((struct thread_data*)data);

   printf("struct ip: %s\n", inet_ntoa(me.adata));
   printf("struct fd: %d\n", me.new_fd);

   pthread_exit(NULL);  //Existing from the threads. 
}
//**************************************************************************************************************** 
//Main Method------------------------------------------------------------------
int main (void) 
{ 
    int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd 
    struct sockaddr_in my_addr;    // my address information 
    struct sockaddr_in their_addr; // connector's address information 
    struct thread_data td;
    int sin_size; 
    struct sigaction sa; 
    int yes=1;
    pthread_t p_thread[3000]; 
    int thr_id,i=0;   

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
        perror("socket"); 
        exit(1); 
    }

    if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { 
        perror("setsockopt"); 
        exit(1); 
    }

    // bzero((char *) &my_addr, sizeof(my_addr)); 
    my_addr.sin_family = AF_INET;         // host byte order 
    my_addr.sin_port = htons(MYPORT);     // short, network byte order 
    my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP 
    memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) { 
        perror("bind"); 
        exit(1); 
    }

    if (listen(sockfd, BACKLOG) == -1) { 
        perror("listen"); 
        exit(1); 
    }
    sa.sa_handler = sigchld_handler; // read all dead processes 
    sigemptyset(&sa.sa_mask); 
    sa.sa_flags = SA_RESTART; 
    if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
        perror("sigaction"); 
        exit(1); 
    }


    while(1) 
    {  // main accept() loop 
        sin_size = sizeof(struct sockaddr_in); 
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size)) == -1) { 
            perror("accept"); 
            continue; 
        } 
    printf("Got connection from %s\n",inet_ntoa(their_addr.sin_addr));

    td.new_fd = (int *)&new_fd;
    td.adata = their_addr.sin_addr;

        //Creates threads.
        thr_id = pthread_create(&p_thread[i++],NULL,httpRequest,(void*)&td); 
   }
    return 0; 
}

当我获得连接时,程序意外退出。

正如您所看到的,我已将 thread_data struct 作为 sockaddr_in 的模型。如果你能指出我出错的地方,那就太棒了。提前谢谢。

1 个答案:

答案 0 :(得分:1)

可能还有其他错误。但至少:

int main (void) 
{ 
    struct thread_data td;

    while(1) 
    {
       thr_id = pthread_create(&p_thread[i++],NULL,httpRequest,(void*)&td); 
    }
    return 0; 
}

你只有一份td。然而,你将同一个传递给多个线程。因此,在第二个请求中,您将覆盖您为第一个线程提供的相同td。