본문 바로가기
BackEnd FrontEnd/C , C++

[C] curl 로 pcm 파일 전달하기

by forkballpitch 2019. 10. 23.
반응형

 

FILE *fp;

long lSize;

char *buffer;

 

fp = fopen ( "record.pcm" , "rb" );

if( !fp ) perror("record.pcm"),exit(1);

 

fseek( fp , 0L , SEEK_END);

lSize = ftell( fp );

rewind( fp );

 

/* allocate memory for entire content */

buffer = ( char *)calloc( 1, lSize+1 );

if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);

 

/* copy the file into the buffer */

if( 1!=fread( buffer , lSize, 1 , fp) )

fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);




CURL *curl;

CURLcode res;

 

struct curl_httppost *formpost = NULL;

struct curl_httppost *lastptr = NULL;

struct curl_slist *headerlist = NULL;

static const char buf[] = "Expect:";

 

curl_global_init(CURL_GLOBAL_ALL);

 

curl = curl_easy_init();

 

headerlist = curl_slist_append(headerlist, buf);

if (curl) {

 

struct curl_slist *headers = NULL;

headers = curl_slist_append(headers, "Content-Type: audio/wav");

 

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

 

curl_easy_setopt(curl, CURLOPT_POST,1);

curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8080");

curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,lSize);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buffer);

//curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

//curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

 

res = curl_easy_perform(curl);

/* Check for errors */

if (res != CURLE_OK)

fprintf(stderr, "curl_easy_perform() failed: %s\n",

curl_easy_strerror(res));



curl_easy_cleanup(curl);

 

//curl_formfree(formpost);

 

curl_slist_free_all(headerlist);

}

 

/* do your work here, buffer is a string contains the whole text */

 

fclose(fp);

free(buffer);

반응형