如何使用sendto()和recvfrom()函数在socket上传递struct对象?

时间:2013-11-26 00:56:37

标签: sockets networking tcp udp client-server

我试图在客户端/服务器模型中实现代码但是分段错误错误即将到来..

服务器:

/ *这是我试图传递的结构* /

struct pkt{     
        char data[1000];        
        int seqNo;
        int checksum;
        void make_pkt(int seq,char dat[1000],int check)
        {
            seqNo=seq;
            strcpy(data,dat);
            checksum=check;

        }
    };

主要部分是

main()   /*  main */
{
port=client_addr.sin_port;
        cout<<"port : "<<port;
        pkt *rcv=new pkt;
        do{
            cout<<"a"<<endl;
            cout<<"port : "<<port;
            pkt *newpkt=new pkt;
            char *buffer=new char [1000];
            strcpy(buffer,"Hamza");
            newpkt->make_pkt(1,buffer,2);
            cout<<"aa"<<endl;
            client_addr.sin_port=port;
            cout<<"port 2 : "<<client_addr.sin_port<<endl;  
            sendto(sockfd,&newpkt,sizeof(struct pkt),0,(struct sockaddr *) &client_addr,sizeof(client_addr));

            cout<<"aaa"<<endl;
            recvfrom(sockfd,&rcv,sizeof(struct pkt),0,(struct sockaddr *)&client_addr,&leng);
            cout<<"bb :"<<rcv->seqNo<<endl;
        }while(rcv->seqNo!=1);
}

客户端:

struct pkt{ 
    char data[1000];
    int seqNo;
    int checksum;
    pkt()
    {
        strcpy(data," ");
        seqNo=0;
        timer=0;
        checksum=0;
    }
};
main()
{
pkt *newpkt=new pkt;
        do{
        cout<<"aa"<<endl;
            pkt *rcvpkt=new pkt;
            socklen_t lengt=sizeof(my_addr);
            recvfrom(sockfd,&rcvpkt,sizeof(struct pkt),0,(struct sockaddr *)&my_addr,&lengt);
            cout<<"aaa"<<endl;
            cout<<"aaa"<<rcvpkt->data<<endl;
            newpkt->seqNo=1;
            sendto(sockfd,&newpkt,sizeof(struct pkt),0,(struct sockaddr *) &my_addr,sizeof(my_addr));

        }while(newpkt->seqNo!=1);
}

请告诉我这段代码中的问题..?

1 个答案:

答案 0 :(得分:1)

您没有正确地将结构实例传递给sendto()recvfrom()。你的pkt变量是指针,所以你需要在传递它们时去除&运算符。你也有内存泄漏。试试这个:

main()
{
    port = ntohs(client_addr.sin_port);
    cout << "port : " << port;
    pkt *rcv = new pkt;
    do{
        cout << "a" << endl;
        cout << "port : " << port;
        pkt *newpkt = new pkt;
        char buffer[1000];
        strcpy(buffer,"Hamza");
        newpkt->make_pkt(1,buffer,2);
        cout << "aa" << endl;
        client_addr.sin_port = htons(port);
        cout << "port 2 : " << ntohs(client_addr.sin_port) << endl;  
        sendto(sockfd, newpkt, sizeof(struct pkt), 0, (struct sockaddr *) &client_addr, sizeof(client_addr));
        delete newpkt;

        cout << "aaa" << endl;
        recvfrom(sockfd, rcv, sizeof(struct pkt), 0, (struct sockaddr *)&client_addr, &leng);
        cout << "bb :" << rcv->seqNo << endl;
    }
    while(rcv->seqNo != 1);
    delete rcv;
}

main()
{
    pkt *newpkt = new pkt;
    do{
        cout << "aa" << endl;
        pkt *rcvpkt = new pkt;
        socklen_t lengt = sizeof(my_addr);
        recvfrom(sockfd, rcvpkt, sizeof(struct pkt), 0, (struct sockaddr *)&my_addr, &lengt);
        cout << "aaa" << endl;
        cout << "aaa" << rcvpkt->data << endl;
        newpkt->seqNo = 1;
        sendto(sockfd, newpkt, sizeof(struct pkt), 0, (struct sockaddr *) &my_addr, sizeof(my_addr));
        delete rcvpkt;

    }
    while(newpkt->seqNo != 1);
    delete newpkt;
}