LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-15-2006, 06:46 AM   #1
ankurcse
LQ Newbie
 
Registered: Mar 2006
Posts: 29

Rep: Reputation: 15
Segmentation fault in client - server programming in Linux


Hi,

i made server program in c on linux, accepting portno as argument, and i made client program on same architecture accepting hostname & portno as arguments, but when i run client on same machime in which i ran server, it is giving error " Segmentation fault".

why????????????
 
Old 07-15-2006, 07:35 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
There's an error on line 42.
 
Old 07-15-2006, 07:41 AM   #3
ankurcse
LQ Newbie
 
Registered: Mar 2006
Posts: 29

Original Poster
Rep: Reputation: 15
Hi Hivemind,

error on line 42, i m not getting
 
Old 07-15-2006, 07:59 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
The point I was trying to make was that there was not nearly enough information
in your post to help. You didn't post a single line of code. How are we supposed
to know what the problem is then?

I suggest that you create the simplest possible, but still complete and compilable,
test program that exhibits the error and post it here enclosed in code tags.

But first you should step through the crashing program with a debugger, monitoring
every variable that is in scope. Identify the line that makes it crash and at least
post that line along with the definitions of the variables being used in it.

A segmentation fault most likely means that you are tampering with memory that is not yours to tamper with. Maybe you're trying to use a pointer that is not yet pointing to memory you are allowed to read/write. Since you seem so stumped by a segmentation fault I get the feeling that
your grasp of the core C language is poor and you should probably master it to a higher
degree before starting to use networking libraries and other platform specific stuff.

Also, make sure you turn all warnings on, that could help you find the problems faster.

Last edited by Hivemind; 07-15-2006 at 08:01 AM.
 
Old 07-15-2006, 08:21 AM   #5
ankurcse
LQ Newbie
 
Registered: Mar 2006
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,
Actually i m fresher working for IT Dept, & developing a client-server application.

Now i m writing key lines that clears program purpose, & i think u can make out some points from below lines: Client.c


struct sockaddr_in serv_addr ;

struct hostent *server ;

char buffer[256] = " " ;

portno = atoi( argv[2] ) ;

sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ;

server = gethostbyname( argv[1] ) ;

serv_addr.sin_family = AF_INET ;

bcopy( (char*)server->h_addr, (char*)serv_addr.sin_addr.s_addr, server->h_length ) ;

serv_addr.sin_port = htons( portno ) ;

if( connect( sockfd, ( struct sockaddr*)&serv_addr, sizeof( serv_addr )) < 0 )
error( "ERROR connecting" ) ;

fgets( buffer, 255, stdin ) ;

n = write( sockfd, buffer, strlen( buffer )) ;

n = read( sockfd, buffer, 255 ) ;


I m using gcc compiler on RHEL 2.6 kernel, can u pls tell me how can i debug my program line by line ?
 
Old 07-15-2006, 09:39 AM   #6
Progmaker
Member
 
Registered: Sep 2004
Location: Kaunas, Lithuania
Distribution: Knoppix, WinXP PRO SP2, OpenSUSE 10.0 OSS
Posts: 30

Rep: Reputation: 15
And that's it? It's all your client's code?
 
Old 07-15-2006, 11:52 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by ankurcse
can u pls tell me how can i debug my program line by line ?
sure, there is a debugger called gdb. You will need to look around for help on how to use it (man, google) but you can get a great deal of information from it.

To start you off try here

enjoy.

Last edited by graemef; 07-15-2006 at 11:54 AM.
 
Old 07-16-2006, 10:02 AM   #8
ankurcse
LQ Newbie
 
Registered: Mar 2006
Posts: 29

Original Poster
Rep: Reputation: 15
HI,

i have written here only client side code, not server side, bec when i run server no error found, but on running client , segmentation fault comes
 
Old 07-16-2006, 10:37 AM   #9
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
First thing i would check is that i actually have 3 arguments passed in from the command line. You access argv[2] are you sure you have that many arguments passed in.
 
Old 07-17-2006, 12:35 AM   #10
ankurcse
LQ Newbie
 
Registered: Mar 2006
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,

yes client has 2 arguments "hostname" & "portno", while server has only one arg "portno", after running server i run client then it gives segmentation fault on client side.

why ??
 
Old 07-17-2006, 10:41 AM   #11
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Rep: Reputation: 30
if you can't find what is causing the trouble by using gdb, use valgrind with the --db-atach=yes flag, and it will dump you in the debugger with a short description of the first illegal memory access.
 
  


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
Red Hat 9 server crash after 170 days (segmentation fault) phx777 Linux - General 10 04-18-2006 12:36 PM
programming-segmentation fault(sprintf) ramakrishna sura Linux - General 1 12-20-2005 05:12 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 10:34 AM
Segmentation Fault when installing pptp client elluva Linux - Networking 0 12-09-2003 01:08 PM
Segmentation fault on RedHat Linux 8.0 hemanth_13 Linux - General 2 07-28-2003 06:52 AM

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

All times are GMT -5. The time now is 02:09 AM.

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