sending voice file using socket programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phantom3008
    New Member
    • Feb 2007
    • 9

    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 .
  • Harinezumi
    New Member
    • Feb 2008
    • 32

    #2
    Originally posted by phantom3008
    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.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by phantom3008
      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.

      Comment

      • phantom3008
        New Member
        • Feb 2007
        • 9

        #4
        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 :
        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <errno.h>
        #include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
        #include <netdb.h>
        
        #define SERVERPORT 4950 // the port users will be connecting to
        
        int main(int argc,char *argv[])
        {
        int sockfd;
        struct sockaddr_in their_addr; // connector's address information
        struct hostent *he;
        int numbytes;
        int broadcast = 1;
        FILE *fp1;
        int infile[50000]; //filesize
        int *mypointer
        char c;
        int i = 0;
        
        mypointer = infile;
        
        if (argc != 3) {
        fprintf(stderr,"usage: broadcaster hostname message\n");
        exit(1);
        }
        
        if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
        herror("gethostbyname");
        exit(1);
        }
        
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(1);
        }
        
        
        if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
        sizeof broadcast) == -1) {
        perror("setsockopt (SO_BROADCAST)");
        exit(1);
        }
        fp1 = fopen(argv[2],"r");
        while(( c = getc(fp1)) != EOF)
        {
        infile[i] = c;
        i = i+1;
        };
        their_addr.sin_family = AF_INET; // host byte order
        their_addr.sin_port = htons(SERVERPORT); // short, network byte order
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
        
        if ((numbytes=sendto(sockfd, mypointer, i, 0,
        (struct sockaddr *)&their_addr, sizeof their_addr)) == -1) {
        perror("sendto");
        exit(1);
        }
        
        printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
        
        close(sockfd);
        
        return 0;
        }
        Listening file :
        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <errno.h>
        #include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
        
        #define MYPORT 4950 // the port users will be connecting to
        
        #define MAXBUFLEN 50000
        
        int main(void)
        {
        FILE *out;
        int sockfd;
        struct sockaddr_in my_addr; // my address information
        struct sockaddr_in their_addr; // connector's address information
        socklen_t addr_len;
        int numbytes;
        char buf[MAXBUFLEN];
        
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(1);
        }
        
        my_addr.sin_family = AF_INET; // host byte order
        my_addr.sin_port = htons(MYPORT); // short, network byte order
        my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
        memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
        
        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
        perror("bind");
        exit(1);
        }
        
        addr_len = sizeof their_addr;
        if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
        (struct sockaddr *)&their_addr, &addr_len)) == -1) {
        perror("recvfrom");
        exit(1);
        }
        out = fopen("../thanga/xxxx.c","a");//save in a file
        fprintf(out,"got packet from %s\n",inet_ntoa(their_addr.sin_addr));
        fprintf(out,"packet is %d bytes long\n",numbytes);
        buf[numbytes] = '\0';
        fprintf(out,"packet contains \"%s\"\n",buf);
        fclose(out);mohan
        
        close(sockfd);
        
        return 0;
        }
        P.S. Originallly i got this code from a tutorial website.I"m only trying to integrate and make changes to fit my requirement.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #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

          Comment

          • phantom3008
            New Member
            • Feb 2007
            • 9

            #6
            Originally posted by sicarie
            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.

            Comment

            • phantom3008
              New Member
              • Feb 2007
              • 9

              #7
              Can anyone help me here ????

              Comment

              • phantom3008
                New Member
                • Feb 2007
                • 9

                #8
                Originally posted by Harinezumi
                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 ?

                Comment

                • worldwidegeneration
                  New Member
                  • Mar 2008
                  • 4

                  #9
                  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

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by worldwidegenera tion
                    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).

                    Comment

                    Working...