client-server program: segmentation fault

This is a discussion on client-server program: segmentation fault within the Linux Programming forums, part of the Platform Specific Boards category; HI, Hi, i made server program in c on linux, accepting portno as argument, and i made client program on ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    8

    client-server program: segmentation fault

    HI,

    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????????????

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    We need code to help you.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Since my crystal ball is also segfaulting at the moment, I guess you'll have to post some code if you want anything like a specific answer rather than some general waffle about "have you tried x, y, z, blah blah blah"
    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.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Well, before posting your code, compiling it with -g option and run in gdb may be a good practice

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    8
    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.
    Similarly i written server.c
    run:
    on one terminal, ./server <portno>
    on other terminal, ./client <hostname> <portno>
    on client side, i m getting "Segmentation Fault"
    Why?????????????????/

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    code tags ftw.

  7. #7
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Read the previous messages and then post a new one! You can use GDB to locate the line where you get the error...

    Allocate memory before using bcopy(). Something like below will work...
    Code:
    serv_addr.sin_addr.s_addr=malloc(sizeof(unsigned long));
    And, use "code" tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server and client in the same program
    By smithx in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-25-2008, 08:30 PM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 06:36 PM
  3. strange segmentation fault
    By Lateralus in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 09:19 AM
  4. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 04:47 PM
  5. get segmentation fault
    By xenodvs1 in forum C Programming
    Replies: 1
    Last Post: 04-06-2003, 09:26 PM

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