gpt4 book ai didi

客户端发送 500 字节,但服务器接收 244 字节 - 套接字编程?

转载 作者:行者123 更新时间:2023-12-03 11:50:38 26 4
gpt4 key购买 nike

我正在尝试为UDP实现Stop-and-Wait ARQ。根据停止等待约定,我在 01 之间切换 ACK

正确的 ACK 定义为正确的序列号(01)AND消息长度。

以下片段是我的代码的相关部分。

客户端

// transmission function
void str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen)
{
char *buf;
long lsize, ci;
char sends[DATALEN];
struct ack_so ack;
int n, slen;
float time_inv = 0.0;
struct timeval sendt, recvt;
ci = 0;

int prev_msg_acked = TRUE;
int ack_number = 1;

fseek(fp, 0, SEEK_END);
lsize = ftell (fp);
rewind(fp);
printf("The file length is %d bytes\n", (int)lsize);
printf("the packet length is %d bytes\n", DATALEN);

// allocate memory to contain the whole file.
buf = (char *) malloc(lsize);
if (buf == NULL)
exit (2);

// copy the file into the buffer.
fread(buf, 1, lsize, fp);

// the whole file is loaded in the buffer
buf[lsize] ='\0'; // append the end byte
gettimeofday(&sendt, NULL); // get the current time
while(ci <= lsize)
{
if (prev_msg_acked) // only transmits when previous message has been acknowledged
{
if ((lsize+1-ci) <= DATALEN) // final string
slen = lsize+1-ci;
else // send message of length DATALEN
slen = DATALEN;
memcpy(sends, (buf+ci), slen);

/*************** SEND MESSAGE ***************/
if((n = sendto(sockfd, &sends, strlen(sends), 0, addr, addrlen)) == -1) {
printf("send error!\n"); // send the data
exit(1);
}

// update the expected ACK number
if (ack_number == 1)
ack_number = 0;
else
ack_number = 1;

ci += slen;
}

/*************** RECEIVE ACK ***************/
if ((n = recvfrom(sockfd, &ack, 2, 0, addr, &addrlen)) == -1)
{
printf("error when receiving\n");
exit(1);
}

if (ack.num != ack_number || ack.len != slen) // ACK wrong
{
printf("%i %i expected\n", ack_number, strlen(sends));
printf("%i %i received\n", ack.num, ack.len);
printf("ACK check fails, retransmission...\n");
prev_msg_acked = FALSE;
}
else
prev_msg_acked = TRUE;
}
}

服务器端

// transmitting and receiving function
void str_ser(int sockfd, int *ack_number)
{
FILE *fp;
char buf[BUFSIZE];
char recvs[DATALEN];
int end = 0, n = 0;
long lseek = 0;
struct ack_so ack;

struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);

printf("receiving data!\n");

while(!end)
{
// receive the packet
if ((n = recvfrom(sockfd, &recvs, DATALEN, 0, (struct sockaddr *)&addr, &len)) == -1)
{
printf("error when receiving\n");
exit(1);
}

// toggle the ack_number
if (*ack_number == 1)
*ack_number = 0;
else
*ack_number = 1;

// if the last bit of the received string is the EoF
if (recvs[n-1] == '\0')
{
end = 1;
n--;
}

memcpy((buf+lseek), recvs, n);
lseek += n;

// up to here, successfully received a packet
// send ACK back
ack.num = *ack_number;
ack.len = strlen(recvs);
printf("%i %i as ACK sent\n", ack.num, ack.len);
if ((n = sendto(sockfd, &ack, 2, 0, (struct sockaddr *)&addr, len)) == -1)
{
printf("ACK send error!\n");
exit(1);
}
}

if ((fp = fopen ("myUDPreceive.txt", "wt")) == NULL)
{
printf("File doesn't exit\n");
exit(0);
}

fwrite (buf, 1, lseek, fp); //write data into file
fclose(fp);
printf("A file has been successfully received!\nThe total data received is %d bytes\n", (int)lseek);
}

通过此实现,我最终得到了以下结果:

客户端结果

$ ./cli localhost
The file length is 59792 bytes
the packet length is 500 bytes
0 500 expected
0 244 received
ACK check fails, retransmission...

服务器端结果

$ ./ser
receiving data!
0 244 as ACK sent

可以看出,客户端发送了一条长度为 500 的消息,因此期望 ACK0 500。但是,服务器收到长度为 244 的消息,并发回 ACK 0 244。由于它们不匹配,当前的实现就停在那里。

为什么会出现这种长度差异?

最佳答案

在你的客户端,你正在做

sendto(sockfd, &sends, strlen(sends), 0, addr, addrlen)

你想要的是

sendto(sockfd, &sends, slen, 0, addr, addrlen)

strlen 存在问题有两个原因:(1) 您不会以空终止缓冲区,而且更重要的是,(2) 您正在发送的二进制数据可能有一个空字节作为其 245 字节。

经验法则:永远不要对二进制数据使用字符串函数。

关于客户端发送 500 字节,但服务器接收 244 字节 - 套接字编程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409957/

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