Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaynigam authored Sep 28, 2016
1 parent 638a77f commit ca5e9b5
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 0 deletions.
108 changes: 108 additions & 0 deletions Client/Client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define SERVER_PORT 5432
#define MAX_LINE 80

int main(int argc, char * argv[])
{
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char *fname;
char buf[MAX_LINE];
char buf2[MAX_LINE];
int s;
int slen;
struct timeval tv;

if (argc==3) {
host = argv[1];
fname= argv[2];
}
else {
fprintf(stderr, "Usage: ./a.out host filename\n");
exit(1);
}
hp = gethostbyname(host); // it is converting ip address fed into hosten and stored in hp
/* translate host name into peer’s IP address */
if (!hp) { // Checks if there exists a hostent
fprintf(stderr, "Unknown host: %s\n", host);
exit(1);
}

fp = fopen(fname, "r"); // we read a file with filename provided and gives read only access
if (fp==NULL){
fprintf(stderr, "Can't open file: %s\n", fname);
exit(1);
}

/* build address data structure */
bzero((char *)&sin, sizeof(sin)); // same as server code
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); //bcopy copies any data (strcpy copies just string). copying address of structure of host(ip address of server) into address of structure of client socket.
sin.sin_port = htons(SERVER_PORT);

/* active open */
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { //same as server code
perror("Socket");
close(s);
exit(1);
}
tv.tv_sec = 1;
tv.tv_usec = 0;

srandom(time(0)); // generate random number
socklen_t sock_len= sizeof sin;


/* main loop: get and send lines of text */
while(fgets(buf, 80, fp) != NULL){ // get values from the file fp and store in the socket buffer
buf2[0] = 0x00;
slen = strlen(buf);
buf[slen] ='\0'; // last value of the buffer at buffer address length+1 be added as /0 to indicate the end of buffer
if(random()>RAND_MAX/4){
if(sendto(s, buf, slen+1, 0, (struct sockaddr *)&sin, sock_len)<0){ // if -1 then show error (socket id, buffer, length f the buffer+/0, flag, socket address, socket length)
perror("SendTo Error\n");
exit(1);
printf(buf,"\n");
}}
while(buf2[0]==0x00){
if (setsockopt(s,SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
perror("PError");
}
if(recvfrom(s,buf2,sizeof(buf2),0,(struct sockaddr *)&sin, &sock_len)<0){
perror("Perror");
}
if(buf2[0]==0x01){
break;
}
else{
if(random()>RAND_MAX/4){
if(sendto(s, buf, slen+1, 0, (struct sockaddr *)&sin, sock_len)<0){ // if -1 then show error (socket id, buffer, length f the buffer+/0, flag, socket address, socket length)
perror("SendTo Error\n");
exit(1);
}}
}

}
}


*buf = 0x02; // send 0x02 as the last value to denote end of transmission
if(sendto(s, buf, 1, 0, (struct sockaddr *)&sin, sock_len)<0){
perror("SendTo Error\n");
exit(1);
}
fclose(fp);
close(s); //close socket and file
}

