Code: #include<stdio.h> #include<string.h> #include<arpa/inet.h> #include<netdb.h> #include<sys/types.h> #include<sys/socket.h> #include<fcntl.h> #include<sys/stat.h> #include<stdlib.h> #include<time.h> int main() { FILE *logs; logs = fopen("/home/aniroot/logs/log.txt","a"); if(logs = NULL) { printf("fopen failed ... There would be no log for this session!!!"); } int sockfd,newfd; int err; struct addrinfo *res,*p,hints; struct sockaddr_storage their_addr; socklen_t addr_size; int yes=1; char ip[INET6_ADDRSTRLEN]; memset(&hints,0,sizeof(hints)); hints.ai_family=AF_UNSPEC; hints.ai_flags=AI_PASSIVE; hints.ai_socktype=SOCK_STREAM; printf("Server is open for listening on port 80\n"); if( (err = getaddrinfo(NULL,port,&hints,&res) ) == -1) { printf("Err in getaddrinfo : %s\n",gai_strerror(err)); fprintf(logs,"Err in getaddrinfo : %s\n",gai_strerror(err)); return(1); } for(p=res;p!=NULL;p=p->ai_next) { if( ( sockfd = socket(p->ai_family,p->ai_socktype,p->ai_protocol) ) == -1) { printf("Socket error !!!\n"); fprintf(logs,"Socket error !!!\n"); continue; } if( setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { fprintf(logs,"Setsockopt err!!\n"); printf("Setsockopt err!!\n"); return(1); } if( bind(sockfd,p->ai_addr,p->ai_addrlen) == -1) { printf("Binding err\n"); close(sockfd); continue; } break; } if( listen(sockfd,15) == -1) { printf("Error in listen\n"); fprintf(logs,"Error in listen\n"); return(1); } while(1) { char y; addr_size = sizeof(their_addr); if( ( newfd = accept(sockfd, (struct sockaddr *)&their_addr,&addr_size) ) == -1) { printf("Error in accept!\n"); return(1); } time_t ti = time(NULL); // know the time char *s = ctime(&ti); // convert it into string s[strlen(s) - 1] = '\0'; // remove the new line fprintf(logs,"/********************************************/\n"); fprintf(logs,"%s\n",s); for(p=res;p!=NULL;p=p->ai_next) { void *addr; if(p->ai_family == AF_INET) { struct sockaddr_in *ip; ip = (struct sockaddr_in *)p->ai_addr; addr = &(ip->sin_addr); } if(p->ai_family == AF_INET6) { struct sockaddr_in6 *ip; ip = (struct sockaddr_in6 *)p->ai_addr; addr = &(ip->sin6_addr); } inet_ntop(p->ai_family,addr,ip,sizeof(ip)); fprintf(logs,"Got connection from %s\n",ip); printf("Got connection from %s\n",ip); } freeaddrinfo(res); connection(newfd,logs); } fclose(logs); close(newfd); close(sockfd); return(0); } what i am trying to do is that i have made a http-server in C and am trying to put log functionality in it. This program compiles correctly but is giving segmentation error when reaching the fprintf() function ...... what's wrong please tell.. Thanks in Advance
When I try to compile your code I get several errors and warnings. I suggest you set your compiler to show all warnings. In gcc that would be with the compiler flag -Wall. The first warning is in line 16 Code: if(logs = NULL) this should be if(logs == NULL The first error is in line 36 Code: if( (err = getaddrinfo(NULL,port,&hints,&res) ) == -1) error: ‘port’ undeclared (first use in this function)