-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_client.cpp
More file actions
83 lines (76 loc) · 1.98 KB
/
udp_client.cpp
File metadata and controls
83 lines (76 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* ==================================================
* @file udp_client.cpp
* @brief udp客户端
* @author ywj
* @date 2025-06-18 22:36
* @version 1.0
* @copyright Copyright (c) 2025 ywj. All Rights Reserved.
* ==================================================
*/
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#define LOG_INFO(format, ...) \
do { \
time_t now = time(NULL); \
struct tm *tm_info = localtime(&now); \
char timestamp[20]; \
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info); \
printf("[INFO %s] " format "\n", timestamp, ##__VA_ARGS__); \
} while (0)
#define LOG_ERROR(format, ...) \
do { \
time_t now = time(NULL); \
struct tm *tm_info = localtime(&now); \
char timestamp[20]; \
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info); \
printf("[ERROR %s] " format "\n", timestamp, ##__VA_ARGS__); \
} while (0)
//定义socket
int udp_sock=-1;
//中断信号
void handler_NT(int signo)
{
LOG_INFO("process exit!\n");
close(udp_sock);
}
//关机信号
void handler_TERM(int signo)
{
LOG_INFO("process exit!\n");
close(udp_sock);
}
int main(int argc,char**argv)
{
signal(SIGINT,handler_NT);
signal(SIGTERM,handler_NT);
//接收方的地址
sockaddr_in accAddr;
accAddr.sin_family=AF_INET;
inet_aton("192.168.1.3",&accAddr.sin_addr);
accAddr.sin_port=htons(9696);
udp_sock=socket(AF_INET,SOCK_DGRAM,0);
//本机地址
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(9658);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
// char buffer[1024];
const char*data="hello world!";
if (bind(udp_sock,(sockaddr*)&addr,sizeof(addr))<0)
{
LOG_ERROR("bind error!\n");
exit(-1);
}
sendto(udp_sock,data,strlen(data),0,(struct sockaddr *)&accAddr,sizeof(accAddr));
close(udp_sock);
return 0;
}