【问题标题】:UDP socket (DGRAM) bind/sendto errorUDP 套接字 (DGRAM) 绑定/发送错误
【发布时间】:2014-10-31 23:11:06
【问题描述】:

我是 UDP 套接字的新手,我以前使用过 TCP。我的客户端好像无法连接到我的服务器,但我不知道问题出在哪里。

当我运行我的服务器时,它看起来一切正常。编译并运行没有问题,并等待来自客户端的消息。

另一方面,客户端失败,编译没有问题,但在运行时在 bind() 上抛出错误。我在其他地方看到绑定并不总是必要的,所以我也尝试删除它,但是当我这样做时,错误出现在 sendto() 上。我使用 perror() 来尝试定位问题。在 bind 中,消息是“Address already in use”,在 sendto 中是“Address family not supported by protocol”。

我不知道我的方法是否错误。我尝试了几种方法来做到这一点,但似乎没有任何效果。任何帮助将不胜感激。

服务器代码:

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);
}

客户端代码:

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);
}

【问题讨论】:

标签: c sockets udp bind sendto


【解决方案1】:

您不能在客户端和服务器程序中绑定到相同的端口(在同一台机器上)——只有其中一个可以拥有该端点。

另外你的参数 sendto() 看起来不对,它被定义为:

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

【讨论】:

  • 对不起,我的回答迟了..是的,它是端口..我没有填写正确的结构来获得与服务器的连接..我正在填写 dest,它必须是 sa。 ..我改变了它,它工作得很好
猜你喜欢
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
相关资源
最近更新 更多