Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C++ win32] winsock e utilizzo cpu

    Salve,

    ho fatto un piccolo programmino client per registrare lo streaming mp3 da una web Radio. Tuttavia il seguente codice mi impiega la cpu fino al 90% (windows vista). Ora, potrei aver sbagliato a scrivere la parte nel loop while(1) dove effettivamente leggo dal buffer della winsock, oppure è la winsock stessa molto "pesante" nel fare questo tipo di lavoro...

    da notare che ho dovuto mettere if(bsize > 0) perchè molto spesso ottenevo 0 o -1

    la web Radio è all'indirizzo: 38.96.148.42:7060

    Cmq per il resto la registrazione avviene benissimo, soltanto che gli altri programmi (players) per windows non usano loro stessi la winsock?

    Codice PHP:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "socket.h"
    #include "httpUtil.h"
    using namespace std;
    using namespace httpUtil// chomp(),split(),struct respline,struct reqline,getDateTime()

    const string CRLF      "\r\n";
    const 
    char szMp3File TEXT("C:\\Users\\giangaetano\\Desktop\\mmio\\radio.MP3");

    int main()
    {
        
    // Connect to the Server
        //
        
    SocketClient s("38.96.148.42"7060);

        
    // Send http Request
        //
        
    s.SendBytes("GET / HTTP/1.1" CRLF);
        
    s.SendBytes("Host: 38.96.148.42:7060" CRLF);
        
    s.SendBytes("User-Agent: WinampMPEG/5.53" CRLF);
        
    s.SendBytes("Connection: Keep-Alive" CRLF);
        
    s.SendBytes("Icy-MetaData: 0" CRLF);
        
    s.SendBytes(CRLF);

        
    // Response
        // Get first line: GET URI HTTPVER
        //
        
    string line s.ReceiveLine();
        
    chomp(line);
        
    respline resp(line);

        
    // Get the whole http header (key: value)
        //
        
    map<string,stringheaderHash;
        
    map<string,string>::const_iterator it;

        while(
    1)
        {
            
    line s.ReceiveLine();
            
    chomp(line);

            if(
    line.empty())
                break;

            
    headerHash.insert(split(line":"));
        }

        
    // Show Response header
        //
        
    cout << endl;
        
    cout << "statusCode: " << resp.statusCode << endl;
        
    cout << "reasonPhrase: " << resp.reasonPhrase << endl;
        
    cout << endl;

        for(
    it=headerHash.begin(); it!=headerHash.end(); ++it)
        {
            
    cout << it->first << ":" << it->second << endl;
        }

        
    // Get Response body and save it to file
        //
        
    char buffer[32768] = {0};
        
        
    ofstream hfile;
        
    hfile.open(szMp3Fileios::out|ios::binary);
        if(
    hfile.bad())
        {
            
    cout << "file error!" << endl;
            return -
    1;
        }

        
    Sleep(100);
        
    int count 0;

        while(
    1)
        {
            
    int bsize s.ReceiveBytes(buffer32768);
            if(
    bsize 0)
            {
                
    hfile.write(bufferbsize);
                
    count++;
            }
            if(
    count>500)
                break;
        }

        
    hfile.close();

        
    // Disconnect from the Server
        //
        
    s.Close();

        
    system("pause");
        return 
    EXIT_SUCCESS;

    Il programma usa due librerie scaricabili da quì: https://sourceforge.net/projects/httpsocketutil/files/

    grazie
    Alla batteria dai retta ballA

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Inserisci nel loop una

    Sleep(50);
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    incredibile...ora uso cpu: 1%

    Credevo che mettendo una Sleep() mi sarei "perso" qualcosa dal buffer...anzi...invece gli do pure il tempo di accumulare!
    Alla batteria dai retta ballA

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.