Skip to content

Commit

Permalink
Move fopen process to the client side
Browse files Browse the repository at this point in the history
  • Loading branch information
HarinderG committed Mar 19, 2020
1 parent 3ecb1ea commit 55524c1
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 125 deletions.
92 changes: 78 additions & 14 deletions Program 4 - OTP/otp_dec.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Harinder Gakhal
CS 344 - Program 4
otp_dec.c
otp_enc.c
3/13/20
*/

Expand Down Expand Up @@ -53,45 +53,109 @@ void main(int argc, char const *argv[]) {
if (connect(socketFD, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) // Connect socket to address
error("CLIENT: ERROR connecting");

// open file1
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;

fp = fopen(argv[1], "r");
if (fp == NULL)
exit(EXIT_FAILURE);

read = getline(&line, &len, fp);
char msg[strlen(line)];
strcpy(msg, line);
fclose(fp);

//get file size
fp = fopen(argv[1], "r");
fseek(fp, 0L, SEEK_END);
long int sizeofMSG = ftell(fp);
fclose(fp);

char sofMSG[100000];
sprintf(sofMSG, "%ld", sizeofMSG);

// Send size of first file to server
charsWritten = send(socketFD, sofMSG, strlen(sofMSG), 0); // Write to the server
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
if (charsWritten < strlen(sofMSG)) printf("CLIENT: WARNING: Not all data written to socket!\n");
// Get return message from server
memset(buffer, '\0', sizeof(buffer)); // Clear out the buffer again for reuse
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0); // Read data from the socket, leaving \0 at end
if (charsRead < 0) error("CLIENT: ERROR reading from socket");

// if (buffer[0] != 'd'){
// fprintf(stderr, "THE CLIENT IS CONNECTED TO THE WRONG SERVER!\n");
// exit(2);
// }

// Send first file to server
charsWritten = send(socketFD, argv[1], strlen(argv[1]), 0); // Write to the server
charsWritten = send(socketFD, msg, strlen(msg), 0); // Write to the server
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
if (charsWritten < strlen(argv[1])) printf("CLIENT: WARNING: Not all data written to socket!\n");
if (charsWritten < strlen(msg)) printf("CLIENT: WARNING: Not all data written to socket!\n");
// Get return message from server
memset(buffer, '\0', sizeof(buffer)); // Clear out the buffer again for reuse
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0); // Read data from the socket, leaving \0 at end
if (charsRead < 0) error("CLIENT: ERROR reading from socket");

// open file2
FILE * fp2;
char * line2 = NULL;
size_t len2 = 0;
ssize_t read2;

fp2 = fopen(argv[2], "r");
if (fp2 == NULL)
exit(EXIT_FAILURE);

read2 = getline(&line2, &len2, fp2);
char key[strlen(line2)];
strcpy(key, line2);
fclose(fp2);

//get file size
fp2 = fopen(argv[2], "r");
fseek(fp2, 0L, SEEK_END);
long int sizeofKEY = ftell(fp2);

char sofKEY[100000];
sprintf(sofKEY, "%ld", sizeofKEY);

// Send size of second file to server
charsWritten = send(socketFD, sofKEY, strlen(sofKEY), 0); // Write to the server
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
if (charsWritten < strlen(sofKEY)) printf("CLIENT: WARNING: Not all data written to socket!\n");
// Get return message from server
memset(buffer, '\0', sizeof(buffer)); // Clear out the buffer again for reuse
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0); // Read data from the socket, leaving \0 at end
if (charsRead < 0) error("CLIENT: ERROR reading from socket");

// Send second file to server
charsWritten = send(socketFD, argv[2], strlen(argv[2]), 0); // Write to the server
charsWritten = send(socketFD, key, strlen(key), 0); // Write to the server
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
if (charsWritten < strlen(argv[2])) printf("CLIENT: WARNING: Not all data written to socket!\n");

