ACK真的在最简单的UDP套接字编程中丢失了吗?
我知道UDP和TCP不同,有点不可靠。在学习UDP的同时,我尝试实现一个停止和等待流控制协议。这是一种非常简单的通信:一台PC上运行的客户端和另一台PC上运行的服务器。我正在尝试将ACK真的在最简单的UDP套接字编程中丢失了吗?,c,sockets,C,Sockets,我知道UDP和TCP不同,有点不可靠。在学习UDP的同时,我尝试实现一个停止和等待流控制协议。这是一种非常简单的通信:一台PC上运行的客户端和另一台PC上运行的服务器。我正在尝试将.txt文件包从客户端传输到服务器 在第一次,我计划首先实施“无错误”版本,然后故意添加错误概率,并查看停止和等待机制如何工作 我刚刚完成了无错误版本的实现,并且惊奇地发现在我所谓的“无错误”版本中,实际上有ACK丢失案例 因此,即使在我手动添加错误概率之前,ACK已经丢失了(奇怪的是,它从未损坏) 由于这是一个非常简
.txt
文件包从客户端传输到服务器
在第一次,我计划首先实施“无错误”版本,然后故意添加错误概率,并查看停止和等待机制如何工作
我刚刚完成了无错误版本的实现,并且惊奇地发现在我所谓的“无错误”版本中,实际上有ACK丢失案例
因此,即使在我手动添加错误概率之前,ACK已经丢失了(奇怪的是,它从未损坏)
由于这是一个非常简单的通信,我希望没有ACK丢失或损坏。我想知道我是否在某个地方实施了错误的程序,或者应该是这样的
我的密码
cli.c
#include "headsock.h"
float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom); // communication function
void tv_sub(struct timeval *out, struct timeval *in); //calculate the time interval between out and in
int main(int argc, char **argv)
{
int sockfd;
float ti, rt;
long len;
struct sockaddr_in ser_addr;
char ** pptr;
struct hostent *sh;
struct in_addr **addrs;
FILE *fp;
socklen_t len_recvfrom;
if (argc != 2)
{
printf("parameters not match");
exit(0);
}
sh = gethostbyname(argv[1]); // get host's information
if (sh == NULL)
{
printf("error when gethostby name");
exit(0);
}
addrs = (struct in_addr **)sh->h_addr_list;
printf("canonical name: %s\n", sh->h_name); // print the remote host's information
for (pptr=sh->h_aliases; *pptr != NULL; pptr++)
printf("the aliases name is: %s\n", *pptr);
switch(sh->h_addrtype)
{
case AF_INET: // the address family that is used for the socket you're creating (in this case an Internet Protocol address)
printf("AF_INET\n");
break;
default:
printf("unknown addrtype\n");
break;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0); // create the socket
if (sockfd <0)
{
printf("error in socket");
exit(1);
}
ser_addr.sin_family = AF_INET; // address format is host and port number
ser_addr.sin_port = htons(MYUDP_PORT);
//copies count characters from the object pointed to by src to the object pointed to by dest
memcpy(&(ser_addr.sin_addr.s_addr), *addrs, sizeof(struct in_addr));
bzero(&(ser_addr.sin_zero), 8);
if((fp = fopen ("s.txt","r+t")) == NULL)
{
printf("File doesn't exit\n");
exit(0);
}
// perform the transmission and receiving
ti = str_cli(fp, sockfd, &len, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr_in), &len_recvfrom);
rt = ((len-1) / (float)ti); // caculate the average transmission rate
printf("Time(ms) : %.3f, Data sent(byte): %d\nData rate: %f (Kbytes/s)\n", ti, (int)len-1, rt);
close(sockfd);
fclose(fp);
exit(0);
}
// communication function
float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom)
{
char *buf;
long lsize, ci;
struct pack_so packet;
struct ack_so ack;
int n;
float time_inv = 0.0;
struct timeval sendt, recvt;
struct timeval sendTime, curTime;
ci = 0;
int prev_msg_acked = TRUE;
int next_packet_num = 0;
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", PACKLEN);
// allocate memory to contain the whole file.
buf = (char *) malloc(lsize+1);
if (buf == NULL)
exit (2);
// copy the file into the buffer.
// read lsize data elements, each 1 byte
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
{
// form the packet to transmit
if ((lsize-ci+1) <= PACKLEN) // final string
packet.len = lsize-ci+1;
else // send message of length PACKLEN
packet.len = PACKLEN;
packet.num = next_packet_num;
memcpy(packet.data, (buf+ci), packet.len);
/*************** SEND MESSAGE ***************/
gettimeofday(&sendTime, NULL);
if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
{
printf("Send error!\n"); // send the data
exit(1);
}
// update the sequence number
if (packet.num == 1)
next_packet_num = 0;
else
next_packet_num = 1;
ci += packet.len;
prev_msg_acked = FALSE;
}
/*************** RECEIVE ACK ***************/
// MSG_DONTWAIT flag, non-blocking
// receives nothing
if ((n = recvfrom(sockfd, &ack, sizeof(ack), MSG_DONTWAIT, addr, len_recvfrom)) == -1)
{
// monitors how long nothing is received
gettimeofday(&curTime, NULL);
// if timeout
if (curTime.tv_sec - sendTime.tv_sec > TIMEOUT)
{
// retransmit
printf("Timeout! Resend this.\n");
/*************** RESEND MESSAGE ***************/
gettimeofday(&sendTime, NULL);
if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
{
printf("Send error!\n"); // send the data
exit(1);
}
}
}
// An ACK is received
else
{
printf("ACK received. ");
// if what the server expects next is this one or server receives a different length
// if ACK is incorrect
if (ack.num != next_packet_num || ack.len != packet.len)
{
printf("Incorrect. Resend this. ");
printf("(%i %i expected, but %i %i received)\n", next_packet_num, packet.len, ack.num, ack.len);
/*************** RESEND MESSAGE ***************/
gettimeofday(&sendTime, NULL);
if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
{
printf("Send error!\n"); // send the data
exit(1);
}
}
// if ACK correct
else
{
printf("Correct. Send next.\n");
prev_msg_acked = TRUE;
}
}
}
gettimeofday(&recvt, NULL);
*len= ci; // get current time
tv_sub(&recvt, &sendt); // get the whole trans time
time_inv += (recvt.tv_sec)*1000.0 + (recvt.tv_usec)/1000.0;
return(time_inv);
}
//calculate the time interval between out and in
void tv_sub(struct timeval *out, struct timeval *in)
{
if ((out->tv_usec -= in->tv_usec) <0)
{
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
#include "headsock.h"
void str_ser(int sockfd); // transmitting and receiving function
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in my_addr;
//create socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
printf("error in socket\n");
exit(1);
}
my_addr.sin_family = AF_INET; // Address family; must be AF_INET
my_addr.sin_port = htons(MYUDP_PORT); // Internet Protocol (IP) port.
my_addr.sin_addr.s_addr = INADDR_ANY; // IP address in network byte order. INADDR_ANY is 0.0.0.0 meaning "all the addr"
// places nbyte null bytes in the string s
// this function will be used to set all the socket structures with null values
bzero(&(my_addr.sin_zero), 8);
// binds the socket to all available interfaces
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
printf("error in binding\n");
perror("socket error");
exit(1);
}
// receive and ACK
str_ser(sockfd);
close(sockfd);
exit(0);
}
// transmitting and receiving function
void str_ser(int sockfd)
{
FILE *fp;
char buf[BUFSIZE];
int end = 0, n = 0;
long lseek = 0;
struct ack_so ack;
struct pack_so packet;
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
printf("Start receiving...\n");
srand(time(NULL)); // seed for random number
uint8_t prev_pkt_seq = 1;
while(!end)
{
/*************** RECEIVE MESSAGE ***************/
// if error in receiving
if ((n = recvfrom(sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&addr, &len)) == -1)
{
printf("Error when receiving\n");
exit(1);
}
// if nothing received
else if (n == 0)
{
printf("Nothing received\n");
}
// if something received
else
{
// random number 0-99
// ACK lost
// send ACK
if ((rand() % 100) > NO_ACK_RATE)
{
// tell sender what to expect next
if (packet.num == 0)
ack.num = 1;
else
ack.num = 0;
ack.len = packet.len;
// random number 0-99
// ACK damaged
// damage ACK by toggling ACK
if ((rand() % 100) < WRONG_ACK_RATE)
{
if (ack.num == 0)
ack.num = 1;
else
ack.num = 0;
printf("ACK damaged! ");
}
/*************** SEND ACK ***************/
if ((n = sendto(sockfd, &ack, sizeof(ack), 0, (struct sockaddr *)&addr, len)) == -1)
{
printf("ACK send error!\n");
exit(1);
}
printf("%i %i as ACK sent\n", ack.num, ack.len);
}
// does not send ACK
else
printf("ACK lost!\n");
// only save packet if it is not a duplicate
if (packet.num != prev_pkt_seq)
{
// if the last bit of the received string is the EoF
if (packet.data[packet.len-1] == '\0')
{
end = 1;
packet.len--;
}
// copy this packet
memcpy((buf+lseek), packet.data, packet.len);
lseek += packet.len;
}
// record down previous packet sequence
prev_pkt_seq = packet.num;
}
}
if ((fp = fopen ("r.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);
}
#包括“headstock.h”
float str_cli(文件*fp,int sockfd,长*len,结构sockaddr*addr,int addrlen,socklen_t*len_recvfrom);//通信功能
无效tv_子(结构时间值*输出,结构时间值*输入)//计算输出和输入之间的时间间隔
int main(int argc,字符**argv)
{
int-sockfd;
浮动ti,rt;
龙伦;
ser_addr中的结构sockaddr_;
字符**pptr;
结构宿主*sh;
地址中的结构**地址;
文件*fp;
socklen_t len_recvfrom;
如果(argc!=2)
{
printf(“参数不匹配”);
出口(0);
}
sh=gethostbyname(argv[1]);//获取主机信息
如果(sh==NULL)
{
printf(“按名称获取主机时出错”);
出口(0);
}
地址=(地址**)sh->h\u地址列表中的结构;
printf(“规范名称:%s\n”,sh->h_name);//打印远程主机的信息
对于(pptr=sh->h_别名;*pptr!=NULL;pptr++)
printf(“别名名称为:%s\n”,*pptr);
开关(sh->h_添加类型)
{
case AF_INET://用于正在创建的套接字的地址族(在本例中为Internet协议地址)
printf(“AF_INET\n”);
打破
违约:
printf(“未知的addrtype\n”);
打破
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);//创建套接字
如果(sockfd tv_usec)tv_sec;
out->tv_usec+=1000000;
}
输出->电视秒-=输入->电视秒;
}
序列号c
#include "headsock.h"
float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom); // communication function
void tv_sub(struct timeval *out, struct timeval *in); //calculate the time interval between out and in
int main(int argc, char **argv)
{
int sockfd;
float ti, rt;
long len;
struct sockaddr_in ser_addr;
char ** pptr;
struct hostent *sh;
struct in_addr **addrs;
FILE *fp;
socklen_t len_recvfrom;
if (argc != 2)
{
printf("parameters not match");
exit(0);
}
sh = gethostbyname(argv[1]); // get host's information
if (sh == NULL)
{
printf("error when gethostby name");
exit(0);
}
addrs = (struct in_addr **)sh->h_addr_list;
printf("canonical name: %s\n", sh->h_name); // print the remote host's information
for (pptr=sh->h_aliases; *pptr != NULL; pptr++)
printf("the aliases name is: %s\n", *pptr);
switch(sh->h_addrtype)
{
case AF_INET: // the address family that is used for the socket you're creating (in this case an Internet Protocol address)
printf("AF_INET\n");
break;
default:
printf("unknown addrtype\n");
break;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0); // create the socket
if (sockfd <0)
{
printf("error in socket");
exit(1);
}
ser_addr.sin_family = AF_INET; // address format is host and port number
ser_addr.sin_port = htons(MYUDP_PORT);
//copies count characters from the object pointed to by src to the object pointed to by dest
memcpy(&(ser_addr.sin_addr.s_addr), *addrs, sizeof(struct in_addr));
bzero(&(ser_addr.sin_zero), 8);
if((fp = fopen ("s.txt","r+t")) == NULL)
{
printf("File doesn't exit\n");
exit(0);
}
// perform the transmission and receiving
ti = str_cli(fp, sockfd, &len, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr_in), &len_recvfrom);
rt = ((len-1) / (float)ti); // caculate the average transmission rate
printf("Time(ms) : %.3f, Data sent(byte): %d\nData rate: %f (Kbytes/s)\n", ti, (int)len-1, rt);
close(sockfd);
fclose(fp);
exit(0);
}
// communication function
float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom)
{
char *buf;
long lsize, ci;
struct pack_so packet;
struct ack_so ack;
int n;
float time_inv = 0.0;
struct timeval sendt, recvt;
struct timeval sendTime, curTime;
ci = 0;
int prev_msg_acked = TRUE;
int next_packet_num = 0;
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", PACKLEN);
// allocate memory to contain the whole file.
buf = (char *) malloc(lsize+1);
if (buf == NULL)
exit (2);
// copy the file into the buffer.
// read lsize data elements, each 1 byte
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
{
// form the packet to transmit
if ((lsize-ci+1) <= PACKLEN) // final string
packet.len = lsize-ci+1;
else // send message of length PACKLEN
packet.len = PACKLEN;
packet.num = next_packet_num;
memcpy(packet.data, (buf+ci), packet.len);
/*************** SEND MESSAGE ***************/
gettimeofday(&sendTime, NULL);
if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
{
printf("Send error!\n"); // send the data
exit(1);
}
// update the sequence number
if (packet.num == 1)
next_packet_num = 0;
else
next_packet_num = 1;
ci += packet.len;
prev_msg_acked = FALSE;
}
/*************** RECEIVE ACK ***************/
// MSG_DONTWAIT flag, non-blocking
// receives nothing
if ((n = recvfrom(sockfd, &ack, sizeof(ack), MSG_DONTWAIT, addr, len_recvfrom)) == -1)
{
// monitors how long nothing is received
gettimeofday(&curTime, NULL);
// if timeout
if (curTime.tv_sec - sendTime.tv_sec > TIMEOUT)
{
// retransmit
printf("Timeout! Resend this.\n");
/*************** RESEND MESSAGE ***************/
gettimeofday(&sendTime, NULL);
if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
{
printf("Send error!\n"); // send the data
exit(1);
}
}
}
// An ACK is received
else
{
printf("ACK received. ");
// if what the server expects next is this one or server receives a different length
// if ACK is incorrect
if (ack.num != next_packet_num || ack.len != packet.len)
{
printf("Incorrect. Resend this. ");
printf("(%i %i expected, but %i %i received)\n", next_packet_num, packet.len, ack.num, ack.len);
/*************** RESEND MESSAGE ***************/
gettimeofday(&sendTime, NULL);
if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
{
printf("Send error!\n"); // send the data
exit(1);
}
}
// if ACK correct
else
{
printf("Correct. Send next.\n");
prev_msg_acked = TRUE;
}
}
}
gettimeofday(&recvt, NULL);
*len= ci; // get current time
tv_sub(&recvt, &sendt); // get the whole trans time
time_inv += (recvt.tv_sec)*1000.0 + (recvt.tv_usec)/1000.0;
return(time_inv);
}
//calculate the time interval between out and in
void tv_sub(struct timeval *out, struct timeval *in)
{
if ((out->tv_usec -= in->tv_usec) <0)
{
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
#include "headsock.h"
void str_ser(int sockfd); // transmitting and receiving function
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in my_addr;
//create socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
printf("error in socket\n");
exit(1);
}
my_addr.sin_family = AF_INET; // Address family; must be AF_INET
my_addr.sin_port = htons(MYUDP_PORT); // Internet Protocol (IP) port.
my_addr.sin_addr.s_addr = INADDR_ANY; // IP address in network byte order. INADDR_ANY is 0.0.0.0 meaning "all the addr"
// places nbyte null bytes in the string s
// this function will be used to set all the socket structures with null values
bzero(&(my_addr.sin_zero), 8);
// binds the socket to all available interfaces
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
printf("error in binding\n");
perror("socket error");
exit(1);
}
// receive and ACK
str_ser(sockfd);
close(sockfd);
exit(0);
}
// transmitting and receiving function
void str_ser(int sockfd)
{
FILE *fp;
char buf[BUFSIZE];
int end = 0, n = 0;
long lseek = 0;
struct ack_so ack;
struct pack_so packet;
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
printf("Start receiving...\n");
srand(time(NULL)); // seed for random number
uint8_t prev_pkt_seq = 1;
while(!end)
{
/*************** RECEIVE MESSAGE ***************/
// if error in receiving
if ((n = recvfrom(sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&addr, &len)) == -1)
{
printf("Error when receiving\n");
exit(1);
}
// if nothing received
else if (n == 0)
{
printf("Nothing received\n");
}
// if something received
else
{
// random number 0-99
// ACK lost
// send ACK
if ((rand() % 100) > NO_ACK_RATE)
{
// tell sender what to expect next
if (packet.num == 0)
ack.num = 1;
else
ack.num = 0;
ack.len = packet.len;
// random number 0-99
// ACK damaged
// damage ACK by toggling ACK
if ((rand() % 100) < WRONG_ACK_RATE)
{
if (ack.num == 0)
ack.num = 1;
else
ack.num = 0;
printf("ACK damaged! ");
}
/*************** SEND ACK ***************/
if ((n = sendto(sockfd, &ack, sizeof(ack), 0, (struct sockaddr *)&addr, len)) == -1)
{
printf("ACK send error!\n");
exit(1);
}
printf("%i %i as ACK sent\n", ack.num, ack.len);
}
// does not send ACK
else
printf("ACK lost!\n");
// only save packet if it is not a duplicate
if (packet.num != prev_pkt_seq)
{
// if the last bit of the received string is the EoF
if (packet.data[packet.len-1] == '\0')
{
end = 1;
packet.len--;
}
// copy this packet
memcpy((buf+lseek), packet.data, packet.len);
lseek += packet.len;
}
// record down previous packet sequence
prev_pkt_seq = packet.num;
}
}
if ((fp = fopen ("r.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);
}
#包括“headstock.h”
void stru_ser(int sockfd);//发射和接收功能
int main(int argc,char*argv[])
{
int-sockfd;
我的地址中的结构sockaddr\u;
//创建套接字
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))=-1){
printf(“套接字中的错误\n”);
出口(1);
}
my_addr.sin_family=AF_INET;//地址族;必须是AF_INET
my_addr.sin_port=htons(MYUDP_port);//互联网协议(IP)端口。
my_addr.sin_addr.s_addr=INADDR_ANY;//网络字节顺序的IP地址。INADDR_ANY为0.0.0.0,表示“所有地址”
//在字符串s中放置nbyte null字节
//此函数将用于设置所有具有空值的套接字结构
bzero(&(我的地址sin_zero),8);
//将套接字绑定到所有可用接口
if(bind(sockfd,(struct sockaddr*)和my_addr,sizeof(struct sockaddr))=-1){
printf(“绑定错误”\n);
perror(“套接字错误”);
出口(1);
}
//接收和确认
stru_ser(sockfd);
关闭(sockfd);
出口(0);
}
//发射和接收功能
无效stru_ser(int sockfd)
{
文件*fp;
字符buf[BUFSIZE];
int end=0,n=0;
长lseek=0;
结构确认;
结构包;
地址中的结构sockaddr\u;
socklen\u t len=sizeof(结构sockaddr\u in);
printf(“开始接收…\n”);
srand(time(NULL));//随机数的种子
uint8上一个软件包序列=1;
当(!结束)
{
/***************接收消息***************/
//如果接收错误
if((n=recvfrom(sockfd,&packet,sizeof(packet),0,(struct sockaddr*)&addr,&len))=-1)
{
printf(“接收时出错\n”);
出口(1);
}
//如果没有收到
else如果(n==0)
{
printf(“未收到任何信息”);
}
//如果收到什么
其他的
{
//随机数0-99
//确认丢失
//发送确认
如果((rand()%100)>无确认率)
{
//告诉发件人下一步的预期
if(packet.num==0)
ack.num=1;
其他的
ack.num=0;
ack.len=packet.len;
//随机数0-99
//ACK损坏
//通过切换ACK来损坏ACK
如果((rand()%100)<错误确认率)
{
如果(ack.num==0)
ack.num=1;
其他的
ack.num=0;
printf(“确认损坏!”);
}
/***************发送确认***************/
if((n=sendto(sockfd,&ack,sizeof(ack),0,(struct sockaddr*)&addr,len))=-1)
{
printf(“确认发送错误!\n”);
出口(1);
}
printf(“%i%i作为已发送的ACK\n”,ACK.num,ACK.len);
}
//不发送确认
其他的
printf(“确认丢失!\n”);
//仅当数据包不是重复数据包时才保存
如果(packet.num!=prev_pkt_seq)
{
//如果接收字符串的最后一位是EoF
if(packet.data[packet.len-1]=='\0')
{