gpt4 book ai didi

c - 如何从接收方向发送方发送NACK消息

转载 作者:行者123 更新时间:2023-12-03 11:54:49 31 4
gpt4 key购买 nike

我正在尝试在Visual Studio的C语言中创建应用程序,其中一个“发件人”将向多个“收件人”发送多个消息,在一些随机ID上,“收件人”会收到NACK消息并将NACK发送给“发件人”以通知他发送再次发送消息(仅通过字符串),“发件人”再次发送该消息,如果他在接下来的3秒钟内未收到NACK消息,则该消息将被视为已成功发送给“接收者”。

我的问题是:'Recevier'成功获取NACK消息,但是当我尝试告诉'Sender'我已经收到NACK消息时,该字符串显示的内容不是我要发送给他的字符串。
你能告诉我我在做什么错吗?

发件人:

#include <Winsock2.h>
#define _WINSOCK_DEPCRECATED_NO_WARNINGS
#include <sys/types.h> /* for type definitions */
#include <winsock.h>
#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for strlen() */
#include <time.h>

#define MAX_LEN 1000 /* maximum string size to send */
#define MIN_PORT 1024 /* minimum port allowed */
#define MAX_PORT 65535 /* maximum port allowed */
#define IP_MULTICAST_TTL 10

int main(int argc, char *argv[]) {

int sock; /* socket descriptor */
char send_str[MAX_LEN]; /* string to send */
struct sockaddr_in mc_addr; /* socket address structure */
int send_len; /* length of string to send */
char* mc_addr_str; /* multicast IP address */
int mc_port; /* multicast port */
char mc_ttl = 1; /* time to live (hop count) */
WSADATA wsaData;
int counter = 0;
struct sockaddr_in from_addr;
int recv_len;
char recv_str[MAX_LEN + 1];
int from_len;

/* Load Winsock 2.0 DLL */
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup() failed");
exit(1);
}

/* validate number of arguments */
if (argc != 3) {

fprintf(stderr, "Usage: %s <Multicast IP> <Multicast Port>\n",
argv[0]);
// exit(1);

}

mc_addr_str = argv[1]; /* arg 1: multicast IP address */
mc_port = atoi(argv[2]); /* arg 2: multicast port number */

/* validate the port range */
if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
fprintf(stderr, "Invalid port number argument %d.\n", mc_port);
fprintf(stderr, "Valid range is between %d and %d.\n",
MIN_PORT, MAX_PORT);
exit(1);

}

/* create a socket for sending to the multicast address */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket() failed");
exit(1);

}

/* set the TTL (time to live/hop count) for the send */
if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL,
(void*)&mc_ttl, sizeof(mc_ttl))) < 0) {
perror("setsockopt() failed");
exit(1);

}

/* construct a multicast address structure */
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family = AF_INET;
mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
mc_addr.sin_port = htons(mc_port);

printf("Write a message to send:\n");
for(counter; counter < 70; counter++){

/* clear send buffer */
memset(send_str, 0, sizeof(send_str));

/* while (*/fgets(send_str, MAX_LEN, stdin);/*) {*/

printf("Write a message to send:\n");
counter++;
send_len = strlen(send_str);

if ((sendto(sock, send_str, send_len, 0,
(struct sockaddr *) &mc_addr,
sizeof(mc_addr))) != send_len) {

perror("sendto() sent incorrect number of bytes");
/*exit(1);*/
}
else
{
if (rand() % 5 == 0) {
recv_len = recvfrom(sock, (char *)recv_str, 1024, 0,
(struct sockaddr*)&mc_addr, sizeof(&mc_addr));

printf("\nMessage ID %d\n and message: %s\n",recv_str);
}
}
}
/* clear send buffer */
memset(send_str, 0, sizeof(send_str));

/*closesocket(sock);*/
close(sock);
exit(0);

}

接收者:

#include <WinSock2.h> 
#define _WINSOCK_DEPCRECATED_NO_WARNINGS
#include <sys/types.h> /* for type definitions */
#include <winsock.h>
#include <time.h>
#include <stdio.h>/* for printf() and fprintf() */
#include <stdlib.h>/* for atoi() */
#include <string.h>/* for strlen() */

#define MAX_LEN 1024 /* maximum receive string size */
#define MIN_PORT 1024 /* minimum port allowed */
#define MAX_PORT 65535 /* maximum port allowed */
#define IP_ADD_MEMBERSHIP 12
#define IP_DROP_MEMBERSHIP 13
#define TIME 3000

