分类: LINUX
2009-06-15 16:48:43
#include #include #include #include #include #include #define BUFLEN 255 int main(int argc, char **argv) { struct sockaddr_in peeraddr, myaddr; int sockfd; char recmsg[BUFLEN + 1]; unsigned int socklen; /* 创建 socket 用于UDP通讯 */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { printf("socket creating error\n"); exit(1); } socklen = sizeof(struct sockaddr_in); /* 设置对方的端口和IP信息 */ memset(&peeraddr, 0, socklen); peeraddr.sin_family = AF_INET; if (argv[2]) peeraddr.sin_port = htons(atoi(argv[2])); else peeraddr.sin_port = htons(7838); if (argv[1]) { /* 注意这里设置的对方地址是指组播地址,而不是对方的实际IP地址 */ if (inet_pton(AF_INET, argv[1], &peeraddr.sin_addr) <= 0) { printf("wrong group address!\n"); exit(0); } } else { printf("no group address!\n"); exit(0); } /* 设置自己的端口和IP信息 */ memset(&myaddr, 0, socklen); myaddr.sin_family = AF_INET; if (argv[4]) myaddr.sin_port = htons(atoi(argv[4])); else myaddr.sin_port = htons(23456); if (argv[3]) { if (inet_pton(AF_INET, argv[3], &myaddr.sin_addr) <= 0) { printf("self ip address error!\n"); exit(0); } } else myaddr.sin_addr.s_addr = INADDR_ANY; /* 绑定自己的端口和IP信息到socket上 */ if (bind (sockfd, (struct sockaddr *) &myaddr, sizeof(struct sockaddr_in)) == -1) { printf("Bind error\n"); exit(0); } /* 循环接受用户输入的消息发送组播消息 */ for (;;) { /* 接受用户输入 */ bzero(recmsg, BUFLEN + 1); if (fgets(recmsg, BUFLEN, stdin) == (char *) EOF) exit(0); /* 发送消息 */ if (sendto (sockfd, recmsg, strlen(recmsg), 0, (struct sockaddr *) &peeraddr, sizeof(struct sockaddr_in)) < 0) { printf("sendto error!\n"); exit(3); } printf("'%s' send ok\n", recmsg); } } |
#include #include #include #include #include #include #include #include #define BUFLEN 255 int main(int argc, char **argv) { struct sockaddr_in peeraddr; struct in_addr ia; int sockfd; char recmsg[BUFLEN + 1]; unsigned int socklen, n; struct hostent *group; struct ip_mreq mreq; /* 创建 socket 用于UDP通讯 */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { printf("socket creating err in udptalk\n"); exit(1); } /* 设置要加入组播的地址 */ bzero(&mreq, sizeof(struct ip_mreq)); if (argv[1]) { if ((group = gethostbyname(argv[1])) == (struct hostent *) 0) { perror("gethostbyname"); exit(errno); } } else { printf ("you should give me a group address, 224.0.0.0-239.255.255.255\n"); exit(errno); } bcopy((void *) group->h_addr, (void *) &ia, group->h_length); /* 设置组地址 */ bcopy(&ia, &mreq.imr_multiaddr.s_addr, sizeof(struct in_addr)); /* 设置发送组播消息的源主机的地址信息 */ mreq.imr_interface.s_addr = htonl(INADDR_ANY); /* 把本机加入组播地址,即本机网卡作为组播成员,只有加入组才能收到组播消息 */ if (setsockopt (sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(struct ip_mreq)) == -1) { perror("setsockopt"); exit(-1); } socklen = sizeof(struct sockaddr_in); memset(&peeraddr, 0, socklen); peeraddr.sin_family = AF_INET; if (argv[2]) peeraddr.sin_port = htons(atoi(argv[2])); else peeraddr.sin_port = htons(7838); if (argv[1]) { if (inet_pton(AF_INET, argv[1], &peeraddr.sin_addr) <= 0) { printf("Wrong dest IP address!\n"); exit(0); } } else { printf("no group address given, 224.0.0.0-239.255.255.255\n"); exit(errno); } /* 绑定自己的端口和IP信息到socket上 */ if (bind (sockfd, (struct sockaddr *) &peeraddr, sizeof(struct sockaddr_in)) == -1) { printf("Bind error\n"); exit(0); } /* 循环接收网络上来的组播消息 */ for (;;) { bzero(recmsg, BUFLEN + 1); n = recvfrom(sockfd, recmsg, BUFLEN, 0, (struct sockaddr *) &peeraddr, &socklen); if (n < 0) { printf("recvfrom err in udptalk!\n"); exit(4); } else { /* 成功接收到数据报 */ recmsg[n] = 0; printf("peer:%s", recmsg); } } } |