Binary file added Client/Client.out
Binary file not shown.
38 changes: 38 additions & 0 deletions Client/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ooMMMMMMMooo
oMMMMMMMMMMMMMMMoo
MMMMMMMMMMMMMMo"MMMo
"MMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMo
MMMM""MMMMMM"o" MMMMMMM
MMo o" MMM" oo ""MMMMM
MM MMo MMM" MMoM "MMMMM
MMo"M"o" "" MMM" oMMMMM"
oMM M o" " o "o MMMMMM"
oM"o " o " o "o MMMMMMM
oMMoM o " M M "o MMMM"MMo
Mo " M "M "o" o MMMoMMMo
MMo " "" M " MMMMMMMo
oMM" "o o " MMMMMMMM
MMM" MMMMMMMMo
oMMMo "MMMMMMMMo
MMMMM o " " o" "MMMMMMMMMo
MMMMM " " "MMMMMMMMMo
oMMMM ""MMMMMMMMMo
oMMMM o o MMoMMMMMMM
MMMM o "MMMMMMMMMM
MMMM" o o o "MMMMMMMMMMo
oMMMMM MMMMMMMMMMo
MMM"MM "MMM"MMMMMMM
MMMMMM " o " MMMMMMMMMMM
"o "ooo o o o"MMMMMMMMoM"
" o "o "MMo " o" MMMMMMMM"
o "o" o o " MMMo o o""""MMMM"o" "
" o "o " o o" " MMMMoo " o "o M"" M "o " "
"o o" " o o" " " "MMMM" o M o "o" o" o" " o
M o M " o " " " " MM"" o oMo"o " o o "o " "o "
o" o " "o " " M " " o MMMMo"o " o o o o" o o" "
o" "o " o " " o o" M "oo ooMMMMMMM o "o o o " o o o "
M "o o" o" "o o o " o"oMMMMMMMMMMMMMMMMMMMo" o o "o "o o"
"" "o"o"o"o o"o "o"o"oMMMMMMMMMMMMMMMMMMo"o"o "o o"oo"
"" M Mo"o"oo"oM"" " MMoM M M M
""" """ " """ "
92 changes: 92 additions & 0 deletions Server/Server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <fcntl.h>

#define SERVER_PORT 5432
#define MAX_LINE 256

int main(int argc, char * argv[])
{
char *fname;
char buf[MAX_LINE];
char buf2[MAX_LINE];
struct sockaddr_in sin;
int len;
int s, i;
struct timeval tv;
char seq_num = 1;
FILE *fp;

if (argc==2) {
fname = argv[1];
}
else {
fprintf(stderr, "usage: ./client_udp filename\n");
exit(1);
}


/* build address data structure */
bzero((char *)&sin, sizeof(sin)); // cleaning -> makes sin all 0
sin.sin_family = AF_INET; // sin belongs to AFINET family. socket sin is addressed to AFINET family. ipv_4 internet protocol
sin.sin_addr.s_addr = INADDR_ANY; // can take any legal address. You can add an ip address through this command. INADDR_ANY is 0
sin.sin_port = htons(SERVER_PORT); // obtains server port (5432) htons converts integer port number to hardware port number. uses big endian or small endian according to the system.

/* setup passive open */
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { //PF_INET used, although AFINET can be used too. PFINET addresses a wider range of protocol family. this command creates a socket s and gives it arguments. SOCK_DGRAM uses udp datagram. And protocol number is 0 in usual cases.
perror("simplex-talk: socket");
close(s);
exit(1); // when error exist socket() returns value -1. hence the if statement. and exit the program.
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) { // bind (socket file descriptor (int), sin memory location, size of sin). technically assigning name to the socket.
perror("simplex-talk: bind");
close(s);
exit(1); // error if -1 is returned.
}

// now the server connection is established
socklen_t sock_len = sizeof sin; // getting size of sin and storing it
srandom(time(NULL)); // random number generated

fp = fopen(fname, "w"); //opening a file input later. we want to open and write the file.
if (fp==NULL){
printf("Can't open file\n"); // if fp is still NULL, then we couldn't open the file.
exit(1);
}

while(1){
len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &sock_len); //the server is now listening. it recieves messages from socket.(socket name, buffer, size of buffer, socket address and socket length)

if(len == -1){
perror("PError");
}

else if(len == 1){ // the last value after transmission completion writes at buffer address 0x02. hence after reading it, transmission completes.
if (buf[0] == 0x02){
printf("Transmission Complete\n");
break;
}
else{ // if the last value after transmission completion writes at some other value, there is an error.
perror("Error: Short packet\n");
}
}
else if(len > 1){ // here the server receives data from the buffer of the socket and writes it in a file fp.
if(fputs((char *) buf, fp) < 1){
printf("fputs() error\n");
}}
*buf2 = 0x01;
if(sendto(s,buf2,1,0,(struct sockaddr *)&sin, sock_len)<0){
perror("Sendto Error\n");
exit(1);
}
}
fclose(fp); //it is necessary to close filename
close(s); // it is necessary to close socket or else ram fills fast and it may cause crashes.
}

