2024.8.21

#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);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值