Thread: [networking] socket problem in bind() and recvfrom()

  1. #1
    Registered User
    Join Date
    May 2012

    [networking] socket problem in bind() and recvfrom()

    i have this piece of code:

    /*       compilation options:   reset; gcc  -g -Wall -ansi -pedantic testes.c -o tt          */
    #define _POSIX_C_SOURCE 200112L 
    #define  _XOPEN_SOURCE 600
    #define _BSD_SOURCE /*|| _SVID_SOURCE*/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <limits.h>
    #include <errno.h>
     #include <strings.h>
    #define MAX_READ 100
    int main(int argc, char **argv)
        struct sockaddr_in serv_addr;
        int sockfd, slen=sizeof(serv_addr);
        char command_string[] ="REG;;61000;60000";    
        char *saport="58000";
        char *IPserver="";
        socklen_t len_inet;
        int z;
        int n;
        int addrlen;
        char receive[50];
       sockfd = socket(AF_INET, SOCK_DGRAM, 0);
       if ( sockfd == -1 ) 
          printf("UDP socket() call failed to create");
            printf("UDP socket created ping is %s \n", saport);
     memset((void*)&serv_addr,(int)'\0', sizeof(serv_addr));
        /*bzero(&serv_addr, sizeof(serv_addr));*/
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(atoi(saport));
        if (inet_aton(IPserver, &serv_addr.sin_addr)==0)
            fprintf(stderr, "inet_aton() failed\n");
        len_inet = sizeof(serv_addr);
        z = bind(sockfd, (struct sockaddr *)&serv_addr, len_inet);
        if ( z == -1) 
            printf ("UDP ..Bind socket bind() failed\n");
            printf("UDP socket bound to port \n");
         printf("command_string=%s antes do sendto func\n",command_string);
        n = sendto(sockfd, command_string, strlen(command_string), 0, (struct sockaddr *)&serv_addr, len_inet);/* ver o zero dos flags, meter uma macro*/
        if(n==-1) exit(1);        
        /*memset(command_string, '0', MAX_READ );*/
       /* addrlen=sizeof(res);*/
        memset(command_string,'\0', sizeof(command_string));
        printf("command_string=%s ... func\n",command_string);
        n=recvfrom(sockfd,receive/*command_string*/, 50 /*sizeof(command_string)*/, 0, (struct sockaddr *)&serv_addr, &len_inet);/*ver se posso meter &(sizeof(res) em vez de addrlen)*/
          printf("command_string=%s ...\n",command_string);
          printf("receive=%s ...\n",receive); /*output must be :   DNS;;60000   */
          return 0;

    i am not able to bind it nor to receive anything in the recvfrom and i dont know why. so is smb able to help me?

    i thank you in advance for the help
    Last edited by tindala; 03-29-2014 at 12:26 PM.

  2. #2
    Join Date
    Dec 2007
    Use perror to print the actual error that's occurring (assuming you're receiving an error; you don't say whether or not you are).

  3. #3
    Registered User
    Join Date
    May 2012
    thaank you for the answer, i got the following message when using perror: The following error occurred: Cannot assign requested address
    i used the following iunstruction perror ("The following error occurred");
    ok, i was already knowing that it was not able to do that, but i dont know why....

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    A quick Google search on the error message "Cannot assign requested address" turned up this page: socket.error:[errno 99] cannot assign requested address and namespace in python - Stack Overflow. Yes, it's for Python, but it should be quite similar for C.

    I notice that you're trying to bind to Is that the IP address of a valid, up interface on your computer? You can't bind to the address of another computer, or even your router.

  5. #5
    Registered User
    Join Date
    May 2012
    @anduril462 thank you very much for the answer.
    that solved my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket operation on non-socket problem
    By MiniComa in forum C Programming
    Replies: 9
    Last Post: 08-20-2012, 12:59 PM
  2. problem with ipv6 sockets on bind
    By mushy in forum C Programming
    Replies: 1
    Last Post: 10-06-2010, 11:56 AM
  3. Bind problem
    By moment in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-24-2009, 11:36 AM
  4. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 04:27 PM
  5. Socket Datagram info sendto(), recvfrom()
    By siluro in forum C Programming
    Replies: 1
    Last Post: 02-20-2005, 12:10 PM

Tags for this Thread