Created
April 11, 2019 16:59
-
-
Save Sisekelo/589004ebd300623b20f73f81223ff63f to your computer and use it in GitHub Desktop.
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
| #include <sys/socket.h> | |
| #include <sys/types.h> | |
| #include <netinet/in.h> | |
| #include <netdb.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <errno.h> | |
| #include <arpa/inet.h> | |
| #include "rdwrn.h" | |
| typedef struct { | |
| int id_number; | |
| int age; | |
| float salary; | |
| } employee; | |
| // how to send and receive structs | |
| void send_and_get_employee(int socket, employee *e) | |
| { | |
| size_t payload_length = sizeof(employee); | |
| // send the original struct | |
| writen(socket, (unsigned char *) &payload_length, sizeof(size_t)); | |
| writen(socket, (unsigned char *) e, payload_length); | |
| // get back the altered struct | |
| readn(socket, (unsigned char *) &payload_length, sizeof(size_t)); | |
| readn(socket, (unsigned char *) e, payload_length); | |
| // print out details of received & altered struct | |
| printf("Age is %d\n", e->age); | |
| printf("id is %d\n", e->id_number); | |
| printf("Salary is %6.2f\n", e->salary); | |
| } // end send_and_get_employee() | |
| // how to receive a string | |
| void get_hello(int socket) | |
| { | |
| char hello_string[32]; | |
| size_t k; | |
| readn(socket, (unsigned char *) &k, sizeof(size_t)); | |
| readn(socket, (unsigned char *) hello_string, k); | |
| printf("Hello String: %s\n", hello_string); | |
| printf("Received: %zu bytes\n\n", k); | |
| } // end get_hello() | |
| int main(void) | |
| { | |
| // *** this code down to the next "// ***" does not need to be changed except the port number | |
| int sockfd = 0; | |
| struct sockaddr_in serv_addr; | |
| //socket creation to also return an erro message on fail | |
| if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
| perror("Error - could not create socket"); | |
| exit(EXIT_FAILURE); | |
| } | |
| // Server information. | |
| serv_addr.sin_family = AF_INET;// ip type | |
| serv_addr.sin_port = htons(50001);//port | |
| serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//ip address | |
| // try to connect... | |
| if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1) { | |
| perror("Error - connect failed"); | |
| exit(1); | |
| } else | |
| printf("Connected to server...\n"); | |
| // *** | |
| // your own application code will go here and replace what is below... | |
| // i.e. your menu etc. | |
| char input[5]; | |
| do{ | |
| printf(" System C by Sisekelo\n \n================================================================================"); | |
| printf(" \n\nPress A: Your creator's details \nPress B: Random Number \nPress C: for UNAME \nPress D: for Files \nPress Z: to terminate program \n \n"); | |
| fgets(input, 5, stdin); | |
| size_t n = strlen(input) + 1; | |
| writen(sockfd, (unsigned char *) &n, sizeof(size_t)); | |
| writen(sockfd, (unsigned char *) input, n); | |
| printf("good option %s\n", input); | |
| char valread[32]; | |
| size_t k; | |
| //TO DO: ensure that only a b c are clicked | |
| readn(sockfd, (unsigned char *) &k, sizeof(size_t)); | |
| readn(sockfd, (unsigned char *) valread, k); | |
| printf("%s\n", valread); | |
| printf("\n") | |
| } | |
| while(input[0] != 'q'); | |
| //get_hello(sockfd); | |
| // get a string from the server | |
| //get_hello(sockfd); | |
| // send and receive a changed struct to/from the server | |
| //employee *employee1; | |
| // employee1 = (employee *) malloc(sizeof(employee)); | |
| // arbitrary values | |
| //employee1->age = 23; | |
| //employee1->id_number = 3; | |
| //employee1->salary = 13000.21; | |
| //int i; | |
| //for (i = 0; i < 5; i++) { | |
| //printf("(Counter: %d)\n", i); | |
| //send_and_get_employee(sockfd, employee1); | |
| //printf("\n"); | |
| //} | |
| //free(employee1); | |
| // *** make sure sockets are cleaned up | |
| close(sockfd); | |
| exit(EXIT_SUCCESS); | |
| } // end main() |
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
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <errno.h> | |
| #include <string.h> | |
| #include <sys/types.h> | |
| #include <pthread.h> | |
| #include "rdwrn.h" | |
| // thread function | |
| void *client_handler(void *); | |
| //send Hello | |
| void send_hello(int); | |
| //send Random | |
| void send_rando(int); | |
| // you shouldn't need to change main() in the server except the port number | |
| int main(void) | |
| { | |
| int listenSocket = 0, connfd = 0; | |
| //server structure | |
| struct sockaddr_in serv_addr; | |
| //client structure | |
| struct sockaddr_in client_addr; | |
| //memory reserved for soket | |
| socklen_t socksize = sizeof(struct sockaddr_in); | |
| //socket creation | |
| listenSocket = socket(AF_INET, SOCK_STREAM, 0); | |
| // set asside memory for server structure | |
| memset(&serv_addr, '0', sizeof(serv_addr)); | |
| //instatiate server structure | |
| serv_addr.sin_family = AF_INET; //Address type IPV4 | |
| serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//IP address | |
| serv_addr.sin_port = htons(50001);//port number | |
| //bind new socket to the structure | |
| bind(listenSocket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); | |
| if (listen(listenSocket, 10) == -1) { | |
| perror("Failed to listen"); | |
| exit(EXIT_FAILURE); | |
| } | |
| // end socket setup | |
| printf("Waiting for a client to connect...\n"); | |
| connfd = accept(listenSocket, (struct sockaddr *) &client_addr, &socksize); | |
| printf("Connection accepted...\n"); | |
| //Accept and incoming connection | |
| puts("Waiting for incoming connections..."); | |
| while (1) { | |
| pthread_t sniffer_thread; | |
| // third parameter is a pointer to the thread function, fourth is its actual parameter | |
| if (pthread_create(&sniffer_thread, NULL, client_handler, (void *) &connfd) < 0) { | |
| perror("could not create thread"); | |
| exit(EXIT_FAILURE); | |
| } | |
| //Now join the thread , so that we dont terminate before the thread | |
| pthread_join( sniffer_thread , NULL); | |
| printf("Handler assigned\n"); | |
| } | |
| // never reached... | |
| // ** should include a signal handler to clean up | |
| exit(EXIT_SUCCESS); | |
| } // end main() | |
| // thread function - one instance of each for each connected client | |
| // this is where the do-while loop will go | |
| void *client_handler(void *socket_desc) | |
| { | |
| //Get the socket descriptor | |
| int connfd = *(int *) socket_desc; | |
| char buffer[5]; | |
| do{ | |
| size_t k; | |
| readn(connfd, (unsigned char *) &k, sizeof(size_t)); | |
| readn(connfd, (unsigned char *) buffer, k); | |
| printf(" the input that the client entered is %s\n",buffer ); | |
| switch(buffer[0]) | |
| { | |
| case 'a': | |
| case 'A': | |
| send_hello(connfd); | |
| printf("Your option is: %s", buffer); | |
| break; | |
| case 'b': | |
| case 'B': | |
| send_rando(connfd); | |
| printf("Your option is: %s", buffer); | |
| break; | |
| case 'c': | |
| printf("Your option is: %s", buffer); | |
| break; | |
| default: | |
| write(connfd, buffer, sizeof(buffer)); | |
| printf("Message sent\n"); | |
| printf("Default: Value is: %s\n", buffer); | |
| break; | |
| } | |
| } | |
| while( buffer[0]!= 'q' ); | |
| shutdown(connfd, SHUT_RDWR); | |
| close(connfd); | |
| printf("Thread %lu exiting\n", (unsigned long) pthread_self()); | |
| // always clean up sockets gracefully | |
| shutdown(connfd, SHUT_RDWR); | |
| close(connfd); | |
| return 0; | |
| } // end client_handler() | |
| // how to send a string | |
| void send_hello(int socket) | |
| { | |
| char hello_string[] = "Sisekelo Dlamini | S1719039 | 127.0.0.1"; | |
| size_t n = strlen(hello_string) + 1; | |
| writen(socket, (unsigned char *) &n, sizeof(size_t)); | |
| writen(socket, (unsigned char *) hello_string, n); | |
| } // end send_hello() | |
| // how to send a string 2 | |
| void send_rando(int socket) | |
| { | |
| srand(time(NULL)); | |
| int c, d; | |
| char integer_string[32]; //prepares to be an integer | |
| char other_string[64] = "Your lucky numbers are: "; // create new string for words | |
| char empty_string[12] = " "; //empty string | |
| for (c = 0; c <= 4; c++) { | |
| d = rand() % 100 + 1; // random integer | |
| int integer = d; //get integer | |
| sprintf(integer_string, "%d", integer);//convert integer to string | |
| strcat(other_string, integer_string); // Add integer to new string | |
| strcat(other_string, empty_string); // Add space | |
| }; | |
| size_t n = strlen(other_string) + 1; | |
| writen(socket, (unsigned char *) &n, sizeof(size_t)); | |
| writen(socket, (unsigned char *) other_string, n); | |
| } // end send_rando() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment