Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-27-2008, 02:29 PM
phantom308 phantom308 is offline
Newbie
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0
phantom308 is on a distinguished road
Question audio over UDP ?

I've got some code from a tutorial website (Beej's network proamming ) trying to understand how it works .got a lot out of that website !

Issue : i want to Broadcast an audio file to my listeners : which i can record and save .now here is some changes that i've made into the exsisting code but it seems like i have to convert the file into binary first and then convert it back to playback .Not sure how to do that ?????.Please see if anyone could help me do that .
Quote:
Code : Broadcasting file.
Quote:
#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;
}
-------------------------------------------------------------------------------------

Code :listening file
Quote:
#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;
}
-------------------------------------------------------------------------

it listens the contents from broadcasting file and saves it in a file xxxx.c but when sending and audio file it recives some garbage value....
Please advice
Thanks...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 02-28-2008, 12:52 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Datagram sockets are unreliable, and they may not arrive at the destination. And if they actually arrive, then there's a big chance that the data arrived unordered. I would rather use stream sockets. But you'll still have to watch out. If you send large chunks of data, all of it may not arrive, but fortunately you can check how many bytes were sent, and send the rest, if not all the data were sent the first time.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 08:54 PM
TkTech TkTech is offline
 
Join Date: Jun 2006
Posts: 1,034
Last Blog:
Having trouble with yo...
Rep Power: 20
TkTech is on a distinguished road
Send a message via MSN to TkTech
Default

TCP is almost always preffered over UDP. UDP is, technically, less bandwith intensive, but unlike TCP it doesn't guarantee packet delivery. UDP is handy for local networks where you can just keep sending packets and to hell with bandwith. UDP is also used in games like Wc3 where massive amounts of non-crucial traffic need to be sent rapidly.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I Want To Program An Audio Player ibegi.saviour C and C++ 1 02-05-2008 12:55 PM
I Want To Program An Audio Player ibegi.saviour Visual Basic Programming 1 02-05-2008 11:49 AM
How to Store and take audio file from database? heeromai Database & Database Programming 2 11-15-2007 04:59 PM
Blaze Audio Wave Creator 3.2 Kernel Software Development Tools 0 09-26-2006 07:57 AM


All times are GMT -5. The time now is 11:49 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads