LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2011, 08:52 AM   #1
chetan11
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
Segmentation fault during run time


I have compiled the below code successfully,
I have download this code from a site.
This code is related to client-server socket.
But when I run it, I got segmentation error.
Please anyone help.
Thanks

Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>

int main(int argc, char **argv)
{
	int sockfd,newsockfd;
	int c;
	char buf[100];
	struct sockaddr_in servaddr,cliaddr;
	
	sockfd=socket(AF_INET,SOCK_STREAM,0);

	servaddr.sin_family=AF_INET;
	servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	servaddr.sin_port=htons(atoi(argv[1]));

	bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

        listen(sockfd,5);//5 standsfor the max connections it can take.in linux 5 is for 8 connections.
	c=sizeof(cliaddr);
    	newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&c);

	if(read(newsockfd,buf,100)<0)
		printf("Error Recv Data\n");
	else	
		printf("Server: Data Recvd\n");
	
	if(write(newsockfd,"Hello!!!",100)<0)
		printf("Error Sending Data\n");
	else
		printf("Server: Replying\n");
	return 0;
}

Last edited by colucix; 08-24-2011 at 09:49 AM. Reason: Added code tags
 
Old 08-24-2011, 09:55 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'm assuming it happens at read or write and that accept fails because listen does, which fails because bind does. What port number are you using? Please rewrite your code to check the return values of socket, bind, listen, and accept. If any are -1, print out strerror(errno) and exit from the program. If you still get a segfault, compile with -g and run it with gdb.
Kevin Barry
 
Old 08-24-2011, 10:55 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Along with taOkira's advice, I also would recommend that you zero-out the servaddr structure before using it:
Code:
        ...
        memset(&servaddr, 0, sizeof(servaddr));

	servaddr.sin_family=AF_INET;
	servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	servaddr.sin_port=htons(atoi(argv[1]));
If you are running your application as a regular user, your port number must fall somewhere between 1024 and 65535. If you are running as root, then port numbers less than 1024 are available.

Check if bind() fails; it is does, it could indicate that the port you have chosen is already in use!
 
  


Reply



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
[SOLVED] Segmentation fault while trying to run C program. jahobjafwar Programming 2 07-28-2011 09:10 PM
Segmentation fault comes every time when i issue any command in linux cj_cheema Linux - Software 2 04-18-2011 04:04 AM
segmentation fault in program at run time tkmsr Programming 4 10-16-2010 05:13 PM
segmentation fault output on trying to run opera ! s.craik Linux - Software 8 07-18-2007 04:37 AM
when code tries to execute at that time i am getting segmentation fault dayalan_cse Programming 1 12-17-2006 10:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration