diff --git a/cnlabtest/CN/A3/q2-audio/a.out b/cnlabtest/CN/A3/q2-audio/a.out new file mode 100644 index 0000000..7999e0d Binary files /dev/null and b/cnlabtest/CN/A3/q2-audio/a.out differ diff --git a/cnlabtest/CN/A3/q2-audio/main.c b/cnlabtest/CN/A3/q2-audio/main.c new file mode 100644 index 0000000..f988571 --- /dev/null +++ b/cnlabtest/CN/A3/q2-audio/main.c @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +#include +#include + +#define FRAMES_PER_BUFFER 1024 + +typedef struct +{ + uint16_t formatType; + uint8_t numberOfChannels; + uint32_t sampleRate; + size_t size; + float *recordedSamples; +} AudioData; + +typedef struct +{ + float *snippet; + size_t size; +} AudioSnippet; + +AudioData initAudioData(uint32_t sampleRate, uint16_t channels, int type) +{ + AudioData data; + data.formatType = type; + data.numberOfChannels = channels; + data.sampleRate = sampleRate; + data.size = 0; + data.recordedSamples = NULL; + return data; +} + +float avg(float *data, size_t length) +{ + float sum = 0; + for (size_t i = 0; i < length; i++) + { + sum += fabs(*(data + i)); + } + return (float) sum / length; +} + +long storeFLAC(AudioData *data, const char *fileName) +{ + + uint8_t err = SF_ERR_NO_ERROR; + SF_INFO sfinfo = + { + .channels = data->numberOfChannels, + .samplerate = data->sampleRate, + .format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16 + }; + + SNDFILE *outfile = sf_open(fileName, SFM_WRITE, &sfinfo); + if (!outfile) return -1; + + // Write the entire buffer to the file + long wr = sf_writef_float(outfile, data->recordedSamples, data->size / sizeof(float)); + err = data->size - wr; + + // Force write to disk and close file + sf_write_sync(outfile); + sf_close(outfile); + puts("Wrote to file!!!!"); + return err; +} + +int main(void) +{ + PaError err = paNoError; + if((err = Pa_Initialize())) goto done; + const PaDeviceInfo *info = Pa_GetDeviceInfo(Pa_GetDefaultInputDevice()); + AudioData data = initAudioData(44100, info->maxInputChannels, paFloat32); + AudioSnippet sampleBlock = + { + .snippet = NULL, + .size = FRAMES_PER_BUFFER * sizeof(float) * data.numberOfChannels + }; + PaStream *stream = NULL; + sampleBlock.snippet = malloc(sampleBlock.size); + time_t talking = 0; + time_t silence = 0; + PaStreamParameters inputParameters = + { + .device = Pa_GetDefaultInputDevice(), + .channelCount = data.numberOfChannels, + .sampleFormat = data.formatType, + .suggestedLatency = info->defaultHighInputLatency, + .hostApiSpecificStreamInfo = NULL + }; + + if((err = Pa_OpenStream(&stream, &inputParameters, NULL, data.sampleRate, FRAMES_PER_BUFFER, paClipOff, NULL, NULL))) goto done; + if((err = Pa_StartStream(stream))) goto done; + for(int i = 0;;) + { + err = Pa_ReadStream(stream, sampleBlock.snippet, FRAMES_PER_BUFFER); + if (err) goto done; + else if(avg(sampleBlock.snippet, FRAMES_PER_BUFFER) > 0.000550 && i < 1000) // talking + { + printf("You're talking! %d\n", i); + i++; + + time(&talking); + data.recordedSamples = realloc(data.recordedSamples, sampleBlock.size * i); + data.size = sampleBlock.size * i; + if (data.recordedSamples) memcpy((char*)data.recordedSamples + ((i - 1) * sampleBlock.size), sampleBlock.snippet, sampleBlock.size); + else + { + free(data.recordedSamples); + data.recordedSamples = NULL; + data.size = 0; + } + } + else //silence + { + double test = difftime(time(&silence), talking); + if (test >= 1.5 && test <= 10) + { + char buffer[100]; + snprintf(buffer, 100, "file:%d.flac", i); + storeFLAC(&data, buffer); + talking = 0; + free(data.recordedSamples); + data.recordedSamples = NULL; + data.size = 0; + } + } + } + +done: + free(sampleBlock.snippet); + Pa_Terminate(); + return err; +} \ No newline at end of file diff --git a/cnlabtest/CN/A3/q2/client b/cnlabtest/CN/A3/q2/client new file mode 100644 index 0000000..0b94312 Binary files /dev/null and b/cnlabtest/CN/A3/q2/client differ diff --git a/cnlabtest/CN/A3/q2/client.c b/cnlabtest/CN/A3/q2/client.c new file mode 100644 index 0000000..93873f3 --- /dev/null +++ b/cnlabtest/CN/A3/q2/client.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +int main() +{ + char buf[100]; + int sock_fd, t; + struct sockaddr_in clientaddr; + fd_set n; + memset(&clientaddr,0,sizeof(clientaddr)); + sock_fd = socket(AF_INET,SOCK_STREAM,0); + if (sock_fd==-1) + { + printf("Socket creation error\n"); + return 0; + } + + clientaddr.sin_family = AF_INET; + clientaddr.sin_addr.s_addr=INADDR_ANY; + clientaddr.sin_port=5005; + + t = connect(sock_fd,(struct sockaddr *)&clientaddr,sizeof(clientaddr)); + if (t==-1) + { + printf("Connection error\n"); + return 0; + } + + + printf("Type your Message and press Enter\n"); + while (1) + { + FD_ZERO(&n); + FD_SET(0,&n); + FD_SET(sock_fd,&n); + int ret=select(10,&n,0,0,0); + if (ret==-1) + { + printf("SELECT Error\n"); + close(sock_fd); + return 0; + } + else if (FD_ISSET(0,&n)) + { + + fgets(buf,100,stdin); + + t = send(sock_fd,buf,100,0); + if (t==-1) + { + printf("Message Send error\n"); + return 0; + } + if (strncmp(buf,"bye",3)==0) + { + close(sock_fd); + return 0; + } + + } + else if (FD_ISSET(sock_fd,&n)) + { + t = recv(sock_fd,buf,100,0); + if (t==-1) + { + printf("Message Receive error\n"); + close(sock_fd); + return 0; + } + if (strncmp(buf,"bye",3)==0) + { + close(sock_fd); + return 0; + } + printf("\nMessage from Server: %s\n",buf); + } + } + return 0; +} + +//scp [remoteUserName]@[ip]:[path to file] [path where to copy in your comp.] \ No newline at end of file diff --git a/cnlabtest/CN/A3/q2/server b/cnlabtest/CN/A3/q2/server new file mode 100644 index 0000000..a585e01 Binary files /dev/null and b/cnlabtest/CN/A3/q2/server differ diff --git a/cnlabtest/CN/A3/q2/server.c b/cnlabtest/CN/A3/q2/server.c new file mode 100644 index 0000000..d790436 --- /dev/null +++ b/cnlabtest/CN/A3/q2/server.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + int sock_fd, new_fd, t; + socklen_t len; + char buf[100]; + fd_set n; + struct sockaddr_in serveraddr, clientaddr; + + memset(&serveraddr,0,sizeof(serveraddr)); + memset(&clientaddr,0,sizeof(clientaddr)); + + sock_fd = socket(AF_INET,SOCK_STREAM,0); + if (sock_fd==-1) + { + printf("Socket creation error\n"); + return 0; + } + + + serveraddr.sin_family=AF_INET; + serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1"); + serveraddr.sin_port=5005; + + t = bind(sock_fd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)); + if (t==-1) + { + printf("Bind error\n"); + return 0; + } + + t = listen(sock_fd,20); + if (t==-1) + { + printf("Listen error\n"); + return 0; + } + + len = sizeof(clientaddr); + new_fd = accept(sock_fd,(struct sockaddr *)&clientaddr, &len); + if (new_fd==-1) + { + printf("New_FD error\n"); + return 0; + } + printf("Type your Message and press Enter\n"); + while (1) + { + FD_ZERO(&n); + FD_SET(0,&n); + FD_SET(new_fd,&n); + int ret=select(10,&n,0,0,0); + if (ret==-1) + { + printf("SELECT Error\n"); + close(new_fd); + close(sock_fd); + return 0; + } + else if (FD_ISSET(0,&n)) + { + fgets(buf,100,stdin); + + t = send(new_fd,buf,100,0); + if (t==-1) + { + printf("Message Send error\n"); + return 0; + } + if (strncmp(buf,"bye",3)==0) + { + close(new_fd); + close(sock_fd); + return 0; + } + + } + else if (FD_ISSET(new_fd,&n)) + { + t = recv(new_fd,buf,100,0); + if (t==-1) + { + printf("Message Receive error\n"); + close(new_fd); + close(sock_fd); + return 0; + } + if (strncmp(buf,"bye",3)==0) + { + close(new_fd); + close(sock_fd); + return 0; + } + printf("\nMessage from Client: %s\n",buf); + } + } + close(new_fd); + close(sock_fd); + return 0; +} + + + + diff --git a/cnlabtest/fork/client1.c b/cnlabtest/fork/client1.c new file mode 100644 index 0000000..d4d41aa --- /dev/null +++ b/cnlabtest/fork/client1.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 500 +#define PORT 5432 + + + + + +int main(int argc, char* argv[]) +{ + int sockfd , connfd; + struct sockaddr_in sin,cli; + struct hostent *hp; + + char *host; + + host = argv[1]; + + //printf("%s\n", host); + hp = gethostbyname(host); + + + + + bzero(&sin, sizeof(sin)); + + sin.sin_family = AF_INET; + bcopy(hp->h_addr,(char*)&sin.sin_addr,hp->h_length); + sin.sin_port = htons(PORT); + + sockfd = socket(PF_INET, SOCK_STREAM,0); + + if(sockfd == -1) + { + printf("socket creation failed\n"); + } + + + if(connect(sockfd,( struct sockaddr*)&sin,sizeof(sin))!=0) + { + printf("error\n"); + } + else + { + printf("I am connected\n"); + + char name[7] = "hi.txt\0"; + + send(sockfd, name, sizeof(name),0); + + // receive file + + char file[11]; + + recv(sockfd, file, sizeof(file),0); + + for(int i=0;i<11;i++) + { + file[i] = file[i] ^ 'S'; + } + + printf("%s\n", file); + FILE *fd = fopen("program1.txt", "w"); + fprintf(fd,"%s",file); + + + } + + + close(sockfd); + +} diff --git a/cnlabtest/fork/client2.c b/cnlabtest/fork/client2.c new file mode 100644 index 0000000..c03a9fa --- /dev/null +++ b/cnlabtest/fork/client2.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 500 +#define PORT 5432 + + + + + +int main(int argc, char* argv[]) +{ + int sockfd , connfd; + struct sockaddr_in sin,cli; + struct hostent *hp; + + char *host; + + host = argv[1]; + + //printf("%s\n", host); + hp = gethostbyname(host); + + + + + bzero(&sin, sizeof(sin)); + + sin.sin_family = AF_INET; + bcopy(hp->h_addr,(char*)&sin.sin_addr,hp->h_length); + sin.sin_port = htons(PORT); + + sockfd = socket(PF_INET, SOCK_STREAM,0); + + if(sockfd == -1) + { + printf("socket creation failed\n"); + } + + + if(connect(sockfd,( struct sockaddr*)&sin,sizeof(sin))!=0) + { + printf("error\n"); + } + else + { + printf("I am connected\n"); + + char name[7] = "hi.txt\0"; + + send(sockfd, name, sizeof(name),0); + + // receive file + + char file[11]; + + recv(sockfd, file, sizeof(file),0); + + for(int i=0;i<11;i++) + { + file[i] = file[i] ^ 'S'; + } + + printf("%s\n", file); + FILE *fd = fopen("program2.txt", "w"); + fprintf(fd,"%s",file); + + + } + + + close(sockfd); + +} diff --git a/cnlabtest/fork/client3.c b/cnlabtest/fork/client3.c new file mode 100644 index 0000000..ac8c4ed --- /dev/null +++ b/cnlabtest/fork/client3.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 500 +#define PORT 5432 + + + + + +int main(int argc, char* argv[]) +{ + int sockfd , connfd; + struct sockaddr_in sin,cli; + struct hostent *hp; + + char *host; + + host = argv[1]; + + //printf("%s\n", host); + hp = gethostbyname(host); + + + + + bzero(&sin, sizeof(sin)); + + sin.sin_family = AF_INET; + bcopy(hp->h_addr,(char*)&sin.sin_addr,hp->h_length); + sin.sin_port = htons(PORT); + + sockfd = socket(PF_INET, SOCK_STREAM,0); + + if(sockfd == -1) + { + printf("socket creation failed\n"); + } + + + if(connect(sockfd,( struct sockaddr*)&sin,sizeof(sin))!=0) + { + printf("error\n"); + } + else + { + printf("I am connected\n"); + + char name[7] = "hi.txt\0"; + + send(sockfd, name, sizeof(name),0); + + // receive file + + char file[11]; + + recv(sockfd, file, sizeof(file),0); + + for(int i=0;i<11;i++) + { + file[i] = file[i] ^ 'S'; + } + + printf("%s\n", file); + FILE *fd = fopen("program3.txt", "w"); + fprintf(fd,"%s",file); + + + } + + + close(sockfd); + +} diff --git a/cnlabtest/fork/hi.txt b/cnlabtest/fork/hi.txt new file mode 100644 index 0000000..c59f2ce --- /dev/null +++ b/cnlabtest/fork/hi.txt @@ -0,0 +1 @@ +This is a sample file!!!!!!!!!!!!!!!! diff --git a/cnlabtest/fork/program1.txt b/cnlabtest/fork/program1.txt new file mode 100644 index 0000000..18e1714 --- /dev/null +++ b/cnlabtest/fork/program1.txt @@ -0,0 +1 @@ +This is a s \ No newline at end of file diff --git a/cnlabtest/fork/program2.txt b/cnlabtest/fork/program2.txt new file mode 100644 index 0000000..18e1714 --- /dev/null +++ b/cnlabtest/fork/program2.txt @@ -0,0 +1 @@ +This is a s \ No newline at end of file diff --git a/cnlabtest/fork/program3.txt b/cnlabtest/fork/program3.txt new file mode 100644 index 0000000..18e1714 --- /dev/null +++ b/cnlabtest/fork/program3.txt @@ -0,0 +1 @@ +This is a s \ No newline at end of file diff --git a/cnlabtest/fork/server.c b/cnlabtest/fork/server.c new file mode 100644 index 0000000..ad7c247 --- /dev/null +++ b/cnlabtest/fork/server.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 500 +#define PORT 5432 +char buffer[32]; + + +int main() +{ + int sockfd , connfd; + struct sockaddr_in sin,cli; + + sockfd = socket(PF_INET, SOCK_STREAM,0); + + if(sockfd == -1) + { + printf("socket creation failed\n"); + } + + bzero(&sin, sizeof(sin)); + + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_port = htons(PORT); + + if(bind(sockfd,(struct sockaddr*)&sin,sizeof(sin))!=0) + { + printf("socket binding failed\n"); + } + + if(listen(sockfd,5)!=0) + { + printf("failed\n"); + } + + for(int i=0;i<3;i++) + { + if(fork() == 0) + { + printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid()); + int len = sizeof(sin); + connfd = accept(sockfd, ( struct sockaddr*)&sin,&len); + + if(connfd<0) + { + printf("accept failed\n"); + } + + else + { + printf("%d is connected\n", getpid()); + for(int i=0;i<32;i++) + { + buffer[i] = '\0'; + } + recv(connfd, buffer , sizeof(buffer),0); + + FILE* fp = fopen(buffer,"r"); + + + for(int i=0;i<32;i++) + { + buffer[i] = '\0'; + } + + + char ch, ch2; + for (i = 0; i < 32; i++) { + ch = fgetc(fp); + printf("%c",ch); + ch2 = ch ^ 'S'; + buffer[i] = ch2; + } + printf("\n"); + + send(connfd, buffer, sizeof(buffer), 0); + } + + close(sockfd); + + exit(0); + } + + } + + for(int i=0;i<3;i++) // loop will run n times (n=5) + wait(NULL); + +}