Saket Soni


back to wav
back to home


///////////////////////////////////client udp////////////////////////////////////

#include "unp.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define BUFF 16404
typedef struct sockaddr SA;
void dg_cli(FILE *, int, struct sockaddr *, socklen_t );
struct soundSet {
	float length;
	int bitsPerSample;
	int numOfChannels;
	int sampleRate;
	int noOfIteration;
}set;
unsigned char *buf = 0;
int connect1;
int main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;
//	if(argc !=2)
//		printf("error ip not supplied\n");//err_quit("usage: udpcli ");
	printf("client program\n");
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;

	servaddr.sin_port = htons(SERV_PORT);
	inet_pton(AF_INET, "192.168.52.40", &servaddr.sin_addr)	;
	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
	dg_cli(stdin, sockfd,  (struct sockaddr *) &servaddr, sizeof(servaddr));
}

void dg_cli(FILE *fp, int sockfd,  struct sockaddr *pservaddr, socklen_t servlen)
{
	int n,  arg, buffSize , status , fd, i;
	char sendline[MAXLINE], recvline[MAXLINE + 1];
	//printf("\nenter msg\n");
	//while(fgets(sendline, MAXLINE, fp) != NULL)
	//{
	//	printf("for loop\n");
		connect1 = 1;
                printf("requesting for connection ...\n");
		sendto(sockfd, &connect1, 4, 0, pservaddr, servlen);
                printf("waiting for reply ...\n");
		n = recvfrom(sockfd, &connect1, 4, 0, NULL, NULL);
                printf("connectin established ...\n");
		if ( connect1 != 1 )
                {
                  printf("\nServer down\n");
                  exit(0);
                }

                printf("requesting for sound settings ...\n");
                sendto(sockfd, &connect1, sizeof(connect1), 0 , pservaddr, servlen );	
                //receiving sound settings in set structure
                printf("waiting for sound settings ...\n");
		n = recvfrom(sockfd, &set, sizeof(set), 0, NULL, NULL);
                if ( n == -1 )
                {
                  perror ( "ERROR is recieving the sound settings ...!!!\n\n");
                  exit(1);
                }
                printf("sound settings recieved ...\n\n");

		printf("\nbits per sample  : %d\n",set.bitsPerSample);
		printf("numOfChannels    : %d\n",set.numOfChannels);
		printf("sampleRate       : %d\n",set.sampleRate);
		printf("length           : %f\n",set.length);
		printf("iteration        : %d\n",set.noOfIteration);
                fflush(stdout);

		fd = open("/dev/dsp", O_WRONLY);   // opening the device file ...
   		if ( fd < 0 )  
		{
        	  perror ( "open of /dev/dsp failed\n" );
        	  exit(0);
                }
                printf("device file opened ...\n");

                // set sample size for audio device 
		arg = set.bitsPerSample;
		status = ioctl ( fd, SOUND_PCM_WRITE_BITS, &arg );
                if ( status == -1 ) {
                  perror("SOUND_PCM_WRITE_BITS ioctl failed\n");
                  exit(1);
                }
                if ( arg != set.bitsPerSample ) {
                  perror( "unable to set sample size\n" );
                  exit(1);
                }
                printf("bits per sample has been set ...\n");
 
                // set no of channels for audio device
                arg = set.numOfChannels;  // mono or stereo
                status = ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &arg );
                if ( status == -1 ) {
                  perror("SOUND_PCM_WRITE_CHANNELS ioctl failed\n");
                  exit(1);
                }
                if ( arg != set.numOfChannels ) {
                  perror("unable to set number of channels\n");
                  exit(1);
                }
                printf("no of channels has been set ...\n");

                // set sampling rate for audio device
                arg = set.sampleRate;	   // sampling rate
                status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
                if (status == -1) {
                  perror("SOUND_PCM_WRITE_WRITE ioctl failed\n");
                  exit(1);
                }
                if ( arg != set.sampleRate ) {
                  perror("unable to set the sampling rate\n");
                  exit(1);
                }
                printf("sampling rate has been set ...\n");                
            // dividing block size parameters
               arg = 2;	   // sampling rate
               status = ioctl(fd, SOUND_PCM_SUBDIVIDE, &arg);
               printf("sub divide :%d :%d\n",status,arg);
               if (status == -1) {
                  perror("SOUND_PCM_SUBDIVIDE ioctl failed\n");
                  exit(1);
            }
  
                buffSize = set.length * set.sampleRate * set.bitsPerSample * set.numOfChannels / 8;
                printf("Buffer Size     : %10d  bytes\n", buffSize);
		buffSize = BUFF;
                if ( buf )
                  free ( buf );
                buf = (unsigned char*) malloc ( buffSize );
                if ( buf == 0 )	{
                  perror ( "Cannot allocate memory for buffer\n");
                  close(fd);
                  exit(1);
                }
                printf("buffer has been allocated ...\n\n");

                printf("sending request for sending sound data ...\n");
                n = sendto(sockfd, &connect1, sizeof(connect1), 0 , pservaddr, servlen );
                if ( n == -1 )
                {
                  perror ( "cannot recieve from the server\n" );
                  exit(1);
                }

		for ( i = 0 ; i < set.noOfIteration-1 ; i++ )
		{
                  //sendto(sockfd, &connect1, 4, 0, pservaddr, servlen);
                  //sendto(sockfd, buf, buffSize, 0, pservaddr, servlen);
//                  printf("Reading   %d ...\n",i);	
                  n = recvfrom(sockfd, buf, buffSize, 0, NULL, NULL);
                  if ( n == -1 )
                  {
                    perror ( "cannot recieve from the server\n" );
                    exit(1);
                  }
//                  printf("Data read %d ...\n",i);
//                  printf("Sending Ack %d ...\n",i);
                  n = sendto(sockfd, &i, sizeof(n), 0, pservaddr, servlen );
                  if ( n == -1 )
                  {
                    perror ( "cannot send acknoledgement to the server\n" );
                    exit(1);
                  }
//                  printf("Ack send    %d ...\n",i);
//                  printf("Playing          %d ...\n",i);
                  status = write ( fd , buf, buffSize );
//                  printf("Playing finished %d ...\n",i);
                  if ( status != buffSize )    {
                    perror ( "wrote wrong number of bytes into the audio device\n" );
                    exit(1);
                  }
/*                  status = ioctl(fd, SOUND_PCM_SYNC, 0); 
                  if (status == -1) {
                    perror( "SOUND_PCM_SYNC ioctl failed\n" );
                    exit(1);
                  }*/
		}
		close(fd);
		//recvline[n] = 0;
	//	fputs(recvline, stdout);
	//}
}



back to wav
back to home

Locations of visitors to this page 1