- 论坛徽章:
- 0
|
/*******begin of file multi_send.c********/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#define MYPORT 20000
#define MYPORT1 20002
#define BUFFSIZE 128
int sendmessage_m(int nsock, char *message, int len);
int recvmessage_m(int nsock, char *message);
int main(int argc,char *argv[])
{
int sockfd;
char m_ttl = 16;
int sock_reuse = 1;
char sd_msg[BUFFSIZE], rc_msg[BUFFSIZE];
struct sockaddr_in sender_addr;
//struct in_addr addr1;
struct sockaddr_in addr1;
struct ip_mreq multicast;
unsigned long addr = htonl(INADDR_ANY);
if (argc != 2)
{
printf("useage <multicast ip>\n" ;
exit(1);
}
sender_addr.sin_family = AF_INET;
sender_addr.sin_port = htons(MYPORT1);
sender_addr.sin_addr.s_addr = htonl(INADDR_ANY);
multicast.imr_multiaddr.s_addr = inet_addr(argv[1]);
multicast.imr_interface.s_addr = htonl(INADDR_ANY);
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("sender:new socket error!\n" ;
exit(1);
}
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&sock_reuse,sizeof(sock_reuse)) < 0)
{
perror("server : socket options set error" ;
exit(1);
}
if (bind(sockfd, (struct sockaddr *)&sender_addr, sizeof(sender_addr)) < 0)
{
close(sockfd);
perror("server : socket bind error" ;
exit(1);
}
if (setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_TTL,(char *)&m_ttl, sizeof(m_ttl)) < 0)
{
perror("TTL:" ;
printf("sender:set ip_multicast_ttl error!\n" ;
exit(1);
}
/**
addr1.sin_family = AF_INET;
addr1.sin_port = htons(MYPORT);
addr1.sin_addr.s_addr = htonl(INADDR_ANY);
if (setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_IF,(char *)&addr1,sizeof(addr1)) < 0)
{
perror("MULTICAST_IF" ;
printf("sender:set ip_multicast_if error!\n" ;
exit(1);
}
*/
if (setsockopt(sockfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char *)&multicast,sizeof(multicast)) < 0)
{
printf("sender:add_membership error!\n" ;
exit(1);
}
printf("sender has multicasted %s on port 20000!\n",argv[1]);
strcpy(sd_msg,"Hello World!" ;
if (sendmessage_m(sockfd,sd_msg,strlen(sd_msg)) < 0)
{
close(sockfd);
printf("sender:send message error!\n");
exit(1);
}
/**
if (recvmessage_m(sockfd, rc_msg) < 0)
{
close(sockfd);
printf("recver:receive message error!\n");
}
*/
close(sockfd);
return 0;
}
int sendmessage_m(int nsock,char *message,int len)
{
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(MYPORT);
client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
message[strlen(message)] = 0;
if (sendto(nsock, message, len, 0, (struct sockaddr *)&client_addr, size
of(struct sockaddr)) < 0)
{
return -1;
}
printf("sender send message: %s\n",message);
return 0;
}
int recvmessage_m(int nsock, char *message)
{
int n,source_len;
struct sockaddr_in source_addr;
if((n = recvfrom(nsock,message,BUFFSIZE,0,(struct sockaddr *)&source_add
r,(socklen_t *)&source_len)) < 0)
{
return -1;
}
message[n] = 0;
printf("recver receive message: %s ",message);
return 0;
}
/*****end of file multi_send.c*****/
/*****begin of file muli_recv.c*****/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MYPORT 20000
#define BUFFSIZE 128
int recvmessage_m(int nsock,char *message);
int main(int argc,char *argv[])
{
int sockfd;
int sock_reuse = 1;
char rc_msg[BUFFSIZE];
struct ip_mreq multicast;
struct sockaddr_in recver_addr;
if (argc != 2)
{
printf("useage <multicast ip>\n");
exit(1);
}
multicast.imr_multiaddr.s_addr = inet_addr(argv[1]);
multicast.imr_interface.s_addr = htonl(INADDR_ANY);
recver_addr.sin_family = AF_INET;
recver_addr.sin_port = htons(MYPORT);
recver_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(recver_addr.sin_zero), ;
if ((sockfd=socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("recver:new a socket error!\n");
exit(1);
}
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&sock_reuse,sizeof
(sock_reuse)) < 0)
{
perror("server : socket options set error");
exit(1);
}
if (bind(sockfd,(struct sockaddr *)&recver_addr,sizeof(recver_addr)) < 0
)
{
close(sockfd);
printf("recver:bind socket error!\n");
exit(1);
}
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char *)&multicast,
sizeof(multicast)) <0)
{
printf("recver:add_membership error!\n");
exit(1);
}
printf("recver has joined %s listen on 20000\n",argv[1]);
if (recvmessage_m(sockfd, rc_msg) < 0)
{
close(sockfd);
printf("recver:recvive message error!\n");
exit(1);
}
return 0;
}
int recvmessage_m(int nsock, char *message)
{
int n,source_len;
struct sockaddr_in source_addr;
if ((n = recvfrom(nsock, message, BUFFSIZE, 0, (struct sockaddr *)&sourc
e_addr, (socklen_t *)&source_len)) < 0)
{
return -1;
}
message[n] = 0;
printf("recver receive message %s\n",message);
return 0;
}
/*******end of file multi_recv.c******/ |
|