69,382
社区成员
发帖
与我相关
我的任务
分享
/**********************************************************************
* Compiler: GCC
* Last Update: Mon 14 May 2012 08:40:24 PM CST
* File Name: myServerMulticast2.c
* Description: 多播客户端
* Usage: myServerMulticast2 多播地址 [端口号(默认为PORT)]
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#define MAXBUF 1024
#define PORT 60000
int main(int argc, char **argv)
{
int myFd;
struct sockaddr_in mCastAddr;
int addrlen = sizeof(mCastAddr);
char buf[MAXBUF];
bzero(&mCastAddr, sizeof(mCastAddr));
bzero(buf, sizeof(buf));
//创建套接字
if((myFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
printf("socket()错误码:%d, %s\n", errno, strerror(errno));
exit(errno);
}
mCastAddr.sin_family = AF_INET;
if (argc > 1) { //IP地址
int res;
if ((res = inet_pton(AF_INET, argv[1], &mCastAddr.sin_addr)) == -1) {
printf("inet_pton()错误码:%d, %s\n", errno, strerror(errno));
exit(errno);
} else if(res == 0) {
printf("IP地址无效\n");
printf("请输入一个多播地址:224.0.0.1 ~ 239.255.255.255\n");
return 0;
}
} else {
printf("Usage: %s 多播IP地址\n", argv[0]);
return 0;
}
if (argc == 3) {
mCastAddr.sin_port = htons(atoi(argv[2]));
} else {
mCastAddr.sin_port = htons(PORT);
}
while (1) {
bzero(buf, sizeof(buf));
printf("请输入要发送的消息:");
if (fgets(buf, sizeof(buf), stdin) == NULL) {
exit(0);
}
if (sendto(myFd, buf, strlen(buf) + 1, 0,
(struct sockaddr *)&mCastAddr, sizeof(mCastAddr)) == -1) {
printf("sendto()错误码:%d, %s\n", errno, strerror(errno));
exit(errno);
}
printf("成功给对方发送消息:%s", buf);
}
return 0;
}
/**********************************************************************
* Compiler: GCC
* Last Update: Sat 12 Nov 2011 11:17:13 PM CST
* File Name: myClientMulticast2.c
* Description:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#define _DEBUG
#define MAXBUF 1024
#define PORT 60000
int main(int argc, char **argv)
{
int myFd, clientFd;
struct sockaddr_in myAddr, clientAddr;
struct ip_mreq mreq;
int addrlen = sizeof(clientAddr);
char buf[MAXBUF];
char addrTemp[INET_ADDRSTRLEN];
bzero(&myAddr, sizeof(myAddr));
bzero(&clientAddr, sizeof(clientAddr));
//创建套接字,datagrams
if((myFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
printf("%s Line number:%d socket()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
//设置本地地址端口信息
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY;
if (argc == 3) {
myAddr.sin_port = htons(atoi(argv[2]));
} else {
myAddr.sin_port = htons(PORT);
}
//绑定本地套接字
if(bind(myFd, (struct sockaddr *)&myAddr, sizeof(myAddr)) != 0) {
printf("%s Line number:%d bind()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
//设置IP_MULTICAST_LOOP
u_char loop = 1;
if (setsockopt(myFd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) != 0) {
printf("%s Line number:%d setsockopt()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
//设置IP_MULTICAST_TTL 为1,即该多播最多只能经过一个路由器
u_char ttl = 1;
if (setsockopt(myFd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) != 0) {
printf("%s Line number:%d setsockopt()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
//对struct ip_mreq这个结构体赋值
if (argc > 1) { //设置IP地址
int res;
if ((res = inet_pton(AF_INET, argv[1], &mreq.imr_multiaddr)) == -1) {
printf("%s Line number:%d setsockopt()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
} else if(res == 0) {
printf("IP地址无效\n");
printf("请输入一个多播地址:224.0.0.1 ~ 239.255.255.255\n");
return 0;
}
} else {
printf("Usage:%s 多播地址(224.0.0.1 ~ 239.255.255.255)\n", argv[0]);
return(0);
}
mreq.imr_interface.s_addr = INADDR_ANY;
//将本机加入多播组中
if (setsockopt (myFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
printf("%s Line number:%d setsockopt()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
printf("客户端已启动,等待多播消息.....\n");
while (1) {
bzero(buf, sizeof(buf));
if (recvfrom(myFd, buf, sizeof(buf) - 1, 0,
(struct sockaddr *)&clientAddr, &addrlen) == -1) {
printf("%s Line number:%d recvfrom()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
printf("收到来自%s的消息:%s",
inet_ntop(AF_INET, &clientAddr.sin_addr, addrTemp, sizeof(addrTemp)), buf);
if (strcmp("exit\n", buf) == 0) {
break;
}
}
//退出广播组
if (setsockopt (myFd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
printf("%s Line number:%d setsockopt()错误码:%d, %s\n", \
__FILE__, __LINE__, errno, strerror(errno));
exit(errno);
}
return 0;
}