Search code examples
csocketsudpbindsendto

UDP socket (DGRAM) bind/sendto error


I'm new to UDP sockets, I have worked with TCP before. It seems my client can't connect to my server, but I don't know where is the problem.

When I run my server, it looks all is working fine. Compiles and runs without a problem and waits for the message from the client.

The client on the other hand is failing, compiles without a problem but in the runtime throws me an error on bind(). I've seen in other places that bind is not always necessary so I also try to remove it, but when I do this the error appears on sendto(). I use perror() to try to locate the problem. In bind, the message is "Address already in use", and in sendto it is "Address family not supported by protocol".

I don't know if my approach is wrong. I've tried several ways to do it but nothing seems to work. Any help would be greatly appreciated.

Server code:

int main (){
    int sockfd, newsockfd;
    int portno;
    socklen_t tamcli;
    struct sockaddr_in dest, sa;
    char* mensaje;

    bzero((char *) &dest, sizeof(dest));
    portno = 5001;
    mensaje = (char*)malloc(sizeof(char)*100);

    sockfd = socket(PF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0){
        printf("ERROR al abrir socket\n");
        perror("sockto");
        exit(1);
    }

    dest.sin_family = AF_INET;
    dest.sin_port = htons(portno);
    dest.sin_addr.s_addr = INADDR_ANY;
    tamcli = sizeof(sa);

    if (bind(sockfd, (struct sockaddr *) &dest, sizeof(dest)) < 0){
        printf("ERROR en enlazar\n");
        perror("bind");
        exit(1);
    }


    newsockfd = recvfrom(sockfd, mensaje, sizeof(mensaje), 0,(struct sockaddr *)&sa, &tamcli);
    if (newsockfd < 0){
        printf("ERROR en aceptar el mensaje\n");
        perror("recvfrom");
        exit(1);
    }

    printf("El mensaje del cliente fue: %s\n", mensaje);

    close(sockfd);
}

Client code:

int main (){
    int sockfd, newsockfd;
    int portno;
    socklen_t tamcli;
    struct sockaddr_in dest, sa;
    char* mensaje;

    bzero((char *) &dest, sizeof(dest));
    portno = 5001;
    mensaje = (char*)malloc(sizeof(char)*100);
    tamcli = sizeof(sa);

    mensaje = "Hola";

    sockfd = socket(PF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0){
        printf("ERROR al abrir socket\n");
        perror("socket");
        exit(1);
    }

    dest.sin_family = AF_INET;
    dest.sin_port = htons(portno);
    if (inet_aton("127.0.0.1", &dest.sin_addr) == 0){
        printf("Error conectandose a la direccion");
        perror("inet_aton");
        exit(1);
    }

    if (bind(sockfd, (struct sockaddr *) &dest, sizeof(dest)) < 0){
        printf("ERROR en enlazar\n");
        perror("bind");
        exit(1);
    }


    newsockfd = sendto(sockfd, mensaje, sizeof(mensaje), 0, (struct sockaddr *) &sa, tamcli);
    if (newsockfd < 0){
        printf("ERROR en enviar el mensaje\n");
        perror("sendto");
        exit(1);
    }

    close(sockfd);
}

Solution

  • You can not bind to the same port (on the same machine) in both your client and server programs -- only one of them can own that endpoint.

    Also your args to sendto() look wrong, it is defined as:

     ssize_t sendto(int s, const void *msg, size_t len, int flags,
                    const struct sockaddr *to, socklen_t tolen);