if (charsWritten < strlen(key)) printf("CLIENT: WARNING: Not all data written to socket!\n");
// Get return message from server
memset(buffer, '\0', sizeof(buffer)); // Clear out the buffer again for reuse
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0); // Read data from the socket, leaving \0 at end
if (charsRead < 0) error("CLIENT: ERROR reading from socket");

FILE *fp = fopen(argv[1], "r+");
fseek(fp, 0L, SEEK_END);
long int sizeofFile = ftell(fp);

// Get return message from server
memset(buffer, '\0', sizeof(buffer)); // Clear out the buffer again for reuse
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0); // Read data from the socket, leaving \0 at end
if (charsRead < 0) error("CLIENT: ERROR reading from socket");
sizeofFile -= strlen(buffer);
sizeofMSG -= strlen(buffer);
printf("%s", buffer);

// If message is not fully sent, receive message again
while (sizeofFile != 0) {
while (sizeofMSG != 0) {
if (strlen(buffer) == 0)
break;
memset(buffer, '\0', sizeof(buffer)); // Clear out the buffer again for reuse
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0); // Read data from the socket, leaving \0 at end
if (charsRead < 0) error("CLIENT: ERROR reading from socket");
sizeofFile -= strlen(buffer);
if (charsRead < 0) error("CLIENT: ERROR reading from socket\n");
sizeofMSG -= strlen(buffer);
printf("%s", buffer);
}

Expand Down
112 changes: 60 additions & 52 deletions Program 4 - OTP/otp_dec_d.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/*/*
Harinder Gakhal
CS 344 - Program 4
otp_dec_d.c
otp_enc_d.c
3/13/20
*/

Expand All @@ -15,13 +15,12 @@

// Prototypes
char* decryptMessage(char* msg, char *key);
char* encryptMessage(char* msg, char *key);
void error(const char *msg) { perror(msg); exit(1); } // Error function used for reporting issues