int main(int argc, char *argv[]) {
int sock; /* socket descriptor */
int flag_on = 1; /* socket option flag */
struct sockaddr_in mc_addr; /* socket address structure */
char recv_str[MAX_LEN + 1]; /* buffer to receive string */
int recv_len; /* length of string received */
// struct ip_mreq mc_req; /* multicast request structure */
struct ip_mreq {
struct in_addr imr_multiaddr; /* Group multicast address */
struct in_addr imr_interface; /* Local interface address */
}mc_req;
char* mc_addr_str; /* multicast IP address */
/*unsigned*/ short mc_port; /* multicast port */
struct sockaddr_in from_addr; /* packet source */
/*unsigned*/ int from_len; /* source addr length */
int counter = 0;
char bufferData;
WSADATA wsaData;
char send_str[MAX_LEN];
int send_len;

/* Load Winsock 2.0 DLL */
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup() failed");
exit(1);
}
/* validate number of arguments */
if (argc != 3) {
fprintf(stderr,
"Usage: %s <Multicast IP> <Multicast Port>\n",
argv[0]);
exit(1);

}

mc_addr_str = argv[1]; /* arg 1' multicast ip address */
mc_port = atoi(argv[2]); /* arg 2' multicast port number */

/* validate the port range */
if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
fprintf(stderr, "Invalid port number argument %d.\n",
mc_port);
fprintf(stderr, "Valid range is between %d and %d.\n",
MIN_PORT, MAX_PORT);
exit(1);

}

/* create socket to join multicast group on */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket() failed");
exit(1);

}

/* set reuse port to on to allow multiple binds per host */
if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
sizeof(flag_on))) < 0) {
perror("setsockopt() failed");
exit(1);

}

/* construct a multicast address structure */
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family = AF_INET;
mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
mc_addr.sin_port = htons(mc_port);

/* bind multicast address to socket */
if ((bind(sock, (struct sockaddr *) &mc_addr,
sizeof(mc_addr))) < 0) {
perror("bind() failed");
exit(1);

}

/* construct an IGMP join request structure */
mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

/* send an ADD MEMBERSHIP message via setsockopt */
if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void*)&mc_req, sizeof(mc_req))) < 0) {
perror("setsockopt() failed");
exit(1);

}

for (; ;) { /* loop forever */

/* clear the receive buffers & structs */
memset(recv_str, 0, sizeof(recv_str));
from_len = sizeof(from_addr);
memset(&from_addr, 0, from_len);

/* block waiting to receive a packet */
if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0,
(struct sockaddr*)&from_addr, &from_len)) < 0) {
perror("recvfrom() failed");
exit(1);

}
counter++;

if (rand() % 5 == 0) {
printf("\nNACK received for message with ID: %d\n", counter);

sendto(sock, "NACK", strlen("NACK"),
0, (struct sockaddr *) &from_addr,
sizeof(&from_addr));

printf("NACK message sent.\n");
Sleep(TIME);
if (TIME < 3000) {
printf("\nError while receiving message.NACK received for message with ID: %d\n", counter);
}
else {

printf("\nReceived %d bytes from %s ", recv_len,
inet_ntoa(from_addr.sin_addr));
printf("\nMessage ID: %d\nMessage: %s", counter, recv_str);
//memset(recv_str, 0, sizeof(recv_str));
}
}
else {
/* output received string */
printf("\nReceived %d bytes from %s ", recv_len,
inet_ntoa(from_addr.sin_addr));
printf("\nMessage ID: %d\nMessage: %s", counter, recv_str);
}

}

/* send a DROP MEMBERSHIP message via setsockopt */
if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(void*)&mc_req, sizeof(mc_req))) < 0) {
perror("setsockopt() failed");
exit(1);

}

close(sock);

}

最佳答案

问题在于您如何打印发件人从收件人那里得到的消息:

printf("\nMessage ID %d\n and message: %s\n",recv_str);

您的格式字符串需要 intchar *作为参数,但是您只传递了 char *。这意味着您有一个格式说明符不匹配,并且没有足够的参数用于格式说明符。结果是 undefined behavior,表现为显示错误消息。

Visual Studio应该向您发出警告。当我编译发送者代码时,得到以下输出:

sender.c(102): warning C4047: 'function': 'int *' differs in levels of indirection from 'unsigned int'
sender.c(102): warning C4024: 'recvfrom': different types for formal and actual parameter 6
sender.c(104): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'char *'
sender.c(104): warning C4473: 'printf' : not enough arguments passed for format string
sender.c(104): note: placeholders and their parameters expect 2 variadic arguments, but 1 were provided
sender.c(104): note: the missing variadic argument 2 is required by format string '%s'

来自编译器的警告不应 而不是被忽略。请注意,除了您询问的问题之外,您还有其他问题。

由于您还没有消息ID,因此请将其从格式字符串中删除。
printf("Message: %s\n",recv_str);

关于c - 如何从接收方向发送方发送NACK消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60496823/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com