我知道与 TCP 不同,UDP 有点不可靠。在学习 UDP 时,我尝试实现一个停止等待流控制协议。这是一种极其简单的通信:在一台 PC 上运行的客户端和在另一台 PC 上运行的服务器。我正在尝试将我的.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);
}
头文件.h
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#define MYTCP_PORT 4950
#define MYUDP_PORT 5350
#define DATALEN 65
#define BUFSIZE 60000
#define PACKLEN 1000
#define NO_ACK_RATE 0 // 10%
#define WRONG_ACK_RATE 0 // 10%
#define TRUE 1
#define FALSE 0
#define TIMEOUT 1
// data packet structure
struct pack_so
{
uint32_t num; // the sequence number
uint32_t len; // the packet length
char data[PACKLEN]; // the packet data
};
struct ack_so
{
uint8_t num; // the sequence number
uint32_t len; // the packet length
};