Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <string.h> /* strlen, memset */
- #include "list.h"
- #include "ports.h"
- /* Connect to this server using: telnet localhost 12345
- It will respond to one request and close connections */
- pthread_mutex_t list_mutex;
- void* echo_thread(void* socket) {
- char str[BUFFER_SIZE];
- int* comm_fd = (int*)socket;
- while(1){
- memset(str, 0, BUFFER_SIZE);//initializing of str. (Where the request will be)
- int num_bytes = read(*comm_fd, str, BUFFER_SIZE-1);//reading client request
- if (num_bytes == -1) {
- fprintf(stderr, "Error: Reading from client failed, killing thread...\n");
- free(socket);
- return NULL;
- }
- else if (num_bytes == 0) {
- printf("Client disconnected normally, killing thread...\n");
- free(socket);
- return NULL;
- }
- else {
- if(strncmp(str, "ADD:", 4) == 0){
- char *toAdd = str+4;
- /* Check for white spaces */
- char word[MAX_STR];
- int c = 0, d = 0;
- while (toAdd[c] != '\0') {
- if (toAdd[c] == ' ') {
- int temp = c;
- if (toAdd[temp] != '\0') {
- while (toAdd[temp] == ' ' && toAdd[temp] != '\0') {
- if (toAdd[temp] == ' ') {
- c++;
- }
- temp++;
- }
- }
- }
- word[d] = toAdd[c];
- c++;
- d++;
- }
- word[d] = '\0';
- if(search_by_name(word) == NULL){
- pthread_mutex_lock(&list_mutex);
- writeFile(word);
- pthread_mutex_unlock(&list_mutex);
- generate_item(word);
- write(*comm_fd, "OK\n", strlen("OK\n"));
- }
- else{
- write(*comm_fd, "ERROR\n", strlen("ERROR\n"));
- }
- } else if(strncmp(str, "COUNT", 5) == 0){
- int count = count_list_size();
- sprintf(str, "%d\n", count); //int value of count read and saved in buffer str
- if(strncmp(str, "0", 1) == 0){
- write(*comm_fd, str, strlen("0"));
- }
- else {
- write(*comm_fd, str, strlen(str));
- }
- } else if(strncmp(str, "RANDOM", 6) == 0){
- if(count_list_size() == 0){
- write(*comm_fd, "ERROR\n", strlen("ERROR\n"));
- }
- else {
- item *randItem = get_item_at_index(rand() % count_list_size()); //pointer
- strcpy(str, randItem->name); //point -> notation?
- write(*comm_fd, str, strlen(str)); //+1 or -1?
- }
- } else if(strncmp(str, "CLEAR", 5) == 0){ //use toupper to compare
- free_list();
- clearFile();
- write(*comm_fd, "OK\n", strlen("OK\n")+1); //why strlen\n ?
- } else {
- write(*comm_fd, "UNKNOWN\n", strlen("UNKNOWN\n"));
- }
- }
- if (write(*comm_fd, str, strlen(str)+1) < 0) {
- fprintf(stderr, "Error: failed to write to client, killing thread...\n");
- free(socket);
- return NULL;
- }
- printf("Echoed: %s", str);
- }
- }
- int main(int argc, char *argv[]) { //Accepts parameters
- openFile();
- pthread_mutex_init(&list_mutex, NULL);
- int listen_fd;
- struct sockaddr_in cliaddr;
- struct sockaddr_in servaddr;
- socklen_t clilen = sizeof(cliaddr);
- pthread_t mythread;
- listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //socket
- if (listen_fd == -1) {
- fprintf(stderr, "ERROR: socket()\n");
- goto cleanup;
- }
- if(listen_fd < 0){
- printf("Failed to open socket");
- }
- //initialize socket
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- servaddr.sin_port = htons(LISTEN_PORT);
- //Allows re-use of the port as soon as the server terminates
- /* int enable = 1;
- setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
- */
- if(bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < -1){
- printf("bind failed");
- goto cleanup;
- }
- if(listen(listen_fd, 10)){
- printf("listen failed");
- goto cleanup;
- }
- while(1) {
- printf("Accepting new connections...\n");
- int* comm_fd = malloc(sizeof(int));
- if (comm_fd == NULL) {
- fprintf(stderr, "ERROR: malloc() failed\n");
- goto cleanup;
- }
- *comm_fd = accept(listen_fd, (struct sockaddr *)&cliaddr, &clilen);
- if (*comm_fd == -1) {
- fprintf(stderr, "ERROR: accept()\n");
- free(comm_fd);
- goto cleanup;
- }
- printf(" -> New Client\n");
- if (pthread_create(&mythread, NULL, echo_thread, comm_fd) != 0) {
- fprintf(stderr, "ERROR: pthread_create()\n");
- pthread_mutex_destroy(&list_mutex);
- free(comm_fd);
- goto cleanup;
- }
- }
- cleanup:
- close(listen_fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement