【问题标题】:Server side error Selective repeat in s.cpp服务器端错误 s.cpp 中的选择性重复
【发布时间】:2014-06-29 15:25:08
【问题描述】:

以下是我尝试在 s.cpp aka 服务器中使用选择性重复进行传输的代码链接,但出现以下错误。我尝试了很多,但我无法摆脱错误。如果有人可以帮助我并让代码运行,我将非常感激。以下压缩文件夹中包含有关代码和详细信息的所有内容。请帮忙。

http://ge.tt/#!/19q29ug1/v/0

对于 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);
    }
}

【问题讨论】:

  • @JoachimPileborg 请仅查看上述问题。

标签: c++ network-programming g++ redhat network-protocols


【解决方案1】:

从第 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)  );

它们只是无效的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2010-09-16
    • 2013-05-27
    • 2013-03-18
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多