클라이언트로 부터 char 300정도 받으면 세크먼트 에러가......
글쓴이: cccc2002 / 작성시간: 수, 2003/08/20 - 5:55오후
Segmentation fault (core dumped) 가 생기네요....
#include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <signal.h> #include <sys/socket.h> #include <sys/file.h> #include <netinet/in.h> #include <string.h> #include <signal.h> #define MAXLINE 1024 #define MAX_SOCK 64 char rline[MAXLINE]; char *escapechar="exit"; int getmax(int); void removeClient(int); int maxfdp1; int num_com=0; int client_s[MAX_SOCK]; int command_nflag,command_nsize,option_nflag,option_nsize; char command[256],option[256]; int main(int argc,char *argv[]) { int i,j,n; int s,client_fd,clilen; fd_set read_fds; struct sockaddr_in client_addr,server_addr; signal(SIGHUP, SIG_IGN); signal(SIGTERM, SIG_IGN); if(argc !=2) { printf("%s port\n",argv[0]); exit(0); } if((s=socket(PF_INET,SOCK_STREAM,0)) <0) { printf("Server: Can't open stream socket."); exit(0); } //setting server_addr bzero((char *)&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(atoi(argv[1])); if(bind(s,(struct sockaddr *)&server_addr,sizeof(server_addr))<0) { printf("Server:Can't bind local address.\n"); exit(0); } listen(s,5); maxfdp1=s+1; while(1) { FD_ZERO(&read_fds); FD_SET(s,&read_fds); for(i=0; i<num_com;i++) FD_SET(client_s[i],&read_fds); maxfdp1=getmax(s)+1; if(select(maxfdp1,&read_fds,(fd_set *)0,(fd_set *)0,(struct timeval *)0) <0) { printf("select error <= 0 \n"); exit(0); } if(FD_ISSET(s,&read_fds)) { clilen=sizeof(client_addr); client_fd=accept(s,(struct sockaddr *)&client_addr,&clilen); if(client_fd == -1) { printf("accept error\n"); exit(0); } client_s[num_com]=client_fd; num_com++; printf("%d client_commputer\n",num_com); } for(i=0;i<num_com;i++) { if(FD_ISSET(client_s[i],&read_fds)) { if((n=recv(client_s[i],rline,MAXLINE,0)) <= 0) { removeClient(i); continue; } if(strstr(rline,escapechar)!=NULL) { removeClient(i); continue; } rline[n]='\0'; for(j=0;j<num_com;j++) send(client_s[j],rline,n,0); memcpy(&command_nflag,rline,4); memcpy(&command_nsize,rline+4,4); command[command_nsize]='\0'; memcpy(command,rline+8,command_nsize); memcpy(&option_nflag,rline+sizeof(int)*2 + command_nsize,4); memcpy(&option_nsize,rline+sizeof(int)*3 + command_nsize,4); } for(i=0;i<num_com;i++) { if(FD_ISSET(client_s[i],&read_fds)) { if((n=recv(client_s[i],rline,MAXLINE,0)) <= 0) { removeClient(i); continue; } if(strstr(rline,escapechar)!=NULL) { removeClient(i); continue; } rline[n]='\0'; for(j=0;j<num_com;j++) send(client_s[j],rline,n,0); } } } } void removeClient(int i) { close(client_s[i]); if( i !=(num_com -1)) { client_s[i]=client_s[num_com -1]; } num_com--; printf("total computer : %d\n",num_com); } int getmax(int k) { int max=k; int r; for(r=0;r<num_com;r++) { if(client_s[r] >max) max=client_s[r]; } return max; }
Forums:
죄송....command[256]으로 잡아서 그랬습니다......
command로 넘어오는 값이 256을 넘어서 그랬습니다.
지송.
댓글 달기