#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

Recommended Answers

All 6 Replies

is giving segmentation error when reaching the fprintf() function ......

You have a really nasty bug there, something is wrong with the following line ..

...
if(logs = NULL)
...

Furthermore, if you fail to open the log file, then you must NOT try to fprintf() anything to it (otherwise, the result is the same as now - a crash).

PS. Have you checked that the compiler is not giving any warnings?

commented: Nice answer thank you!!!! +0

You have a really nasty bug there, something is wrong with the following line ..

...
if(logs = NULL)
...

Furthermore, if you fail to open the log file, then you must NOT try to fprintf() anything to it (otherwise, the result is the same as now - a crash).

PS. Have you checked that the compiler is not giving any warnings?

I have done that but now it is going right till while(1) loop after that it is not giving error but it is not printing the data in the file...

What's the problem now!!!!

int main()
{
	FILE *log1s;
	log1s = fopen("/home/aneesh/aniroot/logs/log.txt","a");
	
	if(log1s == NULL)
	{
		printf("fopen failed ... There would be no log for this session!!!");
		return(1);
	}
	
	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(log1s,"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(log1s,"Socket error !!!\n");
			continue;
		}
		if( setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
		{
			fprintf(log1s,"Setsockopt err!!\n");			
			printf("Setsockopt err!!\n");
			return(1);
		}

		if( bind(sockfd,p->ai_addr,p->ai_addrlen) == -1)
		{
			printf("Binding err\n");
			fprintf(log1s,"Binding err\n");
			close(sockfd);			
			continue;
		}
		
		break;
	}

	if( listen(sockfd,15) == -1)
	{
		printf("Error in listen\n");
		fprintf(log1s,"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(log1s,"/********************************************/\n");
		fprintf(log1s,"%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(log1s,"Got connection from %s\n",ip);
			printf("Got connection from %s\n",ip);
		}
		connection(newfd);
	}
	fclose(log1s);
	freeaddrinfo(res);
	close(newfd);
	close(sockfd);
	return(0);
}

Are you sure that any fprintf() -calls are actually made? fprintf() returns a value - you might check what you are receiving.

Perhaps write a little fprintf() -test program so that you get familiar with it?

I tried that also i did:-

if( fprintf(log1s,"/********************************************/\n") < 0)
		{
				printf("fprintf failed!!!\n");
				return(1);
		}

then also no error and still no printing!!!!!!!!!

What is this ???????????????????????? :(

I'd suggest that you first get the fprintf() -thing working, separately from any other code.

So, try the following and see what it outputs.

#include <stdio.h>

int main()
{
  FILE * fp = fopen("/home/aniroot/logs/log.txt", "a");

  if(fp != NULL)
  {
    /* Write something .. */
    int result = fprintf(fp, "%s", "testing ...\n");

    if(result < 0)
    {
      /* Failed, display error information */
      perror("fprintf()");
    }
    else
    {
      printf("result: %d\n", result);
    }

    /* Try to close the file (also flushes any written output) */
    if(fclose(fp) == EOF)
    {
      /* Failed, display error information */
      perror("fclose()");
    }
  }
  else
  {
    /* Failed, display error information */
    perror("fopen()");
  }

  return 0;
}

You might also check out related functions such as clearerr()/ferror()/fflush().

When you are checking to see how many chars have been printed via fprint use the condition <=0 insteaad of < 0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.