2014-03-24 109 views
2

我想通過C網絡上的麥克風發送聲音作爲一種網絡嬰兒監視器。 我已經得到了服務器錄音的聲音,但對我來說生活無法讓它在客戶端播放。 我的代碼是一個可怕的爛攤子,我appologise,但是這是我迄今爲止發送聲音通過網絡Ubuntu和C

服務器

int main (int argc, const char * argv[]) { 
    printf("Server\n"); 

    int server_sock_fd;  //file descriptor for server socket 
    int client_sock_fd;  //file descriptor for client socket 
    socklen_t server_len, client_len; 

    struct sockaddr_in server_address; 
    struct sockaddr_in client_address; 

    //Remove any old sockets and re-create 
    unlink("server_socket"); 

    //Obtain the file descriptor for the socket 
    server_sock_fd = socket(AF_INET, SOCK_STREAM, 0); 

    //Name the socket and set properties 
    server_address.sin_family  = AF_INET;     //This is the Internet protocol 
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);  //Accept from any address (note the conversion function) 
    server_address.sin_port   = htons(9734);    //remember to use host-to-network-type conversion 
    server_len = sizeof(server_address); 
    bind(server_sock_fd, (struct sockaddr*)&server_address, server_len); //bind socket to address 

    //Create a connection queue and define max number of client connections 
    listen(server_sock_fd, 1); 

    //Main loop (ctrl-C will quit this application) 
    while (1) { 
     char ch; 
     printf("Server waiting\n"); 
     client_len = sizeof(client_address); 

     //Accept incoming client connection and create client socket 
     //Returns a file descriptor for each connection in the queue (BLOCKS IF QUEUE IS EMPTY) 
     client_sock_fd = accept(server_sock_fd, (struct sockaddr*)&client_address, &client_len); 

     //Now we can read and write data using the standard read and write functions 
     //Note that the socket is treated like a file with read/write permissions 
     //We read the socket as if it were a file - note it is the client 
     //read(client_sock_fd, &ch, 1); //Read a single byte 
     //ch++; 


    //Build the command string 
    char strCommand[64]; 
    sprintf(strCommand, "arecord -q -c 1 -t raw -f S16_LE -r %u -D plughw:0,0", SAMPLING_RATE); 
    printf("Issuing command %s\n", strCommand); 

    //Open arecord as a child process for read (stdout will be directed to the pipe) 
    inputStream = popen(strCommand,"r"); 
    if (inputStream == NULL) { 
     perror("Cannot open child process for record"); 
     exit(1); 
    } 
//We get back a C Stream - I prefer to use a UNIX file descriptor 
    fidIn = fileno(inputStream); 

    //Read a finite block of data into a buffer 
    char* pBuffer = buf; 
    int iSamplesRead; 
    iBytesRead = 0; 
    while (iBytesRead < BUFFER_SIZE_IN_BYTES) { 

     //Grab a block samples 
     iSamplesRead = read(fidIn, pBuffer, BUFFER_SIZE_IN_BYTES);   //4000 is max for one hit 

     //It is likely that the actual number of samples read is not as many as we wanted 
     if (iSamplesRead > 0) { 
      iBytesRead += iSamplesRead; 
      pBuffer += iSamplesRead; 
      printf("%d ", iSamplesRead); 
     } else { 
      break; 
     } 
    } 

    //All done, then close the child process 
    pclose(inputStream); 
     //We can also send a response 
     //write(client_sock_fd, &ch, 1); //Write a single byte 

     //Now close the client socket (we have finished) 
     close(client_sock_fd); 
    } 
    return 0; 
} 

客戶

int main (int argc, const char * argv[]) { 
    printf("Client\n"); 

    int socket_fd; //file descriptor 
    int len; 
    struct sockaddr_in address; 
    int result; 
    char ch = 'A'; 

    //Create client socket 
    socket_fd = socket(AF_INET, SOCK_STREAM, 0); 

    //Name the socket - align with the server 
    address.sin_family  = AF_INET; 
    address.sin_addr.s_addr = inet_addr("127.0.0.1"); 
    address.sin_port  = htons(9734); 
    len = sizeof(address); 

    //Connect client socket to server socket 
    result = connect(socket_fd, (struct sockaddr*)&address, len); 
    if (result == -1) { 
     printf("Error - cannot connect socket\n"); 
     exit(0); 
    } 

    //ALL DONE - now we can use the socket as if it were a file 
    //write(socket_fd, &ch, 1); 
    //read(socket_fd, &ch, 1); 
    //printf("Character back from server = %u\n", ch); 
//Open process pipe for read only (pipe stream is uni-direction on Linux) 
char strCommand[64]; 
char* pBuffer = buf;  
sprintf(strCommand, "aplay -q -c 1 -t raw -f S16_LE -r %u", SAMPLING_RATE); 
    printf("Issuing command %s\n", strCommand); 
    outputStream = popen(strCommand,"w"); 
    if (outputStream == NULL) { 
     perror("Cannot open child process"); 
     exit(1); 
    } 
    fidOut = fileno(outputStream); 

    //Play audio 
    iBytesWritten = 0; 
    pBuffer = buf; 
    int iSamplesWritten; 
    fflush(outputStream); 
    while (iBytesWritten < BUFFER_SIZE_IN_BYTES) { 
     iSamplesWritten = write(fidOut, pBuffer, BUFFER_SIZE_IN_BYTES); 
     if (iSamplesWritten > 0) { 
      iBytesWritten += iSamplesWritten; 
      pBuffer += iSamplesWritten; 
      printf("%d ", iSamplesWritten); 
     } else { 
      break; 
     } 
    } 
    //Close pipes and unblock child process 
    pclose(outputStream); 
    printf("Address of buffer: %p\nAddress of pointer: %p\nDelta = %d\n", buf, pBuffer, (pBuffer-buf)); 

    return (EXIT_SUCCESS); 
} 

任何幫助或提示將非常讚賞!

+0

我知道編碼自己的解決方案是樂趣。但是如果你只需要一些有用的東西,那麼看看http://jackaudio.org/它可以做你想要的東西。 – jbr

回答