LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 02-17-2011, 05:27 AM   #1
rajbir
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
Segmentation fault in receiving http request from client code


There is a segmentation fault error while receiving http request from client side program. Following is the 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
 
Old 02-18-2011, 03:36 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by rajbir View Post
Code:
	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;
You should verify that bytes_received is not -1 before trying to use it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php https wrapper giving Segmentation fault but not http imraven Linux - Software 2 03-27-2009 02:20 AM
segmentation fault in assembly code tanoatlq Programming 1 09-19-2008 09:26 AM
What is wrong with my code? C++ and segmentation fault mesh2005 Programming 12 07-25-2007 02:42 PM
when code tries to execute at that time i am getting segmentation fault dayalan_cse Programming 1 12-17-2006 10:30 AM
segmentation fault in following peice of code :please help linorg Programming 2 03-18-2004 08:57 PM


All times are GMT -5. The time now is 06:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration