Example #1
0
int client::download()
{
    int res = 0;

    char file_name[BUFFSIZE] = {0};
    if(m_cmd.size() < 2)
    {
        do
        {
            cout << "file name: ";
            cout.flush();

            cin.getline(file_name, BUFFSIZE);

        }while(file_name[0] == '\0');
    }
    else
    {
        strncpy(file_name, m_cmd[1], BUFFSIZE);
    }

    m_buff.clear();

    // Add the pack type.
    m_buff.push_back(PT_DWREQ);

    // Add the filename.
    add_buff(file_name, strlen(file_name));

    // Add checkcode.
    m_analyser.load_data(m_buff);
    m_buff.push_back(m_analyser.check());

    if(-1 == send_data())
    {
        ;// Send error.
    }

    char pack_type = acceptable();
    if( is_pack_type(PT_OK, pack_type) )
    {
        if(-1 == recv_file_info() )
        {
            return -1;// Recv error.
        }

        res = recv_file();
    }
    else if( is_pack_type(PT_FNOEXIST, pack_type) )
    {
        cout << "file not exist" << endl;;
    }

    return res;
}
Example #2
0
void* client_connect(void* info){
    thread_info* new_info = (thread_info* )info;
    int sockfd = new_info -> sockfd;
    std::string addr = new_info -> address;
    int port = new_info -> port;

    std::cout << sockfd << " " << addr << " " << port << '\n';
    /*int read_size;
    char client_message[2000];
    char welcome_message[200];
    strcpy(welcome_message, "Welcome to simple P2P server !\n");

    write(sockfd, welcome_message, strlen(welcome_message));
	//test block

	login::Login login;
	login.set_id("testID");
	login.set_passwd("testPasswd");
	int pkg_size = login.ByteSize() + 4;
	char* pkt = new char[pkg_size];
	google::protobuf::io::ArrayOutputStream aos(pkt, pkg_size);
	google::protobuf::io::CodedOutputStream* coded_output = new google::protobuf::io::CodedOutputStream(&aos);
	coded_output -> WriteVarint32(login.ByteSize());
	login.SerializeToCodedStream(coded_output);

	write(sockfd, pkt, pkg_size);*/
	//test block
    update();
    login::Login login;
    while( true ){
        int count;
        char buffer[4];
        count = recv(sockfd, buffer, 4, MSG_PEEK);
        if(count == -1)
            perror("Recv with error");
        else{
            ACTION request;
            request = readAction(sockfd, readHdr(buffer));
            if ( request == LOGIN ) {
                login = login_check(sockfd, addr, port);
            } else if ( request == REGIST ) {
                regist_check(sockfd);
            } else if ( request == DELETEACCOUNT ) {
                delete_account(sockfd);
            } else if ( request == SEARCHINFO ) {
                search_info(sockfd);
            } else if ( request == DOWNLOAD ) {
                download_p2p(sockfd);
            } else if ( request == CHAT ) {
                chat(sockfd);
            } else if (request == LOGOUT ) {
                logout(sockfd, file_sets);
                break;
            } else if (request == RECVFILEINFO ) {
                recv_file_info(sockfd, file_sets, login);
            } else if (request == ONLINEINFO){
                send_online_info(sockfd);
            } else if (request == PORTREQUEST) {
                //send_port(sockfd);
            } else {
                printf("%sBugssssssssssss%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
            }
        }
    }


    /*while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ){
		client_message[read_size] = '\0';
        write(sock , client_message , strlen(client_message));
		memset(client_message, 0, 2000);
    }

    if(read_size == 0) {
        puts("Client disconnected");
        fflush(stdout);
    }else if(read_size == -1){
        perror("recv failed");
    }*/
    //printf("Connection left\n");
    return 0;
}