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...