strange segmentation fault

This is a discussion on strange segmentation fault within the C Programming forums, part of the General Programming Boards category; I have been reading Unix Network Programming by Stevens, and have been doing the exercises along with it. I have ...

  1. #1
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45

    strange segmentation fault

    I have been reading Unix Network Programming by Stevens, and have been doing the exercises along with it.

    I have written a small, basic tcp server that is simply going to transfer small packets to and from a client. When I try to execute the program, after a successful compile, I immediatly get a SEGMENTATION FAULT and the program quits. I get the following in gdb (note, i am not an experienced GDB user and only know the very basics)

    Code:
    Starting program: /home/tarcuri/CProjects/tcpserv 8888
    (no debugging symbols found)...(no debugging symbols found)...
    Program received signal SIGSEGV, Segmentation fault.
    0x005b61fd in inet_aton () from /lib/tls/libc.so.6
    Now I am not really sure what GDB is supposed to be doing here but I never even called inet_aton() in my program. I see that it is referencing some library but I dont see how this helps me. Ill post the code below.

    EDIT: I don't even know how to go about deciphering this.

    Code:
    [tarcuri@intern1 CProjects]$ gcc -g tcpserv tcpserv.c
    collect2: ld terminated with signal 11 [Segmentation fault]
    tcpserv(.rodata+0x0): multiple definition of `_fp_hw'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.rodata+0x0): first defined here
    tcpserv(.data+0x4): In function `__data_start':
    : multiple definition of `__dso_handle'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/crtbegin.o(.data+0x0): first defined here
    tcpserv(.init+0x0): In function `_init':
    : multiple definition of `_init'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crti.o(.init+0x0): first defined here
    tcpserv(.text+0x0): In function `_start':
    : multiple definition of `_start'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.text+0x0): first defined here
    tcpserv(.fini+0x0): In function `_fini':
    : multiple definition of `_fini'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crti.o(.fini+0x0): first defined here
    tcpserv(.got.plt+0x0): multiple definition of `_GLOBAL_OFFSET_TABLE_'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.got.plt+0x0): first defined here
    tcpserv(.rodata+0x4): multiple definition of `_IO_stdin_used'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.rodata+0x4): first defined here
    tcpserv(.data+0x0): In function `__data_start':
    : multiple definition of `__data_start'
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.data+0x0): first defined here
    /tmp/ccJhB0Jh.o(.text+0x0): In function `main':
    /home/tarcuri/CProjects/tcpserv.c:16: multiple definition of `main'
    tcpserv(.text+0xa8): first defined here
    /tmp/ccJhB0Jh.o(.text+0x265): In function `sendData':
    /home/tarcuri/CProjects/tcpserv.c:82: multiple definition of `sendData'
    tcpserv(.text+0x30d): first defined here
    /tmp/ccJhB0Jh.o(.text+0x383): In function `recvData':
    /home/tarcuri/CProjects/tcpserv.c:110: multiple definition of `recvData'
    tcpserv(.text+0x42b): first defined here
    /usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
    tcpserv(.dynamic+0x0): first defined here
    Last edited by Lateralus; 06-10-2005 at 09:16 AM.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  2. #2
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    
    #define BUFMAX 4096
    
    int sendData(int sockfd);
    int recvData(int sockfd);
    
    main(int argc, char *argv[])
    {
    	int listenfd, connfd, port;
    	pid_t childpid;
    	size_t clilen;
    	struct sockaddr_in serv_addr, cli_addr;
    
    	if (argc != 2) {
    		printf("USAGE: tcpserv <port>\n");
    		exit(1);
    	}
       port = (int)&argv[1];
    	
    	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	 	perror("socket");
    	 	
    
    	memset(&serv_addr, '\0', sizeof(serv_addr));
    	serv_addr.sin_family = AF_INET;
    	serv_addr.sin_addr.s_addr = inet_addr(INADDR_ANY);
    	serv_addr.sin_port = htons(port); 
    
    
    	if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1)
    		perror("bind");
    	if (listen(listenfd, 5) == -1) 
    		perror("listen");
    	else
    		printf("Listening for connection request on port %d...\n",port);
    	
    	for (;;) {
    		clilen = sizeof(cli_addr);
    		if ((connfd = accept(listenfd, (struct sockaddr *) &cli_addr, &clilen)) == -1)
    			perror("accpet");
    		
    		if ( (childpid = fork() ) == 0) { /* creates child process */
    			close(listenfd);
    			int choice = 0;
    			while (choice == 0) {
    				printf("\n1. Send Data\n");
    				printf("2. Receive Data\n");
    				printf("3. Quit\n\n");
    				scanf("%d", choice);
    				
    				if (choice == 1) 
    					if (sendData(connfd) != 0)
    						printf("Error occured in sendData\n");
    				else if (choice == 2)
    					if (recvData(connfd) != 0)
    						printf("Error occured in recvData\n");
    				else if (choice == 3) {
    				   close(connfd);  
    					exit(1);
    			   }
    				else 
    					printf("Invalid choice, choose again.\n");
    			
    				choice = 0;
    			}
    			exit(0);
    		}
    	}
    			
    }
    
    int sendData(int sockfd) 
    {
    	char file[20], *buffer[BUFMAX];
    	size_t bytes_read, bytes_sent, filesz;
    	FILE *pFile = NULL;
    	
    	printf("Enter name of file to send.\n\n");
    	scanf("%s",&file);
    
       pFile = fopen(file,"r");
    
    	filesz = sizeof(pFile);
    	if ((bytes_read = fread(buffer,1,filesz,pFile)) < 1)
    		perror("fread");
    	
    	if ((bytes_sent = send(sockfd, buffer, bytes_read, 0)) == -1)
    		perror("send");
    	else if (bytes_sent < bytes_read) {
    		printf("\nError sending file!\n");
    		return -1;
    	}
    	else {
    		printf("\nFile sent!\n");
    		return 0;
    	}
    	fclose(pFile);
    }
    
    int recvData(int sockfd) 
    {
    	char *buffer[BUFMAX], *file;
    	size_t bytes_recv, bytes_svd;
    	FILE *pFile;
    	
    	if ((bytes_recv = recv(sockfd, buffer, sizeof(buffer),0)) == -1) {
    		perror("recv");
    		exit(0);
    	}
    	
    	printf("\nWhat would you like to save the file as?\n");
    	scanf("%s",file);
    
    	pFile = fopen(file,"w+b");
    	
    	if ((bytes_svd = fwrite(buffer,1,sizeof(buffer),pFile)) == -1) {
    		perror("fwrite");
    		return -1;
    	} 
    	else if (bytes_svd < bytes_recv) {
    		printf("Only partial file saved!\n");
    		fclose(pFile);
    		exit(0);
    	}
    	printf("\nFile saved to disk!\n");
    	fclose(pFile);
    	return 0;
    }
    hope its not something too embarassing, thanks in advance for any ideas.

    edit: i just noticed i didnt include string.h, but thats still not the problem. Also, that in the choice menu i had 'choice' instead of '&choice', yet i am still getting the seg fault.
    Last edited by Lateralus; 06-10-2005 at 09:29 AM.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Code:
    gcc -Wall -W -ansi -pedantic  hello.c
    hello.c:16: warning: return type defaults to `int'
    hello.c: In function `main':
    hello.c:34: warning: implicit declaration of function `inet_addr'
    hello.c:52: warning: ISO C89 forbids mixed declarations and code
    hello.c:57: warning: format argument is not a pointer (arg 2)
    hello.c:62: warning: suggest explicit braces to avoid ambiguous `else'
    hello.c:59: warning: suggest explicit braces to avoid ambiguous `else'
    hello.c: In function `sendData':
    hello.c:87: warning: char format, different type arg (arg 2)
    hello.c:95: warning: comparison between signed and unsigned
    hello.c:106: warning: control reaches end of non-void function
    hello.c: In function `recvData':
    hello.c:114: warning: comparison between signed and unsigned
    hello.c:124: warning: comparison between signed and unsigned
    1. port = (int)&argv[1];
    This isn't how you convert a string to an int.
    Try sscanf() or strtod()

    2. scanf("%d", choice);
    You've got to remember the & when using scanf, otherwise you simply trash memory you don't own.
    Eg.
    scanf("%d", &choice);

    I suggest you use the compiler options above, and don't run code until it at least clean-compiles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  3. strange segmentation fault
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-19-2004, 08:46 AM
  4. strange segmentation fault error!
    By jayjay in forum Linux Programming
    Replies: 1
    Last Post: 10-20-2003, 03:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21