Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; Hi Guys... I am trying to run client server program, but the server program is giving me problem... after the ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Segmentation fault

    Hi Guys... I am trying to run client server program, but the server program is giving me problem... after the client connects to it.. it says segmentation fault and exits.. i am relatively new to this.. so please bare with me.. i have done lot of research on net, could not find anything that was useful to me... please bail me out!!!!! :-(

    Server program

    Code:

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<sys/types.h> 
    #include<errno.h>
    #include<sys/socket.h> 
    #include<netdb.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    #define PORT 6090
    
    
    int main()
    {
           int client_lenght;
           struct sockaddr_in client,server;
           struct hostent *ptrh;
           int sd,sd2;
           char message[50];
           int len;
           memset((char *)&server,0,sizeof(server));
           server.sin_family=AF_INET;
           server.sin_addr.s_addr=INADDR_ANY;
           server.sin_port = htons(PORT);
           sd = socket(AF_INET,SOCK_STREAM,0);
           if(sd<0)
          {
              fprintf(stderr,"cannot create socket\n");
              exit(1);
           }
    
          if(bind(sd,(struct sockaddr *)&server, sizeof(server))<0)
          {
             fprintf(stderr,"bind failed\n");
             exit(1);
           }
    
           listen(sd,5);
          while(1)
         {
           len = sizeof(client);
    
           if((sd2=accept(sd,(struct sockaddr *)&client, &len))<0)
                  {
                       fprintf(stderr,"Accept failed\n");
                       exit(1);
                  }
    
           printf("Waiting for message from client\n");
           recv(sd2,message,sizeof(message),0);
           printf("Meesage is %s\n",message);
           close(sd2);
         }
        return 0;
    }
    Client Program

    Code:

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<sys/types.h> 
    #include<errno.h>
    #include<sys/socket.h> 
    #include<netdb.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    #define PORT 6090
    
    int main()
    {
         int client_lenght;
         struct sockaddr_in client,server;
         struct hostent *ptrh;
         int sd;
        char message[50];
        memset((char *)&server,0,sizeof(server));
        memset((char *)&client,0,sizeof(client));
        server.sin_family = AF_INET;
    
    
    /*
    ptrh = gethostbyname(host);
    if(((char *)ptrh)==NULL)
    {
    fprintf(stderr,"invalid host: %s\n", host);
    exit(1);
    }
    memcpy(&server.sin_addr,ptrh->h_addr,ptrh->h_length);*/
    
           server.sin_addr.s_addr=INADDR_ANY;
           server.sin_port		= htons(PORT);
           if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	{
    		fprintf(stderr,"Cannot create socket\n");
    		exit(1);
    	}
            if(connect(sd,(struct sockaddr *)&server, sizeof(server))<0)
            {
               fprintf(stderr, "connect failed\n");
               exit(1);
           }
    	printf("Enter the message to be sent to server: ");
    	scanf("%s",&message);
    
            send(sd, message,50,0);
    	fflush(stdout);
    
    	/* Closing the socket*/	
    	close(sd);
    }
    Any help is appreciated........

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - You didn't need to start a new thread for the same topic.
    2 - Does the server segfault when it's run alone? (It doesn't for me.)
    3 - If it's not segfaulting alone, then it's when it's getting a message or connection, so there's your direction of where to start looking.

    You need to learn to troubleshoot problems. Take the first step --- does it happen here? No? Ok, what's the next thing that happens in the program? Does it happen there? Repeat until you find out where it's crashing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quzah.... Thank you...

    seg fault was because of one of the options i missed out in comand line...
    doing,
    gcc -o server server.c -lsocket -lnsl
    ./server

    works fine...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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