以下是我的代码的链接,我试图在s.cpp aka服务器中使用选择性重复进行传输,但是我遇到了以下错误。我尝试了很多,但我无法摆脱错误。如果有任何人可以帮助我并让代码运行,我真的非常感谢。关于以下压缩文件夹中的代码和详细信息,一切都存在。请帮助。
对于s.cpp错误:
In file included from unp.h:7,
from s.cpp:1:
config.h:2:22: warning: extra tokens at end of #undef directive
s.cpp:31: warning: alignment must be a small power of two, not 1000
s.cpp:52: error: expected unqualified-id before ‘while’
s.cpp:60: error: expected unqualified-id before ‘if’
s.cpp:65: error: expected unqualified-id before ‘while’
s.cpp:69: error: expected unqualified-id before ‘return’
s.cpp: In function ‘int main()’:
s.cpp:140: error: ‘udp_checksum’ was not declared in this scope
s.cpp:161: error: ‘l’ was not declared in this scope
S.cpp完整代码:
#include "unp.h"
#include <iostream>
#include <string.h>
#include <fstream>
#define MYPORT 10101
#define BUFFER_SIZE 1000
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
socklen_t len;
int sockfd;
int n;
using namespace std;
ofstream out;
ofstream logout;
time_t mytime;
void handler(int a)
{
logout<<"Exiting..."<<endl;
sleep(1);
close(sockfd);
out.close();
logout.close();
exit(0);
}
#pragma pack(1000) // this helps to pack the struct to 5-bytes
struct pkt
{
int seq_no;
int ack_no;
char segment[BUFFER_SIZE];
uint16_t checksum;
pkt()
{
ack_no=0;
}
};
#pragma pack(0) // turn packing off
struct pkt reciv;
struct pkt data[8];// = new pkt;
struct pkt ack; // = new pkt;
struct pkt ackwindow[8];
while (len > 1)
{
sum += *buf++;
if (sum & 0x80000000)
sum = (sum & 0xFFFF) + (sum >> 16);
len -= 2;
}
if ( len & 1 )
// Add the padding if the packet lenght is odd //
sum += *((uint8_t *)buf);
// Add the carries //
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
// Return the one's complement of sum //
return ( (uint16_t)(~sum) );
void copy(pkt&a,const pkt b)
{
a.seq_no=b.seq_no;
a.ack_no=b.ack_no;
a.checksum=b.checksum;
memcpy(a.segment,b.segment,BUFFER_SIZE);
}
//--------------------------------------------- Main -----------------------------------------//
int rdt_recv()
{
return 1;
}
int N=8;
//--------------------------------------------- Main -----------------------------------------//
int main()
{
logout.open("reciver_logfile.txt", ios::out);
signal(SIGINT,handler);
signal(SIGTSTP,handler);
signal(SIGQUIT,handler);
sockfd = socket(AF_INET, SOCK_DGRAM, 0 );
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MYPORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero) );
//------------------------------------------------------//
bind(sockfd, (struct sockaddr *)&server_addr, sizeof server_addr);
//cout<<endl;
perror("bind");
out.open("file2.txt", ios::binary | ios::out);
//------------------------------------------------------//
struct timeval tv;
tv.tv_sec = 5; /* 4 Secs Timeout */ // See Reference
tv.tv_usec = 0; // Not init'ing this can cause strange errors
int recv_status,send_status;
int counter = 0;
size_t data_size=sizeof(data[0]);
len = sizeof(sockaddr);
int seq = 0; // expected sequence number
int iteration = 0;
int i=0;
int expected_seq_no=0;
int check=0;
while(true)
{
srand(time(0));
memset(reciv.segment, 0, sizeof(reciv.segment));
recv_status = recvfrom(sockfd, & reciv, data_size, 0, (struct sockaddr *) &client_addr, &len);
reciv.ack_no=0;
if(strcmp(reciv.segment,"kill")==0)
kill(getpid(),SIGINT);
cout<<udp_checksum(reciv.segment,BUFFER_SIZE)<<" : "<<reciv.checksum<<endl;
if(udp_checksum(reciv.segment,BUFFER_SIZE)==reciv.checksum && expected_seq_no==reciv.seq_no)
{
copy(data[expected_seq_no%N],reciv);
data[expected_seq_no%N].ack_no=1;
logout<< ctime(&mytime)<<" packet number: "
<<data[expected_seq_no%N].seq_no<<" recived."<<endl;
cout<<"\n\naccepted seq no: "<<reciv.seq_no<<"\n"<<endl;
data[expected_seq_no%N].segment[recv_status-(sizeof(data[expected_seq_no%N])-BUFFER_SIZE)]='\0';
for(int i=0;i<N;i++)
{
cout<<data[i].seq_no<<": "<<data[i].ack_no<<endl;
}
int pkt_written=0;
if(data[0].ack_no==1&&check==7)
int l = 0;
while (data[l].ack_no == 1)
{
if (data[l].ack_no != 0)
{
seq = data[l].seq_no;
data[l].ack_no = 0;
cout << "-------------------writing packet number " << data[l].seq_no << endl;
for (int k = 0; k < (recv_status - (sizeof(data[expected_seq_no%N]) - BUFFER_SIZE)); k++)
{
out << data[l].segment[k];
}
pkt_written = pkt_written+1;
}
check = 0;
l = l + 1;
}
int k = 0;
while (k < pkt_written)
{
int i = 0;
while (i < N)
{
copy(data[i], data[i + 1]);
i = i + 1;
}
data[N - 1].ack_no = 0;
k = k + 1;
}
cout<<"\n------------------------------------------------------"<<expected_seq_no<<endl;
int i = 0;
while (i < N)
{
cout << data[i].seq_no << ": " << data[i].ack_no << endl;
i = i + 1;
}
reciv.ack_no=1;
cout<<reciv.seq_no<<" got it!"<<endl;
expected_seq_no++;
reciv.seq_no=expected_seq_no;
check++;
}
else
{
reciv.ack_no=1;
logout<< ctime(&mytime)<<" packet corrupt: "
<<reciv.seq_no<<endl;
cout<<reciv.seq_no<<" send again"<<endl;
reciv.seq_no=expected_seq_no-1;
}
if(rand()%100<5)
sleep(4);
memset(reciv.segment, 0, sizeof(reciv.segment));
send_status = sendto(sockfd, &reciv, sizeof(reciv), 0, (struct sockaddr *) &client_addr, len);
}
}
答案 0 :(得分:0)
从第52行到第69行删除此代码块
while (len > 1)
{
sum += *buf++;
if (sum & 0x80000000)
sum = (sum & 0xFFFF) + (sum >> 16);
len -= 2;
}
if ( len & 1 )
// Add the padding if the packet lenght is odd //
sum += *((uint8_t *)buf);
// Add the carries //
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
// Return the one's complement of sum //
return ( (uint16_t)(~sum) );
它们只是无效