Created
August 25, 2015 20:36
-
-
Save tanayseven/249acfdc7b2fce03ca6b to your computer and use it in GitHub Desktop.
File Downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2015 Tanay PrabhuDesai | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <fcntl.h> | |
#include <netinet/in.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#define ERROR -1 | |
#define PORT 29008 | |
#define LISTEN_BACKLOG 1 | |
#define CONN_ADDR "127.0.0.1" | |
#define MAX_STR_LEN 2048 | |
void write_file( char *filename, char *file, int len) { | |
int op_file; | |
op_file = open(filename, O_WRONLY | O_CREAT); | |
if ( op_file == ERROR ) { | |
perror("Error accessing file.\n"); | |
return; | |
} | |
write(op_file,file,len); | |
} | |
int main(void) | |
{ | |
int sfd = socket(AF_INET, SOCK_STREAM, 0), len; | |
char *str_buf, *file; | |
if (sfd == ERROR) { | |
perror("Could not create a socket"); | |
return 1; | |
} | |
char ip[20];int port; | |
struct sockaddr_in server_addr; | |
printf("Enter the ip address: "); | |
scanf("%s",ip); | |
printf("Enter the port number: "); | |
scanf("%d",&port); | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_addr.s_addr = inet_addr(ip); | |
server_addr.sin_port = htons(port); | |
if (connect(sfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == ERROR ) { | |
perror("Could not connect to the server"); | |
return 2; | |
} | |
fflush(stdout); | |
fflush(stdin); | |
len = MAX_STR_LEN; | |
str_buf = (char *)malloc(len* sizeof(char)); | |
printf("Request a file from the server: "); | |
scanf("%s",str_buf); | |
len = strlen(str_buf); | |
write(sfd, &len, sizeof(len)); | |
write(sfd, str_buf, len); | |
read(sfd,&len, sizeof(len)); | |
file = (char*)malloc(len * sizeof(char) ); | |
read(sfd,file,len); | |
write_file(str_buf,file,len); | |
free(str_buf); | |
free(file); | |
close(sfd); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2015 Tanay PrabhuDesai | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#define ERROR -1 | |
#define CONN_ADDR "127.0.0.1" | |
#define PORT 29008 | |
#define BUF_SIZE 2048 | |
#define END 0 | |
char* read_file(char* filename, int* len) { | |
int ip_file, rd_cnt, tot_cnt = 0; | |
int buf_size = BUF_SIZE; | |
char * tmp_buf = malloc(buf_size * sizeof(char)); | |
char * offset = tmp_buf; | |
ip_file = open(filename, O_RDONLY); | |
if ( ip_file == ERROR ) { | |
perror("Error accessing file."); | |
return (char*)-1; | |
} | |
do { | |
rd_cnt = read(ip_file,offset,BUF_SIZE); | |
offset += rd_cnt; | |
tot_cnt += rd_cnt; | |
}while(rd_cnt != END); | |
(*len) = tot_cnt; | |
return tmp_buf; | |
} | |
int main(void) { | |
int fd = socket(AF_INET, SOCK_STREAM, 0), cfd, len; | |
pid_t child; | |
char *str_buf, *file; | |
if (fd == ERROR) { | |
perror("Could not create a socket"); | |
return 1; | |
} | |
struct sockaddr_in my_addr; | |
my_addr.sin_family = AF_INET; | |
my_addr.sin_addr.s_addr = inet_addr(CONN_ADDR); //accept any IP address communication | |
my_addr.sin_port = htons(PORT); | |
if (bind(fd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == ERROR) { | |
perror("Could not bind the socket"); | |
return 2; | |
} | |
if (listen(fd, 5) == ERROR ) { | |
perror("Could not listen to client"); | |
return 3; | |
} | |
printf("Listening on: %s:%d\n", CONN_ADDR, PORT); | |
while (1) { | |
cfd = accept(fd, (struct sockaddr *) NULL, NULL); | |
child = fork(); | |
if (child == ERROR) { | |
perror("Could not fork a child"); | |
return 4; | |
} else if ( child == 0 ) { | |
if ( cfd == ERROR ) { | |
perror("Could not connect to client"); | |
return 5; | |
} | |
fflush(stdout); | |
fflush(stdin); | |
read(cfd, &len, sizeof(len)); | |
str_buf = (char*)malloc(len * sizeof(char) ); | |
read(cfd, str_buf, len); | |
printf("Client requested file: %s\n", str_buf); | |
file = read_file(str_buf,&len); | |
if (file == (char *) ERROR) { | |
len = 0; | |
printf("Could not receive the file!\n"); | |
} | |
write(cfd, &len, sizeof(len)); | |
write(cfd, file, len); | |
break; | |
} | |
} | |
free(str_buf); | |
free(file); | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment