把客户端产生进程数超过1000,下面的例子就崩溃:
void* conn(void *ip)
{
int sockfd, num; /* files descriptors */
char buf[BUFLEN]; /* buf will store received text */
struct hostent *he; /* structure that will get information about remote host */
struct sockaddr_in server;
char str[BUFLEN];
int idx = 0;
int err_num = 0;
static int proc_id = 0;
printf("\n\n\nid=%d, ip=%s \n", pthread_self(), (char *)ip);
if((he=gethostbyname((char *)ip)) == NULL)
{
printf("gethostbyname() error\n");
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket() error\n");
exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr = *((struct in_addr *)he->h_addr); //就崩溃到这句上;
把这些代码改为如下,就没有崩溃了:
void* conn(void *ip)
{
int sockfd, num; /* files descriptors */
char buf[BUFLEN]; /* buf will store received text */
struct hostent *he; /* structure that will get information about remote host */
struct sockaddr_in server;
char str[BUFLEN];
int idx = 0;
int err_num = 0;
static int proc_id = 0;
printf("\n\n\nid=%d, ip=%s \n", pthread_self(), (char *)ip);
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket() error\n");
exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr("127.0.0.1");