#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror("创建udp套接字失败\n");
exit(-1);
}
struct sockaddr_in dest_addr;
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(8080);
inet_pton(AF_INET, "192.168.50.49", &dest_addr.sin_addr.s_addr);
socklen_t destlen = sizeof(dest_addr);
char buf[128] = "";
while (1)
{
printf("输入发送的数据\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
int ret = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&dest_addr, destlen);
if (ret < 0)
{
perror("发送数据失败\n");
exit(-1);
}
}
close(sockfd);
return 0;
}