segmentation fault

This is a discussion on segmentation fault within the C++ Programming forums, part of the General Programming Boards category; hi, while receiving a http request from client, Segmenation fault error is there. Following is code: Code: #include<iostream> #include <sys/types.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    segmentation fault

    hi, while receiving a http request from client, Segmenation fault error is there.
    Following is code:
    Code:
    #include<iostream>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    using namespace std;
    int main()
    {       
    	int sock, connected, bytes_recieved;        
    	char recv_data[1024];        
    	struct sockaddr_in server_addr,client_addr;            
    	socklen_t sin_size;      
    	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)	
    	{            
    		cout<<"Socket Error Reason:"<<strerror(errno);            
    		exit(1);        
    	}        
    	server_addr.sin_family = AF_INET;          
    	server_addr.sin_port = htons(8081);        
    	server_addr.sin_addr.s_addr = INADDR_ANY;       
    	bzero(&(server_addr.sin_zero),8);     
    	if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1)	
    	{            
    		cout<<"Unable to bind: Reason="<<strerror(errno);           
    		exit(1);      
    	}     
    	if (listen(sock, 5) == -1) 
    	{          
    	cout<<"Listening error: Reason="<<strerror(errno);          
    	exit(1);       
    	}			
    	cout<<"\nTCPServer Waiting for client on port 8081";       
    	cin.sync(); 	
    	sin_size = sizeof(struct sockaddr_in);        
    	connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);        
    	cout<<"\n Server got a connection from "<<ntohs(client_addr.sin_port)<<endl;
    	bytes_recieved = recv(connected,recv_data,1024,0);        
    	recv_data[bytes_recieved] = '\0';	
    	if((bytes_recieved==0)||(strcmp(recv_data,"\0")==0))
    	{		
    		close(connected);			
    	}	
    	else	
    		cout<<"\n RECIEVED DATA = "<<recv_data;              
    	cin.sync();
    	shutdown(sock,2);
    	close(sock);
    	return 0;
    }
    Please help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Time to learn how to use the debugger then.

    Code:
    g++ -g prog.cpp
    gdb ./a.out
    << snipped gdb banner
    (gdb) run
    gdb will catch the segfault, and allow you to look around your code to see what went wrong.
    Examine variables around the fault location to make sure they have values you expect.
    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.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Think you might like
    Code:
    recv_data[0] == '\0'
    better than
    Code:
    (strcmp(recv_data,"\0")==0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread


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