udp通信
int main()
{
// 创建套接字
int fd = socket(AF_INET, SOCK_DGRAM, 0);
perror("socket");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("192.168.68.152");
addr.sin_port = htons(8888);
socklen_t l = sizeof(addr);
bind(fd, (struct sockaddr*)&addr, l);
perror("bind");
char buf[1000];
// while (1)
{
bzero(buf, 1000);
recvfrom(fd, buf, 1000, 0, (struct sockaddr*)&addr, &l);
perror("recvfrom");
sendto(fd, buf, 1000, 0, (struct sockaddr*)&addr, l);
}
close(fd);
return 0;
}

浙公网安备 33010602011771号