#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
/*
struct ip_mreq
{
struct in_addr imr_multiaddr; //组播地址
struct in_addr imr_interface; //本机地址
}
struct in_addr imr_multiaddr
{
s_addr;//组播地址
}
struct in_addr imr_interface
{
s_addr;//本机地址
}
*/
void *mysend(void *arg)
{
int sock_fd = *(int*)arg;
char *buf=malloc(100);
while(1)
{
memset(buf,0,100);
fgets(buf,100,stdin);
printf("服务器发送消息:%s\n",buf);
}
}
int main(int argc, char **argv)
{
//1.创建套接字
int sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//2.设置套接字的属性
//定义组播地址结构体,给组播地址成员赋值,给本机地址成员赋值
struct ip_mreq zubo;
zubo.imr_multiaddr.s_addr = inet_addr("224.10.10.10");
zubo.imr_interface.s_addr = INADDR_ANY;
//3.设置套接字的属性,加入组播组
struct sockaddr_in local_addr,oppsite;
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(atoi(argv[1]));
local_addr.sin_addr.s_addr = INADDR_ANY;
socklen_t zubo_len = sizeof(zubo);
socklen_t local_len= sizeof(local_addr);
socklen_t oppsite_len= sizeof(oppsite);
setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &zubo, zubo_len);
//
bind(sock_fd, (struct sockaddr*)&local_addr,local_len);
char *buf=malloc(100);
pthread_t pid;
pthread_create(&pid,NULL,mysend,&sock_fd);
while(1)
{
memset(buf,0,100);
recvfrom(sock_fd,buf,100,0,(struct sockaddr *)&oppsite,&oppsite_len);
if(strncmp(buf,"start",strlen("start"))==0)
{
printf("ip地址:%s,端口号:%d,上线了\n",inet_ntoa(oppsite.sin_addr),ntohs(oppsite.sin_port));
continue;
}
if(strncmp(buf,"stop",strlen("stop"))==0)
{
printf("ip地址:%s,端口号:%d,下线了\n",inet_ntoa(oppsite.sin_addr),ntohs(oppsite.sin_port));
continue;
}
printf("消息:%s\n,ip地址:%s\n,端口号:%d\n",buf,inet_ntoa(oppsite.sin_addr),ntohs(oppsite.sin_port));
}
}
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
int sock_fd;
socklen_t zubo_len;
struct sockaddr_in zubo;
/*
struct ip_mreq
{
struct in_addr imr_multiaddr; //组播地址
struct in_addr imr_interface; //本机地址
}
struct in_addr imr_multiaddr
{
s_addr;//组播地址
}
struct in_addr imr_interface
{
s_addr;//本机地址
}
*/
void func(int sig)
{
int a=sendto(sock_fd,"stop",strlen("stop"),0,(struct sockaddr *)&zubo,zubo_len);
if(a==-1)
{
perror("sendto");
}
exit(0);
}
int main(int argc, char **argv)
{
//1.创建套接字
sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//2.设置套接字的属性
//定义组播地址结构体,给组播地址成员赋值,给本机地址成员赋值
zubo.sin_family = AF_INET;
zubo.sin_port = htons(atoi(argv[1]));
zubo.sin_addr.s_addr = inet_addr("224.10.10.10");
zubo_len = sizeof(zubo);
bind(sock_fd, (struct sockaddr*)&zubo, zubo_len);
char *buf=malloc(100);
sendto(sock_fd,"start",strlen("start"),0,(struct sockaddr *)&zubo,zubo_len);
signal(SIGINT,func);
while(1)
{
memset(buf,0,100);
fgets(buf,100,stdin);
buf[strcspn(buf,"\n")]=0;
sendto(sock_fd,buf,100,0,(struct sockaddr *)&zubo,zubo_len);
}
}
04-10
1110
07-03
638
08-16
340