#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
//1.创建套接字
int sdf = socket(AF_INET,SOCK_DGRAM,0);
if(sdf == -1)
{
perror("socket");
return -1;
}
printf("socket创建成功\n");
//2.定义目标主机的ip地址和端口号
struct sockaddr_in dstaddr;
dstaddr.sin_family = AF_INET;
dstaddr.sin_addr.s_addr = inet_addr("10.12.152.137");
dstaddr.sin_port = htons(8080);
char buf[128] = "卡是卡卡洒家洒家";
for(int i = 0;i < 10;i++)
{
ssize_t len = sendto(sdf,buf,strlen(buf),0,(struct sockaddr *)&dstaddr
,sizeof(dstaddr));
if(len != -1)
{
printf("发送数据成功\n");
}
sleep(1);
}
close(sdf);
return 0;
}
12-15
338
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-25
4048
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-09
1911
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)