梦醒潇湘love 发表于 2012-12-28 10:54

sendto error:Address family not supported by protocol

   程序代码如下所示:#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <strings.h>
#include <string.h>

#define MCAST_PORT 8888
#define MCAST_ADDR "224.0.0.88"

int main(int argc, char **argv)
{
        struct sockaddr_in mcast_addr, client_addr;
        struct sockaddr_in temp_addr;
        int sockfd;
       
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(sockfd == -1)
        {
                fprintf(stderr, "socket() error.\n");
                exit(EXIT_FAILURE);
        }
       
        //初始化多播地址
        bzero(&mcast_addr, sizeof(mcast_addr));
        mcast_addr.sin_family = AF_INET;
        mcast_addr.sin_family = htons(MCAST_PORT);
        mcast_addr.sin_addr.s_addr = inet_addr(MCAST_ADDR);

        char send_buf = {0};       
        int i = 1;
        while(1)
        {
                memset(send_buf, 0, sizeof(send_buf));
                sprintf(send_buf, "%d", i);
                int send_num = sendto(sockfd, send_buf,        sizeof(send_buf), 0, (struct sockaddr *)&mcast_addr, sizeof(mcast_addr));
                if(send_num < 0)
                {
                        fprintf(stderr, "sendto() error.\n");
                        printf("the error is: %d, 具体内容为:%s\n", errno, strerror(errno));
                        exit(EXIT_FAILURE);
                }
                else
                {
                        printf("send message: %s\n", send_buf);
                }
                sleep(1);
               
                i++;
                memset(send_buf, 0, sizeof(send_buf));
               
                int temp_len = sizeof(struct sockaddr);
                send_num = recvfrom(sockfd, send_buf, 1024, 0, (struct sockaddr *)&temp_addr, &temp_len);
                printf("receive message: %s\n", send_buf);
        }

        close(sockfd);
        return 0;
}
错误如下:


上网查找资料,没有解决问题,因此来请教。。

梦醒潇湘love 发表于 2012-12-28 11:06

版主,拯救一下小弟。。。:'(

linux_c_py_php 发表于 2012-12-28 12:39

      mcast_addr.sin_family = AF_INET;
      mcast_addr.sin_family = htons(MCAST_PORT);

梦醒潇湘love 发表于 2012-12-28 14:36

回复 3# linux_c_py_php

非常感谢,这种错误,哎,以后要仔细。



   
页: [1]
查看完整版本: sendto error:Address family not supported by protocol