Binary file added Server/Server.out
Binary file not shown.
38 changes: 38 additions & 0 deletions Server/rec.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ooMMMMMMMooo
oMMMMMMMMMMMMMMMoo
MMMMMMMMMMMMMMo"MMMo
"MMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMo
MMMM""MMMMMM"o" MMMMMMM
MMo o" MMM" oo ""MMMMM
MM MMo MMM" MMoM "MMMMM
MMo"M"o" "" MMM" oMMMMM"
oMM M o" " o "o MMMMMM"
oM"o " o " o "o MMMMMMM
oMMoM o " M M "o MMMM"MMo
Mo " M "M "o" o MMMoMMMo
MMo " "" M " MMMMMMMo
oMM" "o o " MMMMMMMM
MMM" MMMMMMMMo
oMMMo "MMMMMMMMo
MMMMM o " " o" "MMMMMMMMMo
MMMMM " " "MMMMMMMMMo
oMMMM ""MMMMMMMMMo
oMMMM o o MMoMMMMMMM
MMMM o "MMMMMMMMMM
MMMM" o o o "MMMMMMMMMMo
oMMMMM MMMMMMMMMMo
MMM"MM "MMM"MMMMMMM
MMMMMM " o " MMMMMMMMMMM
"o "ooo o o o"MMMMMMMMoM"
" o "o "MMo " o" MMMMMMMM"
o "o" o o " MMMo o o""""MMMM"o" "
" o "o " o o" " MMMMoo " o "o M"" M "o " "
"o o" " o o" " " "MMMM" o M o "o" o" o" " o
M o M " o " " " " MM"" o oMo"o " o o "o " "o "
o" o " "o " " M " " o MMMMo"o " o o o o" o o" "
o" "o " o " " o o" M "oo ooMMMMMMM o "o o o " o o o "
M "o o" o" "o o o " o"oMMMMMMMMMMMMMMMMMMMo" o o "o "o o"
"" "o"o"o"o o"o "o"o"oMMMMMMMMMMMMMMMMMMo"o"o "o o"oo"
"" M Mo"o"oo"oM"" " MMoM M M M
""" """ " """ "
38 changes: 38 additions & 0 deletions Server/receive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ooMMMMMMMooo
oMMMMMMMMMMMMMMMoo
MMMMMMMMMMMMMMo"MMMo
"MMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMo
MMMM""MMMMMM"o" MMMMMMM
MMo o" MMM" oo ""MMMMM
MM MMo MMM" MMoM "MMMMM
MMo"M"o" "" MMM" oMMMMM"
oMM M o" " o "o MMMMMM"
oM"o " o " o "o MMMMMMM
oMMoM o " M M "o MMMM"MMo
Mo " M "M "o" o MMMoMMMo
MMo " "" M " MMMMMMMo
oMM" "o o " MMMMMMMM
MMM" MMMMMMMMo
oMMMo "MMMMMMMMo
MMMMM o " " o" "MMMMMMMMMo
MMMMM " " "MMMMMMMMMo
oMMMM ""MMMMMMMMMo
oMMMM o o MMoMMMMMMM
MMMM o "MMMMMMMMMM
MMMM" o o o "MMMMMMMMMMo
oMMMMM MMMMMMMMMMo
MMM"MM "MMM"MMMMMMM
MMMMMM " o " MMMMMMMMMMM
"o "ooo o o o"MMMMMMMMoM"
" o "o "MMo " o" MMMMMMMM"
o "o" o o " MMMo o o""""MMMM"o" "
" o "o " o o" " MMMMoo " o "o M"" M "o " "
"o o" " o o" " " "MMMM" o M o "o" o" o" " o
M o M " o " " " " MM"" o oMo"o " o o "o " "o "
o" o " "o " " M " " o MMMMo"o " o o o o" o o" "
o" "o " o " " o o" M "oo ooMMMMMMM o "o o o " o o o "
M "o o" o" "o o o " o"oMMMMMMMMMMMMMMMMMMMo" o o "o "o o"
"" "o"o"o"o o"o "o"o"oMMMMMMMMMMMMMMMMMMo"o"o "o o"oo"
"" M Mo"o"oo"oM"" " MMoM M M M
""" """ " """ "

0 comments on commit ca5e9b5

Please sign in to comment.