468,512 Members | 1,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

sending voice file using socket programming

Hi i want to record an audio file and send it using UDP . I have server/client program and i'm able to send text file and recive that into another file .But how do i send audio file ..can anyone help me out here ??????

I'm using Unix stations and C .
Feb 27 '08 #1
9 9997
Hi i want to record an audio file and send it using UDP . I have server/client program and i'm able to send text file and recive that into another file .But how do i send audio file ..can anyone help me out here ??????

I'm using Unix stations and C .
Hi,

have you tried sending the audio file as a text? That should work, or I don't see your problem.
Feb 28 '08 #2
sicarie
4,677 Expert Mod 4TB
Hi i want to record an audio file and send it using UDP . I have server/client program and i'm able to send text file and recive that into another file .But how do i send audio file ..can anyone help me out here ??????

I'm using Unix stations and C .
Are you sure you want to use UDP? That sounds more like you are trying to send streaming audio, not transferring a file from one computer to another...

If you send the file via UDP, you will lose data, and you will not be able to resend that data.
Feb 28 '08 #3
here is the code i'm trying to integrate to be able to send/transfer audio file. I just want to save an audio file and want to get to other computer thats all..Plz if someone can do some changes in the code that wud be awesome coz i'm not too aware of network programming .this is probably my first attempt !!!
rt now i'm broadcasting the file :
Broadcasting Code :
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11.  
  12. #define SERVERPORT 4950 // the port users will be connecting to
  13.  
  14. int main(int argc,char *argv[])
  15. {
  16. int sockfd;
  17. struct sockaddr_in their_addr; // connector's address information
  18. struct hostent *he;
  19. int numbytes;
  20. int broadcast = 1;
  21. FILE *fp1;
  22. int infile[50000]; //filesize
  23. int *mypointer
  24. char c;
  25. int i = 0;
  26.  
  27. mypointer = infile;
  28.  
  29. if (argc != 3) {
  30. fprintf(stderr,"usage: broadcaster hostname message\n");
  31. exit(1);
  32. }
  33.  
  34. if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
  35. herror("gethostbyname");
  36. exit(1);
  37. }
  38.  
  39. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  40. perror("socket");
  41. exit(1);
  42. }
  43.  
  44.  
  45. if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
  46. sizeof broadcast) == -1) {
  47. perror("setsockopt (SO_BROADCAST)");
  48. exit(1);
  49. }
  50. fp1 = fopen(argv[2],"r");
  51. while(( c = getc(fp1)) != EOF)
  52. {
  53. infile[i] = c;
  54. i = i+1;
  55. };
  56. their_addr.sin_family = AF_INET; // host byte order
  57. their_addr.sin_port = htons(SERVERPORT); // short, network byte order
  58. their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  59. memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
  60.  
  61. if ((numbytes=sendto(sockfd, mypointer, i, 0,
  62. (struct sockaddr *)&their_addr, sizeof their_addr)) == -1) {
  63. perror("sendto");
  64. exit(1);
  65. }
  66.  
  67. printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
  68.  
  69. close(sockfd);
  70.  
  71. return 0;
  72. }
Listening file :
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10.  
  11. #define MYPORT 4950 // the port users will be connecting to
  12.  
  13. #define MAXBUFLEN 50000
  14.  
  15. int main(void)
  16. {
  17. FILE *out;
  18. int sockfd;
  19. struct sockaddr_in my_addr; // my address information
  20. struct sockaddr_in their_addr; // connector's address information
  21. socklen_t addr_len;
  22. int numbytes;
  23. char buf[MAXBUFLEN];
  24.  
  25. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  26. perror("socket");
  27. exit(1);
  28. }
  29.  
  30. my_addr.sin_family = AF_INET; // host byte order
  31. my_addr.sin_port = htons(MYPORT); // short, network byte order
  32. my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
  33. memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
  34.  
  35. if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
  36. perror("bind");
  37. exit(1);
  38. }
  39.  
  40. addr_len = sizeof their_addr;
  41. if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
  42. (struct sockaddr *)&their_addr, &addr_len)) == -1) {
  43. perror("recvfrom");
  44. exit(1);
  45. }
  46. out = fopen("../thanga/xxxx.c","a");//save in a file
  47. fprintf(out,"got packet from %s\n",inet_ntoa(their_addr.sin_addr));
  48. fprintf(out,"packet is %d bytes long\n",numbytes);
  49. buf[numbytes] = '\0';
  50. fprintf(out,"packet contains \"%s\"\n",buf);
  51. fclose(out);mohan
  52.  
  53. close(sockfd);
  54.  
  55. return 0;
  56. }
  57.  
P.S. Originallly i got this code from a tutorial website.I"m only trying to integrate and make changes to fit my requirement.
Feb 28 '08 #4
sicarie
4,677 Expert Mod 4TB
I updated the thread to avoid further confusion, please let me know if you think it should be modified, and what you'd like it changed to.

Moderator
Feb 28 '08 #5
I updated the thread to avoid further confusion, please let me know if you think it should be modified, and what you'd like it changed to.

Moderator
Hi, i was wondering if someone could provide me with a code how to do the above ???? i'm using unix machines.
Feb 28 '08 #6
Can anyone help me here ????
Mar 1 '08 #7
Hi,

have you tried sending the audio file as a text? That should work, or I don't see your problem.
yes i tried doing that..but i'm getting some garbage value in the receiving text file How do i convert the audio file into text/binary and send and then at the receiving end convert it back ?
Mar 1 '08 #8
Hey,
if you can send a text file between the client/server then you can send an audio file, i think you need make the integer buffer smaller, 50000 is too large, and unnecessary. Also you are missing some brackets () when you use sizeof( ).

Goodluck
Mar 4 '08 #9
Banfa
9,056 Expert Mod 8TB
Also you are missing some brackets () when you use sizeof( ).
The ( and ) on sizeof are optional when that operator is applied to a variable (as opposed to a data type).
Mar 4 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Henrik | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.