我正在编写一个带有ACK的发送方 - 接收方协议。每当发送方发送一个数据包时,接收方将发送一个相应的ACK,然后等待下一个包。问题在于,由于我在同一台机器上测试所有内容(使用127.0.0.1
作为地址),接收器将在无限循环中接收自己的ACK。我做错了什么?
以下是接收方的代码(全部位于while
循环内):
if ((recvfrom(sockfd, buff, PACKETSZ + 1, 0, (struct sockaddr *) &sndaddr, &len)) < 0) {
perror("recvfrom");
return -1;
}
fprintf(stderr, "Packet received: '%s'.\n", buff);
// Get sequence number (seqn) from the packet
char answer[PACKETSZ];
snprintf(answer, PACKETSZ, "%d-ACK", seqn);
printf("Sending '%s'\n", answer);
if (sendto(sockfd, answer, strlen(answer) + 1, 0, (struct sockaddr *) &sndaddr, sizeof(sndaddr)) < 0) {
perror("sendto");
return -1;
}
我如何阻止流程接收自己发送的数据报?
答案 0 :(得分:0)
I don't think you can keep the socket from receiving the message in this situation, rather you need to use the address parameter to recvfrom and ignore the message if it came from your own socket.