LinuxQuestions.org
Review your favorite Linux distribution.
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 11-20-2013, 12:48 PM   #1
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Rep: Reputation: Disabled
Angry Why is there a segmentation fault here?


Code:
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
    int create_socket,cont;
    int bufsize = 1024;
    char *buffer = malloc(bufsize);
    char fname[256];
    struct sockaddr_in address;
    if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
      printf("Socket was created\n"); -->after printing this it gives seg fault.
    address.sin_family = AF_INET;
    address.sin_port = htons(15000);
    inet_pton(AF_INET,argv[1],&address.sin_addr);
    if (connect(create_socket,(struct sockaddr *)&address,sizeof(address))== 0)
    printf("The connection was accepted with the server %s...\n",argv[1]);
    printf("Enter The Filename to Request : "); scanf("%s",fname);
    send(create_socket, fname, sizeof(fname), 0);
    printf("Request Accepted... Receiving File...\n\n");
    printf("The contents of file are...\n\n");
    while((cont=recv(create_socket, buffer, bufsize, 0))>0) {
       write(1, buffer, cont);
    }
    printf("\nEOF\n");
    return close(create_socket);
}

Last edited by colucix; 11-21-2013 at 01:38 AM. Reason: Added CODE tags to increase readability
 
Old 11-20-2013, 03:17 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,150

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
You are failing to check for the presence of the required command-line argument. If you don't provide one inet_pton segfaults on the NULL string argv[1].
 
Old 11-20-2013, 08:22 PM   #3
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
I'm sorry, but could you be a little more specific? I'm new to this. Thank you!
 
Old 11-20-2013, 08:23 PM   #4
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
and how do i fix it??
 
Old 11-21-2013, 01:38 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-21-2013, 07:40 AM   #6
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Try adding something like this early in the function (include ctype.h if you use isdigit()):
Code:
if (argc < 2  ||  !isdigit(argv[1][0]))
{
    fprintf(stderr, "ERROR: An IP address is required\n");
    exit(1);
}
 
Old 11-21-2013, 11:11 PM   #7
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
It got solved. Thank you everyone for your help!

All I had to do was include a port number while executing, cuz this is a socket program for tcp/ip, which I did not realize at first :P
So added the port number and it worked like a charm
 
  


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
Segmentation fault DarkC0de Linux - General 2 03-12-2010 01:01 AM
segmentation fault? kalleanka Programming 9 11-22-2006 05:42 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault? JiggaJerry Fedora 10 01-16-2005 04:01 PM
Segmentation fault compjinx Linux - Newbie 4 11-09-2003 04:51 PM

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

All times are GMT -5. The time now is 01:39 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