Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- struct thread_reqs{
- FILE *fp;
- char *message_buffer_compare;
- };
- void *parent_thread(void *thread){
- struct thread_reqs *thread_req = thread;
- int connection_status;
- int size1, size2;
- struct sockaddr_in server_address_thread_req0;
- // "credentials"
- char username[] = "testuser";
- char password[] = "userpass";
- // create socket fd
- int sockfd_thread_req0 = socket(AF_INET, SOCK_STREAM, 0);
- // specify address for socket
- server_address_thread_req0.sin_family = AF_INET;
- server_address_thread_req0.sin_port = htons(9002);
- server_address_thread_req0.sin_addr.s_addr = INADDR_ANY; //later to change to whatever the server address is
- // connect to the server socket
- connection_status = connect(sockfd_thread_req0, (struct sockaddr *) &server_address_thread_req0, sizeof(server_address_thread_req0));
- if(connection_status == -1){
- printf("Error making connection to server, Thread 2\n");
- perror("Connection error: parent");
- return 0;
- }
- // loop the parent process to send any changes to the 'clipboard' file to the server
- while(1){
- char size_buf[20];
- memset(size_buf, 0, sizeof(size_buf));
- // send credentials to server first
- sprintf(size_buf, "%ld", strlen(username) + 1);
- printf("We are sending username size: %s over the buffer\n", size_buf);
- send(sockfd_thread_req0, size_buf, sizeof(char), 0);
- memset(size_buf, 0, sizeof(size_buf));
- send(sockfd_thread_req0, username, sizeof(char) * strlen(username) + 1, 0);
- printf("Sent username: %s over the socket\n", username);
- sprintf(size_buf, "%ld", strlen(password) + 1);
- printf("We are sending password size: %s over the buffer\n", size_buf);
- send(sockfd_thread_req0, size_buf, sizeof(char), 0);
- memset(size_buf, 0, sizeof(size_buf));
- send(sockfd_thread_req0, password, sizeof(char) * strlen(password) + 1, 0);
- printf("Sent password: %s over the socket\n", password);
- printf("Username and password sent: parent process\n");
- // receive message from server confirming authentication
- if(recv(sockfd_thread_req0, size_buf, sizeof(char), 0) == -1){
- perror("Auth message fail Parent");
- } else {
- printf("Auth message received Parent: %s\n", size_buf);
- }
- if(strcmp("Y", size_buf) == 0){
- // Get the size of the current file so we can dynamically allocate
- // space and read the message into a buffer so it can be sent
- fseek(thread_req->fp, 0, SEEK_END);
- size2 = ftell(thread_req->fp);
- fseek(thread_req->fp, 0, SEEK_SET);
- char *client_message_buffer = (char *)calloc(sizeof(char), size2 + 1);
- fread(client_message_buffer, size2 + 1, 1, thread_req->fp);
- // compare the sizes of the buffers to check for changes
- // so that memory can be allocated if required
- if(size2 > size1){
- thread_req->message_buffer_compare = (char *)realloc(thread_req->message_buffer_compare, sizeof(char) * (size2 + 1));
- }
- sprintf(size_buf, "%d", size2);
- size2 = size1;
- // if message changes at all, send it to the server
- if(strcmp(client_message_buffer, thread_req->message_buffer_compare) != 0){
- // send the size of the message to the server
- if(send(sockfd_thread_req0, size_buf, sizeof(size_buf), 0) == -1){
- perror("Error sending size of message: parent");
- }
- // send the message to the server
- if(send(sockfd_thread_req0, client_message_buffer, size2 + 1, 0) == -1){
- perror("Error sending actual message: parent");
- }
- // copy the message from the buffer to the comparison buffer
- strcpy(thread_req->message_buffer_compare, client_message_buffer);
- // free the allocated buffer
- free(client_message_buffer);
- } else {
- // reset the loop due to no change on the current clipboard
- close(sockfd_thread_req0);
- printf("Parent: Resetting loop due to no change on the clipboard");
- continue;
- }
- // reset the loop due to a succesful change on the clipboard
- close(sockfd_thread_req0);
- printf("Parent: Resetting loop due to successful change on the clipboard");
- continue;
- } else {
- printf("Parent: Authentication failed, username or password incorrect\nProgram Exit\n");
- close(sockfd_thread_req0);
- return 0;
- }
- }
- }
- int main(int argc, char **argv){
- int connection_status;
- int size1, size2;
- struct sockaddr_in server_address;
- struct thread_reqs thread_req0;
- pthread_t thread0;
- // return a FD to the 'clipboard' file
- // (in the future this will be the interface
- // to paste messages to, or maybe a GUI pointing
- // to the same space in memory)
- thread_req0.fp = fopen("clipboard", "w+");
- // create a pointer to a struct full of variables to use in other threads
- struct thread_reqs *p_thread_req0 = &thread_req0;
- // "credentials"
- char username[] = "testuser";
- char password[] = "userpass";
- // get the size so we can allocate memory
- // for string to reallocate in a loop later
- fseek(thread_req0.fp, 0, SEEK_END);
- size1 = ftell(thread_req0.fp);
- fseek(thread_req0.fp, 0, SEEK_SET);
- thread_req0.message_buffer_compare = (char *)calloc(sizeof(char), size1 + 1);
- // Create the thread for the other process,
- // this will share the memory of the file for reading and writing
- int thread_req0_status = pthread_create(&thread0, NULL, parent_thread, p_thread_req0);
- // code for child process
- // loop the child process to poll the server for any changes to apply to the 'clipboard' file
- while(1){
- int sockfd;
- // create socket fd
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
- perror("Child");
- }
- // specify address for socket
- server_address.sin_family = AF_INET;
- server_address.sin_port = htons(9004);
- server_address.sin_addr.s_addr = INADDR_ANY; // later to change to whatever the server address is
- // connect to the server socket
- connection_status = connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address));
- if(connection_status == -1){
- printf("Error making connection to server, child proc\n");
- perror("Connection error: child");
- return 0;
- }
- char changed_state[] = "unchanged";
- char changed_compare[] = "changed";
- char unchanged_reset[] = "unchanged";
- char size_buf[20];
- memset(size_buf, 0, sizeof(size_buf));
- // send credentials to server first
- sprintf(size_buf, "%ld", strlen(username) + 1);
- //printf("We are sending username size: %s over the buffer\n", size_buf);
- send(sockfd, size_buf, sizeof(char), 0);
- memset(size_buf, 0, sizeof(size_buf));
- send(sockfd, username, sizeof(char) * strlen(username) + 1, 0);
- //printf("Sent username: %s over the socket\n", username);
- sprintf(size_buf, "%ld", strlen(password) + 1);
- //printf("We are sending password size: %s over the buffer\n", size_buf);
- send(sockfd, size_buf, sizeof(char), 0);
- memset(size_buf, 0, sizeof(size_buf));
- send(sockfd, password, sizeof(char) * strlen(password) + 1, 0);
- //printf("Sent password: %s over the socket\n", password);
- //printf("Username and password sent: child process\n");
- // receive message from server confirming authentication
- if(recv(sockfd, size_buf, sizeof(char), 0) == -1){
- //perror("Auth message fail");
- } else {
- //printf("Auth message received: %s\n", size_buf);
- }
- if(strcmp("Y", size_buf) == 0){
- memset(size_buf, 0, sizeof(size_buf));
- // read response from server which returns a state that tells the client it's time to update the 'clipboard' file
- if(recv(sockfd, changed_state, sizeof(unchanged_reset) + 1, 0) == -1){
- perror("receive state response error");
- }
- printf("State response: %s received from server child process\n", changed_state);
- // when the server comes back with a changed_state message we read
- if(strcmp(changed_state, changed_compare) != 0){
- printf("Child: Resetting loop due to unchanged message from server\n");
- // wait before polling the server again
- fflush(stdout);
- close(sockfd);
- sleep(5);
- continue;
- } else {
- if(recv(sockfd, size_buf, sizeof(size_buf), 0) == -1){
- perror("Receving size of message, child process");
- }
- size1 = atoi(size_buf);
- char *message_buffer = (char *)calloc(sizeof(char), size1 + 1);
- if(recv(sockfd, message_buffer, size1, 0) == -1){
- perror("message received, child process");
- }
- fwrite(message_buffer, size1 + 1, 1, thread_req0.fp);
- strcpy(changed_state, unchanged_reset);
- free(message_buffer);
- }
- } else {
- printf("Child Authentication failed, username or password incorrect\nProgram Exit\n");
- close(sockfd);
- return 0;
- }
- // wait before polling the server again
- fflush(stdout);
- sleep(3);
- }
- fclose(thread_req0.fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement