网络编程
代码:
/*************************************************************************************
*
* file name: 1.c
* author : [email protected]
* date : 2024/06/04
* function : 多播通信
* note : none
* CopyRight (c) 2024 [email protected] All Right Reserved
*
************************************************************************************/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
int udp_socket;
char w_buf[52]={0};
// 运行客户端可执行文件 ./xxx 服务器端口 服务器地址
/**
* @name: sendm
* @brief 向目标主机发送消息
* @param
@void *arg
* @retval void *
* @date 2024/06/04
* @note none
*/
void *sendm(void *arg)
{
//2.向目标主机发送消息,需要设置目标端口和目标地址
char buf[128];
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET; //协议族,是固定的
dest_addr.sin_port = htons(60000); //服务器端口,必须转换为网络字节序
dest_addr.sin_addr.s_addr = inet_addr("226.66.66.66"); //服务器地址 "192.168.64.xxx"
while(1)
{
scanf("%s",buf);
sendto(udp_socket,buf,strlen(buf),0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
}
return 0;
}
/**
* @name: gettime
* @brief 获取系统时间
* @param
@void *arg
* @retval void *
* @date 2024/06/04
* @note none
*/
void *gettime(void *arg)
{
int sec,min,hour,day,week,mon,year;
// while(1)
// {
//利用循环获取系统时间并将它存入变量
time_t time_sum = time(NULL);
struct tm *CurTime = localtime(&time_sum);
sec = CurTime->tm_sec;
min = CurTime->tm_min ;
hour = CurTime->tm_hour ;
day = CurTime->tm_mday ;
week = CurTime->tm_wday ;
mon = CurTime-> tm_mon +1;
year = CurTime-> tm_year +1900;
//将变量里的整数用spritf函数全部转换成字符串
sprintf(w_buf,"%d年%d月%d日,星期%d %d:%d:%d",year,mon,day,week,hour,min,sec);
}
/**
* @name: recvm
* @brief 向目标主机发送消息
* @param
@void *arg
* @retval void *
* @date 2024/06/04
* @note none
*/
void *recvm(void *arg)
{
//2.向目标主机发送消息,需要设置目标端口和目标地址
//char buf[128];
struct sockaddr_in host_addr;
host_addr.sin_family = AF_INET; //协议族,是固定的
host_addr.sin_port = htons(60000); //服务器端口,必须转换为网络字节序
host_addr.sin_addr.s_addr = htonl(INADDR_ANY);
struct ip_mreqn ip_m;
ip_m.imr_multiaddr.s_addr= inet_addr("226.66.66.66");
ip_m.imr_address.s_addr= inet_addr("192.168.64.100");
ip_m.imr_ifindex= 0;
setsockopt(udp_socket,IPPROTO_IP,IP_ADD_MEMBERSHIP,&ip_m, sizeof(ip_m));
bind(udp_socket,(struct sockaddr *)&host_addr, sizeof(host_addr));
//3.发送客户端的上线时间
//3.调用recvfrom等待接收数据,并且接收客户端的网络信息
char buf[256] = {0};
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
int flag =1;
setsockopt(udp_socket,SOL_SOCKET,SO_BROADCAST,&flag, sizeof(ip_m));
while(1)
{
recvfrom(udp_socket,buf,sizeof(buf), 0 ,(struct sockaddr *)&client,&client_len); //默认会阻塞
gettime();
printf("client ip is [%s],time is [%s]:%s\n",inet_ntoa(client.sin_addr),w_buf,buf);
bzero(buf,sizeof(buf));
}
return 0;
}
int main(int argc,char *argv[])
{
//1.创建UDP套接字
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (udp_socket == -1)
{
fprintf(stderr, "udp socket error,errno:%d,%s\n",errno,strerror(errno));
exit(1);
}
pthread_t thread;
pthread_t thread1;
// pthread_t thread2;
pthread_create(&thread, NULL, sendm, NULL);
pthread_create(&thread1, NULL, recvm, NULL);
// pthread_create(&thread2, NULL, gettime, NULL);
while(1);
return 0;
}