int sockfd; /* 数据端口 */
struct sockaddr_in my_addr; /* 自身的地址信息 */
struct sockaddr_in their_addr; /* 连接对方的地址信息 */
int sin_size, retval;
char buf[128];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); /* 网络字节顺序 */
my_addr.sin_addr.s_addr = INADDR_ANY; /* 自动填本机IP */
bzero(&(my_addr.sin_zero), 8); /* 其余部分置0 */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
perror("bind");
exit(1);
}
/* 主循环 */
while(1) {
retval = recvfrom(sockfd, buf, 128, 0, (struct sockaddr
*)&their_addr, &sin_size);
printf("Received datagram from
%s\n",inet_ntoa(their_addr.sin_addr));
if (retval == 0) {
perror (“recvfrom");
close(sockfd);
break;
}
retval = sendto(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr,
sin_size);
}