user-0 提问: C语言socket programming第一次运行function成功，第二次调用报segmentation fault

回复:
user: user-0
content: 问题是GDB查不出来，因为第一次call function时没问题，第二次call才出错。。

user: user-1
content: gdb是调试段错误的神器

user: user-2
content: 进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈，内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件，参考“ulimit -c core文件最大块大小”命令

user: user-0
content: 代码太长了也看不了，我大概知道问题的原因就是socket，大侠请ctr＋f找 else if(!strcmp(packet.option, "send")) { 和 else if(!strcmp(packet.option, "help")) { 看一下这两个功能的socket写法有什么问题，前者是用来接发送两个client之间的信息，完全没问题， 但是第二个help只能成功调用一次，第二次调用就会segmentation fault，其他例如第一次调用chat后再调用help、quit之类的也会立刻挂掉，问题出现在我的socket写法上，希望大神能指点下

user: user-0
content: else if(!strcmp(packet.option, "allup")){                     fclose(tfp);                     bzero(buffer, BUFFSIZE);                 }             }             // Send to partner             j = (threadinfo.sockfd == channelmap[chptr][0])?1:0;             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "fileout");             strcpy(spacket.buff, file_name);             // Send download request             sent = send(channelmap[chptr][j], (void *)&spacket, sizeof(struct PACKET), 0);             channelmap[chptr][2]+=sent;             if((len = recv(channelmap[chptr][j],  (void *)&spacket, sizeof(struct PACKET), 0))>0){                 if(!strcmp(packet.option, "download")){                     if((tfp=fopen(file_name, "rb"))==NULL){                         error("ERROR cannot open file for read.");                         break;                     }                     while(!feof(tfp)){                         bzero(buffer, sizeof(buffer));                         readlen = fread(buffer, sizeof(char), BUFFSIZE, tfp);                         if(readlen > 0){                             strcpy(spacket.buff, buffer);                             sent = send(channelmap[chptr][j], (void *)&spacket, sizeof(struct PACKET), 0);                             channelmap[chptr][2]+=sent;                         }                     }                     fclose(tfp);                 }             }             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "alldown");             sent = send(channelmap[chptr][j], (void *)&spacket, sizeof(struct PACKET), 0);             channelmap[chptr][2]+=sent;               pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(packet.option, "flag")) {         	pthread_mutex_lock(&clientlist_mutex);             oid = packet.oid;             int i,j;             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "terminate");             strcpy(spacket.buff, "Either you or your partner is blocked.\n");             for(curr = client_list.head; curr != NULL; curr = curr->next) {                 if(oid == curr->threadinfo.uid) {                     curr->threadinfo.chatflag = 0;                       }             }             threadinfo.chatflag = 0;             for(j=0;j<2;j++){                 sent = send(channelmap[chanptr][j], (void *)&spacket, sizeof(struct PACKET), 0);                 channelmap[chanptr][2] += sent;             }             channelmap[chanptr][3] = 0;             blockId(oid); 			pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(packet.option, "send")) {             int i, j;             char target[ALIASLEN];             //for(i = 0; packet.buff[i] != ' '; i++); packet.buff[i++] = 0;             oid = packet.oid;         	chanptr = threadinfo.channel;             strcpy(target, packet.buff);             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));              pthread_mutex_lock(&clientlist_mutex);             j = (threadinfo.sockfd == channelmap[chanptr][0])?1:0;             strcpy(spacket.option, "msg");             strcpy(spacket.alias, packet.alias);             strcpy(spacket.buff, packet.buff);             sent = send(channelmap[chanptr][j], (void *)&spacket, sizeof(struct PACKET), 0);             channelmap[chanptr][2] += sent;             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(packet.option, "quit")) {             printf("[%d] %d %s quit chatting...\n", threadinfo.sockfd, threadinfo.uid, threadinfo.alias);             int i,j;             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "terminate");              pthread_mutex_lock(&clientlist_mutex);             for(curr = client_list.head; curr != NULL; curr = curr->next) {             	if(oid == curr->threadinfo.uid) {             		curr->threadinfo.chatflag = 0;		             	}             }             threadinfo.chatflag = 0;             for(j=0;j<2;j++){             	sent = send(channelmap[chanptr][j], (void *)&spacket, sizeof(struct PACKET), 0);             	channelmap[chanptr][2] += sent;             }             channelmap[chanptr][3] = 0;             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(packet.option, "exit")) {             printf("[%d] %d %s has disconnected...\n", threadinfo.sockfd, threadinfo.uid, threadinfo.alias);             int j;             pthread_mutex_lock(&clientlist_mutex);             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "terminate");              for(curr = client_list.head; curr != NULL; curr = curr->next) {             	if(oid == curr->threadinfo.uid) {             		curr->threadinfo.chatflag = 0;		             	}             }             threadinfo.chatflag = 0;             for(j=0;j<2;j++){             	sent = send(channelmap[chanptr][j], (void *)&spacket, sizeof(struct PACKET), 0);             	channelmap[chanptr][2] += sent;             }             channelmap[chanptr][3] = 0;             list_delete(&client_list, &threadinfo);             pthread_mutex_unlock(&clientlist_mutex);             break;         }         else {             fprintf(stderr, "Garbage data from [%d] %d %s...\n", threadinfo.sockfd, threadinfo.uid, threadinfo.alias);         }     }      // clean up     close(threadinfo.sockfd);      return NULL; }

user: user-0
content: //strcpy(threadinfo.alias, packet.alias);              pthread_mutex_lock(&clientlist_mutex);             list_insert(&client_list, &threadinfo);             pthread_mutex_unlock(&clientlist_mutex);             pthread_create(&threadinfo.thread_ID, NULL, client_handler, (void *)&threadinfo);         }     }       return 0; }   void *admin_handler(void *param) {     char option[OPTLEN];     int i;     while(scanf("%s", option)==1) {         if(!strcmp(option, "end")) {             // clean up              printf("Terminating server...\n");             shutdown_sockets(&client_list);             pthread_mutex_destroy(&clientlist_mutex);             close(sockfd);             exit(0);         }         else if(!strcmp(option, "stats")) {             pthread_mutex_lock(&clientlist_mutex);             showstat();             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(option, "throw")){             pthread_mutex_lock(&clientlist_mutex);         	printf("Specify the client uid:");         	scanf("%d", &i);         	if(!throwId(i, &client_list, 0)){         		fprintf(stderr, "ERROR throwing uid:%d", i);         	}             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(option, "block")){             pthread_mutex_lock(&clientlist_mutex);         	printf("Specify the client uid:");         	scanf("%d", &i);         	if(!blockId(i)){         		fprintf(stderr, "ERROR blocking uid:%d", i);         	}             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(option, "unblock")){             pthread_mutex_lock(&clientlist_mutex);         	printf("Specify the client uid:");         	scanf("%d", &i);         	if(!unblockId(i)){         		fprintf(stderr, "ERROR unblocking uid:%d", i);         	}             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(option, "blocklist")){         	printf("----- BLOCK LIST ------\n");             pthread_mutex_lock(&clientlist_mutex);         	for(i=0;i<BLOCKLIST;i++){         		if(getnamebyuid(blockList[i], &client_list)!=NULL){         			printf("Uid : [%d] %s\n", blockList[i], getnamebyuid(blockList[i], &client_list));	         		}         	}             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(option, "start")){         	// Listen         	if(serverOn == 0){         		if(listen(listenfd, 10) < 0){ 					fprintf(stderr, "ERROR on listen"); 				}         	}         }         else {             fprintf(stderr, "Unknown command: %s...\n", option);         }     }     return NULL; }   void *client_handler(void *fd) {     struct THREADINFO threadinfo = *(struct THREADINFO *)fd;     struct PACKET packet;     struct LLNODE *curr;     int bytes, sent;     int uid, oid, chanptr;     uid = threadinfo.uid;     oid = 0;     while(1) {         bytes = recv(threadinfo.sockfd, (void *)&packet, sizeof(struct PACKET), 0);         if(!bytes) {             fprintf(stderr, "Connection lost from [%d] %s...\n", threadinfo.sockfd, threadinfo.alias);             pthread_mutex_lock(&clientlist_mutex);             list_delete(&client_list, &threadinfo);             pthread_mutex_unlock(&clientlist_mutex);             break;         }         chanptr = threadinfo.channel;         printf("[%d] %s %d %s %s\n", threadinfo.sockfd, packet.uid, packet.option, packet.alias, packet.buff);         if(!strcmp(packet.option, "connect")) {             //printf("[%d]Set alias to %s\n", packet.uid, packet.alias);             pthread_mutex_lock(&clientlist_mutex);             for(curr = client_list.head; curr != NULL; curr = curr->next) {                 if(compare(&curr->threadinfo, &threadinfo) == 0) {                     strcpy(curr->threadinfo.alias, packet.alias);                     strcpy(threadinfo.alias, packet.alias);                     break;                 }             }             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "ackn");             strcpy(spacket.alias, packet.alias);             spacket.uid = uid;             spacket.oid = 0;             threadinfo.chatflag = 0;             sent = send(threadinfo.sockfd, (void *)&spacket, sizeof(struct PACKET), 0);             totalCount++;             pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(packet.option, "help")) {         	pthread_mutex_lock(&clientlist_mutex);             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "help");             spacket.uid = uid;             spacket.oid = oid;             strcpy(spacket.buff, "connect(to server)\nchat(find some one)\ntransfer(a file)\nflag(report misbehavior)\nsend(messages)\nexit(leave chat and disconnect)\n");             sent = send(threadinfo.sockfd, (void *)&spacket, sizeof(struct PACKET), 0);             pthread_mutex_unlock(&clientlist_mutex);         }            else if(!strcmp(packet.option, "send")) {             int i, j;             char target[ALIASLEN];             //for(i = 0; packet.buff[i] != ' '; i++); packet.buff[i++] = 0;             oid = packet.oid;             chanptr = threadinfo.channel;             strcpy(target, packet.buff);             struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));                          pthread_mutex_lock(&clientlist_mutex);             j = (threadinfo.sockfd == channelmap[chanptr][0])?1:0;             strcpy(spacket.option, "msg");             strcpy(spacket.alias, packet.alias);             strcpy(spacket.buff, packet.buff);             sent = send(channelmap[chanptr][j], (void *)&spacket, sizeof(struct PACKET), 0);             channelmap[chanptr][2] += sent;             pthread_mutex_unlock(&clientlist_mutex);         else if(!strcmp(packet.option, "chat")) {         	int i, tgt;             char target[ALIASLEN];         	pthread_mutex_lock(&clientlist_mutex);         	struct PACKET spacket;             memset(&spacket, 0, sizeof(struct PACKET));             spacket.uid = uid;         	if(threadinfo.chatflag != 0){         		strcpy(spacket.option, "in_session");         		strcpy(spacket.buff, "You are already in a session.\n");         	}else if(ifblock(uid)){         		strcpy(spacket.option, "blocked");         		strcpy(spacket.buff, "You are blocked from chatting.\n");         	}else{         		if((oid = randPick(&chat_queue))==0){         			strcpy(spacket.option, "in_queue");                     strcpy(spacket.buff, "You are currently in the queue.\n");                     list_insert(&chat_queue, &threadinfo);         		}else{                     for(curr = client_list.head; curr != NULL; curr = curr->next) {                         if(oid == curr->threadinfo.uid) {                             sprintf(spacket.buff, "You are chatting with [%d] %s \n", oid, curr->threadinfo.alias);                             curr->threadinfo.channel = chptr;                             channelmap[chptr][0] = threadinfo.sockfd;                             channelmap[chptr][1] = curr->threadinfo.sockfd;                             inChat+=2;                             threadinfo.channel = chptr;                             break;                         }                     }                     strcpy(spacket.option, "in_session");                     threadinfo.chatflag = 1;                     chptr++;                 }         	}         	spacket.oid = oid; 			sent = send(threadinfo.sockfd, (void *)&spacket, sizeof(struct PACKET), 0); 			pthread_mutex_unlock(&clientlist_mutex);         }         else if(!strcmp(packet.option, "transfer")) {// transfer signal             struct PACKET spacket;             oid = packet.oid;             int chptr = threadinfo.channel;             int i,j;             char buffer[BUFFSIZE];             int readlen, writelen;             bzero(buffer, sizeof(buffer));             memset(&spacket, 0, sizeof(struct PACKET));             strcpy(spacket.option, "filein");             strcpy(spacket.buff, "Upload start\n");                          pthread_mutex_lock(&clientlist_mutex);             strcpy(file_name, packet.buff);             // Respond  to transfer request             sent = send(threadinfo.sockfd, (void *)&spacket, sizeof(struct PACKET), 0);             if((tfp = fopen(file_name, "wb")) == NULL){                 error("ERROR Can not open to write.");                 break;             }             int len = 0;             while(len = recv(threadinfo.sockfd, (void *)&packet, sizeof(struct PACKET), 0)){                 if(!strcmp(packet.option, "upload")){                     writelen = fwrite(buffer, sizeof(char), sizeof(packet.buff), tfp);                     if(writelen < sizeof(packet.buff)){                         error("ERROR File wirte error");                         break;                     }                     channelmap[chptr][2]+=len;                     bzero(buffer, BUFFSIZE);                 }