Thread: help with socket programming

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    help with socket programming

    hi i am trying to write a simple web server which when i go to my browser and type in localhost:5000 the web server serves a html file

    heres my code so far i have 2 versions of code the first one has a segmentation fault which i cant figure out and the second version just trys to download a binary file or view it in the browser.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<arpa/inet.h>
    #include<strings.h>
    #include<string.h>
    #include<unistd.h>
    #include<signal.h>
    #include<errno.h>
    
    #define DEFAULT_PROTOCOL 0 
    #define MAXBYTE 4096
    #define PATH_SIZE 128
    
    int main(int argc, char *argv[])
    {
    	int listenfd, connectfd, port, val;
    	char recive[MAXBYTE];
    	int client_length;
    	char buffer[256];
    	char  buff[255] = {0};
    	int n;
    	struct sockaddr_in serveraddr; 
    	struct sockaddr_in clientaddr;
    	
    	signal(SIGCHLD, SIG_IGN);
    	
    	if (argc < 2) {
            	fprintf(stderr,"ERROR, no port enterd\n");
            	exit(1);
         		}
    	
    	if((listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
    	{
    	
    		perror("SOCKET");
    		exit(EXIT_FAILURE);
    		
    		}
    		
    	setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
    	
    	bzero((char *) &serveraddr, sizeof(serveraddr));
    	port = atoi(argv[1]);
    	serveraddr.sin_family = AF_INET;
    	serveraddr.sin_port = htons(port);
    	serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	
    	if((bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
    	{
    		perror("bind");
    		exit(EXIT_FAILURE);
    		
    	}
    	
    	
    		 listen(listenfd,10);
    		 client_length = sizeof(clientaddr);
     	
    		 connectfd = accept(listenfd, (struct sockaddr *)&clientaddr, &client_length);
    		 if (connectfd < 0)
      	 		error("eRROR on accept");
    			 bzero(buffer,256);
    		
    	
    			
    		n = read(connectfd, buffer, 255 );
    	 	if (n < 0) error("ERROR reading from socket");
    	 	 printf("Here is the message: %s\n",buffer);
    	 	  FILE *open = NULL;
    	 	  char err_msg[] = "Could not open";
    	 	  
    	
    			
    	if(open = fopen("/home/rocco/www/home.html", "r"))
    	
    	{
    		n = write(connectfd, err_msg, strlen(err_msg));
    		n = write(connectfd, "/home/rocco/www/home.html", strlen("/home/rocco/www/home.html"));
    		n = recv(connectfd, buff, 255, 0);
    		fprintf (open, "%s", buff);
    	
    		exit(EXIT_FAILURE);
    		
    		}
    		
    		 while ((write(connectfd, recive, fread(recive, sizeof(char), MAXBYTE, open))))
    		{}
    		
    		fclose(open);
    		close(connectfd);
    		exit(EXIT_SUCCESS); 
    		}
    2nd version

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<arpa/inet.h>
    #include<strings.h>
    #include<string.h>
    #include<unistd.h>
    #include<signal.h>
    #include<errno.h>
    
    #define DEFAULT_PROTOCOL 0 
    #define MAXBYTE 4096
    #define PATH_SIZE 128
    
    int main(int argc, char *argv[])
    {
    	int listenfd, connectfd, port, val;
    	char recive[MAXBYTE];
    	int client_length;
    	char buffer[256];
    	char  buff[255] = {0};
    	int n;
    	struct sockaddr_in serveraddr; 
    	struct sockaddr_in clientaddr;
    	
    	signal(SIGCHLD, SIG_IGN);
    	
    	if (argc < 2) {
            	fprintf(stderr,"ERROR, no port enterd\n");
            	exit(1);
         		}
    	
    	if((listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
    	{
    	
    		perror("SOCKET");
    		exit(EXIT_FAILURE);
    		
    		}
    		
    	setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
    	
    	bzero((char *) &serveraddr, sizeof(serveraddr));
    	port = atoi(argv[1]);
    	serveraddr.sin_family = AF_INET;
    	serveraddr.sin_port = htons(port);
    	serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	
    	if((bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
    	{
    		perror("bind");
    		exit(EXIT_FAILURE);
    		
    	}
    	
    	
    		 listen(listenfd,10);
    		 client_length = sizeof(clientaddr);
     	
    		 connectfd = accept(listenfd, (struct sockaddr *)&clientaddr, &client_length);
    		 if (connectfd < 0)
      	 		error("eRROR on accept");
    			 bzero(buffer,256);
    		
    	
    			
    		n = read(connectfd, buffer, 255 );
    	 	if (n < 0) error("ERROR reading from socket");
    	 	 printf("Here is the message: %s\n",buffer);
    	 	  FILE *open = NULL;
    	 	  char err_msg[] = "Could not open";
    	 	  
    	while((open = fopen("/home/rocco/ww/home.html","r") != EOF))
    	{
    		n = write(connectfd, &open,1);
    		}

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When I compiled your first program I got these warning/error messages:
    main.c||In function ‘main’:|
    /main.c|59|error: pointer targets in passing argument 3 of ‘accept’ differ in signedness|
    /usr/include/sys/socket.h|214|note: expected ‘socklen_t * restrict’ but argument is of type ‘int *’|
    /main.c|61|error: implicit declaration of function ‘error’|
    /main.c|74|warning: suggest parentheses around assignment used as truth value|
    ||=== Build finished: 2 errors, 1 warnings ===|
    And your second program produces these error/warnings:
    /main.c||In function ‘main’:|
    /main.c|59|error: pointer targets in passing argument 3 of ‘accept’ differ in signedness|
    /usr/include/sys/socket.h|214|note: expected ‘socklen_t * restrict’ but argument is of type ‘int *’|
    /main.c|61|error: implicit declaration of function ‘error’|
    /main.c|72|error: comparison between pointer and integer|
    /main.c|72|error: assignment makes pointer from integer without a cast|
    /main.c|75|error: expected declaration or statement at end of input|
    /main.c|70|warning: unused variable ‘err_msg’|
    /main.c|20|warning: unused variable ‘buff’|
    /main.c|17|warning: unused variable ‘recive’|
    ||=== Build finished: 5 errors, 3 warnings ===|
    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    i compiled mine with gcc i dont get no errors i know i have unused variables but they are there for later on
    this code compiled with n errors
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<arpa/inet.h>
    #include<strings.h>
    #include<string.h>
    #include<unistd.h>
    #include<signal.h>
    #include<errno.h>
    #include <sys/socket.h>
    
    #define DEFAULT_PROTOCOL 0 
    #define MAXBYTE 4096
    #define PATH_SIZE 128
    
    int main(int argc, char *argv[])
    {
    	int listenfd, connectfd, port, val;
    	char recive[MAXBYTE];
    	int client_length;
    	char buffer[256];
    	char  buff[255] = {0};
    	char file[256];
    	int n;
    	struct sockaddr_in serveraddr; 
    	struct sockaddr_in clientaddr;
    	
    	signal(SIGCHLD, SIG_IGN);
    	
    	if (argc < 2) {
            	fprintf(stderr,"ERROR, no port enterd\n");
            	exit(1);
         		}
    	
    	if((listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
    	{
    	
    		perror("SOCKET");
    		exit(EXIT_FAILURE);
    		
    		}
    		
    	setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
    	
    	bzero((char *) &serveraddr, sizeof(serveraddr));
    	port = atoi(argv[1]);
    	serveraddr.sin_family = AF_INET;
    	serveraddr.sin_port = htons(port);
    	serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	
    	if((bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
    	{
    		perror("bind");
    		exit(EXIT_FAILURE);
    		
    	}
    	
    	
    		 listen(listenfd,10);
    		 client_length = sizeof(clientaddr);
     	
    		 connectfd = accept(listenfd, (struct sockaddr *)&clientaddr, &client_length);
    		 if (connectfd < 0)
    		 {
      	 		error("eRROR on accept");
      	 		}	 
    		        bzero(buffer,256);
    		 
    		     FILE *open = NULL;  
    		  
    	        open = fopen("/home/index.html","r");	
    	        if((open = fopen("/home/index.html","r")) == NULL)
    	        {
    	        	puts("error");
    	        	}
    	        	
    	
    	
     
    	
    			
    		while((n = fread(buffer,1,sizeof buffer,open)) < 0)
    		{
    		if (n < 0) error("ERROR reading from socket");
    			
    			n = write(connectfd, buffer,strlen(buffer));
    		}
    	 	}

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Try 'gcc -Wall main.c'. Those warnings are there for good reason. Your code should always compile without any warnings on the strictest setting.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    i took out the unused variables, but still getting these 3 errors which i do not know how to go about fixing them ?

    warning: pointer targets in passing argument 3 of ‘accept’ differ in signedness
    /usr/include/sys/socket.h:214: note: expected ‘socklen_t * __restrict__’ but argument is of type ‘int *’
    95: warning: control reaches end of non-void function

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A Google search on error messages usually brings up tons of info on what went wrong and how to fix it. I'll save you the trouble this time:

    The first warning tells you you have the wrong type for parameter 3 of accept. It wants a 'socklen_t *', but you are giving it an 'int *'. Declare client_length to be the right type.

    The second error means at line 95, you reach the end of a function that is supposed to return something, but you didn't give it something to return.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It seems you don't really know C very well. Might I suggest you learn C a little more thoroughly before diving into writing a web server (even a simple one)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. little help in simple socket library
    By SIFE in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-31-2010, 11:01 PM
  2. Why the local socket program occur core dump?
    By chenayang in forum Linux Programming
    Replies: 16
    Last Post: 08-16-2010, 08:39 AM
  3. Problem with socket descriptors
    By McKracken in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 08:51 AM
  4. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM