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,string> headerHash;
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(szMp3File, ios::out|ios::binary);
if(hfile.bad())
{
cout << "file error!" << endl;
return -1;
}
Sleep(100);
int count = 0;
while(1)
{
int bsize = s.ReceiveBytes(buffer, 32768);
if(bsize > 0)
{
hfile.write(buffer, bsize);
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