Address family not supported by protocol in C socket programming in linux

This is a discussion on Address family not supported by protocol in C socket programming in linux within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am writing on C simple socket programs in Linux. I wrote client and server C programs. My ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    15

    Address family not supported by protocol in C socket programming in linux

    Hi everyone, I am writing on C simple socket programs in Linux. I wrote client and server C programs. My client program is
    Code:
    #include<sys/socket.h>//for socket(), connect(), sendto() and recvform()
    
    #include<sys/types.h>
    
    #include<netinet/in.h>
    
    #include<netdb.h>
    
    #include<stdio.h>// for printf() and fprintf()
    
    #include<string.h>//for memset()
    
    #include<stdlib.h>//for atoi() and exit()
    
    #include<unistd.h>//for close()
    
    #include<errno.h>
    
    #include<arpa/inet.h>//for sockaddr_in and inet_addr()
    
     
    
    
    #define SERVERPORT 11000 
    
    int main()
    
    {
    
        
    int sockfd  = 0;
    
        
    struct sockaddr_in serv_addr;
    
        
    if((sockfd=socket(AF_INET, SOCK_STREAM,0))< 0)
    
        {
    
            printf("Cannot create socket\n");
            
             return 1;
    
        }
    
        printf("Already create socket!!!\n");
    
        memset(&serv_addr, 0 , sizeof(serv_addr));
    
        serv_addr.sin_family = AF_INET;  //AF_INET is IP address family, Internet = IP addr 
    
        printf("Finished AF_INET\n"); 
    
        serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //sin_addr = ip addr
    
        printf("Finished inet_addr\n");
    
        serv_addr.sin_port = htons(11000);  //sin_port = tcp/ip port no        
    
        printf("Finished port\n");
    
        
    //bzero(&serv_addr.sin_zero,8);
    
        
    
        
    if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0) 
    
            {
    
                perror("Cannot connect\n");            
    
                exit (1);
    
            }
    
        
    else
    
            printf("Connected\n");
    
    }
    
    
     
    
    and my server program is
    Code:
    #include<sys/socket.h>
    
    #include<sys/types.h>
    
    #include<netinet/in.h>
    
    #include<netdb.h>
    
    #include<stdio.h>
    
    #include<string.h>
    
    #include<stdlib.h>
    
    #include <unistd.h>
    
    #include<errno.h>
    
    #include<arpa/inet.h>
    
    
    int main()
    
    {
    
        
    int listenfd = 0;
    
        
    struct sockaddr_in serv_addr;
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
        
    
        
    if (listenfd < 0)
    
        {
    
            printf("Cannot create socket\n");
    
            close(listenfd);
    
            
    return 1;
    
        }
    
        printf("Already create socket!!!\n");
    
        serv_addr.sin_family = AF_INET;
    
        printf("Finished AF_INET\n");
    
        serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    
        printf("Finished INADDR_ANY\n");
    
        serv_addr.sin_port = htons(11000);
    
        printf("Finished port\n");
    
        
    
        
    if(bind (listenfd,(struct sockaddr *)&serv_addr, sizeof (serv_addr))<0)
    
        {
    
            printf("Cannot bind socket\n");
    
            close(listenfd);
    
            
               return 1;
    
        }
    
        
    
        
    
    }
    
    
    I am just testing whether can connect or not. I run server program first and client later. But when I run client, it showed that
    "Address family not supported by protocol". Why is it so? if anyone knows it, pls kindly guide me. thanks
    Last edited by why_so_serious; 10-31-2013 at 02:56 AM.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Last edited by SirPrattlepod; 10-31-2013 at 04:59 AM.

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    wots dis lynox fing evy1 torkin aboot?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    243
    Well - haven't checked it in detail - but a few thing I notice

    1. The server shows no code for listening and accepting any connections - so it's offering a bit of a limited service - i.e. none.
    2. There is no need to try and connect port to port - the server port is 11000, the client port can be left for the kernel to decide.
    3. imo the printf statements after each sockaddr_in assignment are pretty useless and just clutter the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Programming using C on linux
    By mgnidhi_3july in forum C Programming
    Replies: 2
    Last Post: 05-18-2010, 03:40 AM
  2. Address family not supported by protocol
    By -EquinoX- in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-28-2009, 09:12 AM
  3. socket programming in c or c++ in linux...
    By pk20 in forum Linux Programming
    Replies: 5
    Last Post: 02-12-2009, 12:54 AM
  4. How write address family independent code ? hints ? experiences ?
    By fredy100 in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-31-2008, 11:17 AM
  5. Linux Socket programming help
    By Keshi in forum C Programming
    Replies: 3
    Last Post: 03-15-2005, 02:58 PM

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