Realization of Peer-to-peer technology in LAN

Source: Internet
Author: User
Tags goto

Peer-to-peer technology to achieve a lot of ways, is mainly introduced by using the socket TCP method to build Peer-to-peer environment.

At the beginning of the main reference NBD protocol part of the server program to implement the BT server program.

Each client side has two processes to achieve peer-to-peer functionality, one is the client's server program and the other is client-side programs. And the BT server side has only one process, responsible for resource allocation and load balancing work.

The following are considerations for the server-side program. Debugging is always found to have "address already in use" information appears, add the following code to resolve this issue.

/* Lose the pesky "address already with" error message * *
if (setsockopt (sockfd,sol_socket,so_reuseaddr,&yes,sizeof (int)) = = 1) {
Perror ("setsockopt so_reuseaddr");
}
if (setsockopt (sockfd,sol_socket,so_keepalive,&yes,sizeof (int)) = = 1) {
Perror ("setsockopt so_keepalive");
}

When each client initiates a request, it first asks the server side, the entire downloaded Mirror's seed distribution state, and then the seed distribution status of each slice. Server-side servers fork a process for each client's request. Wait for client download complete, Report server side, this piece I have downloaded, and updated BT resource data. At the same time can design the client to the BT server can be shut down/boot to do kinds of mechanisms.

The seed resource data structure is as follows:

struct pieces_t{
        struct list_head head;
        struct pieceinfo_t pieceinfo;
        u32 len;                        //how Many clients connect the Piece
} __attribute__ ((packed));

struct clientinfo_t {
        struct list_head list;
        Char ipaddr[ipaddrlen];                 &NBSP
        Char mac[macaddrlen];                   //mac address
};
struct pieceinfo_t
{
        u32 fid;                         &NBSP
        u64 offset;                     

U32 size;
int hash;

};

The Protocol unit program routines are as follows: (client report Download complete)

int download_finish (int sock,u32 FID)
{
struct Smc_request request;
struct smc_reply reply;
int error = 0;

Request.magic = htonl (smc_request_magic);
Request.type = htonl (req_download_finish);
Request.fid = htonl (FID); 0 means all pieces are downloaded

if (Tcp_xmit (Tcp_write, sock, (char *) &request, sizeof (request)) <= 0) {
fprintf (stderr, "request_update_finish:failed 1/n");
Error=-1;
Goto Err_out;
}

        if (Tcp_xmit (Tcp_read, sock, (char *) &reply, sizeof (reply)) <= 0) {
                fprintf (stderr, "request_update_finish:  failed 2/n");
                error=-2;
                Goto Err_out;
       }

if (reply.error) {
error=-3;
fprintf (stderr, "request_update_finish:failed 3/n");
Goto Err_out;
}
DEBUG2 ("DOWNLOAD fid (%d) finished/n", FID);
Err_out:

return error;
}

Also designed to support the blacklist function. That is, when many clients (have been downloaded) shutdown, the client is downloading to avoid some unnecessary connection loss, provide if the return can not connect, directly to the IP corresponding client into the blacklist list, the next download, If the machine is in the blacklist, you can skip it. At the same time regularly update the blacklist inside the machine. If the inside of the machine has been powered on. Then you can release it from the blacklist and continue to do it for other clients.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.