void main(int argc, char const *argv[]) {
int listenSocketFD, establishedConnectionFD, portNumber, charsRead;
socklen_t sizeOfClientInfo;
char buffer[256];
char buffer[1000000];
struct sockaddr_in serverAddress, clientAddress;
pid_t pid;

Expand All @@ -43,82 +42,91 @@ void main(int argc, char const *argv[]) {
error("ERROR on binding");
listen(listenSocketFD, 5); // Flip the socket on - it can now receive up to 5 connections

while (1) {
while(1) {
char msg[1000000] = "", key[1000000] = "";
// Accept a connection, blocking if one is not available until one connects
sizeOfClientInfo = sizeof(clientAddress); // Get the size of the address for the client that will connect
establishedConnectionFD = accept(listenSocketFD, (struct sockaddr *)&clientAddress, &sizeOfClientInfo); // Accept
if (establishedConnectionFD < 0) error("ERROR on accept");

// For every new connection
pid = fork();
if (pid < 0) {
fprintf(stderr, "FORK ERROR\n");
exit(1);
}

if (pid == 0) {
// Get the message from the client and display it
memset(buffer, '\0', 256);
charsRead = recv(establishedConnectionFD, buffer, 255, 0); // Read the client's message from the socket
if (charsRead < 0) error("ERROR reading from socket");

// Get the message from the client
memset(buffer, '\0', 1000000);
charsRead = recv(establishedConnectionFD, buffer, 999999, 0); // Read the client's message from the socket
if (charsRead < 0) fprintf(stderr, "ERROR reading from socket");
// Send a Success message back to the client
charsRead = send(establishedConnectionFD, "I am the server, and I got your message", 39, 0); // Send success back
if (charsRead < 0) error("ERROR writing to socket");

// open file1
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
charsRead = send(establishedConnectionFD, "d", 1, 0); // Send success back
if (charsRead < 0) fprintf(stderr, "ERROR writing to socket");

fp = fopen(buffer, "r");
if (fp == NULL)
exit(EXIT_FAILURE);

read = getline(&line, &len, fp);
char msg[strlen(line)];
strcpy(msg, line);
int sizeofMSG = atoi(buffer);

// Get the message from the client and display it
memset(buffer, '\0', 256);
charsRead = recv(establishedConnectionFD, buffer, 255, 0); // Read the client's message from the socket
if (charsRead < 0) error("ERROR reading from socket");
memset(buffer, '\0', 1000000);
charsRead = recv(establishedConnectionFD, buffer, 999999, 0); // Read the client's message from the socket
if (charsRead < 0) fprintf(stderr, "ERROR reading from socket");
sizeofMSG -= strlen(buffer);
strcat(msg, buffer);

// If message is not fully sent receive message again
while (sizeofMSG != 0) {
if (strlen(buffer) == 0)
break;
memset(buffer, '\0', 1000000);
charsRead = recv(establishedConnectionFD, buffer, 999999, 0); // Read the client's message from the socket
if (charsRead < 0) fprintf(stderr, "ERROR reading from socket");
sizeofMSG -= strlen(buffer);
strcat(msg, buffer);
}
// Send a Success message back to the client
charsRead = send(establishedConnectionFD, "I am the server, and I got your messageeee", 39, 0); // Send success back
if (charsRead < 0) fprintf(stderr, "ERROR writing to socket");

// Get the message from the client
memset(buffer, '\0', 1000000);
charsRead = recv(establishedConnectionFD, buffer, 999999, 0); // Read the client's message from the socket
if (charsRead < 0) fprintf(stderr, "ERROR reading from socket");
// Send a Success message back to the client
charsRead = send(establishedConnectionFD, "I am the server, and I got your message", 39, 0); // Send success back
if (charsRead < 0) error("ERROR writing to socket");
if (charsRead < 0) fprintf(stderr, "ERROR writing to socket");

// open file2
FILE * fp2;
char * line2 = NULL;
size_t len2 = 0;
ssize_t read2;
int sizeofKEY = atoi(buffer);

fp2 = fopen(buffer, "r");
if (fp2 == NULL)
exit(EXIT_FAILURE);

read2 = getline(&line2, &len2, fp2);
char key[strlen(line2)];
strcpy(key, line2);

// Get the message from the client and display it
memset(buffer, '\0', 1000000);
charsRead = recv(establishedConnectionFD, buffer, 999999, 0); // Read the client's message from the socket
if (charsRead < 0) fprintf(stderr, "ERROR reading from socket");
sizeofKEY -= strlen(buffer);
strcat(key, buffer);

// If message is not fully sent receive message again
while (sizeofKEY != 0) {
if (strlen(buffer) == 0)
break;
memset(buffer, '\0', 1000000);
charsRead = recv(establishedConnectionFD, buffer, 999999, 0); // Read the client's message from the socket
if (charsRead < 0) fprintf(stderr, "ERROR reading from socket");
sizeofKEY -= strlen(buffer);
strcat(key, buffer);
}
// Send a Success message back to the client
charsRead = send(establishedConnectionFD, decryptMessage(msg, key), strlen(line), 0); // Send success back
if (charsRead < 0) error("ERROR writing to socket");

fclose(fp);
if (line)
free(line);
charsRead = send(establishedConnectionFD, "I am the server, and I got your message", 39, 0); // Send success back
if (charsRead < 0) fprintf(stderr, "ERROR writing to socket");

fclose(fp2);
if (line2)
free(line2);
// Send a Success message back to the client
charsRead = send(establishedConnectionFD, decryptMessage(msg, key), strlen(msg), 0); // Send success back
if (charsRead < 0) fprintf(stderr, "ERROR writing to socket");
}
close(establishedConnectionFD); // Close the existing socket which is connected to the client
}
close(listenSocketFD); // Close the listening socket
}

char* decryptMessage(char* msg, char *key) {
// Throw error if the key is too short
if (strlen(key) < strlen(msg)) {
Expand Down
Loading

0 comments on commit 55524c1

Please sign in to comment.