示例#1
0
void clientHandler(int clientSocket)
{
    // connection error
    if(clientSocket < 0){
        printf("%s", "Error connecting to clientSocket.\n");
        exit(EXIT_FAILURE);
    }

    int in_session = -1;

    // read from client
    char buffer[256]; // buffer for socket messages
    while(1){
        bzero((void*) buffer, 256);
        if((read(clientSocket, buffer, 255)) <= 0){ // no response
            exit(EXIT_SUCCESS);
        }

        // parse command
        char *command = strtok(buffer, " \n");
        char *commandArg = strtok(NULL, " \n");

        if(strcmp(command, "open") == 0){ // open account
           openAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "start") == 0){ // start account session
            startAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "credit") == 0){ // credit account
            creditAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "debit") == 0){ // debit account
            debitAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "balance") == 0){ // get balance
            getBalance(clientSocket, &in_session);
        }
        else if(strcmp(command, "finish") == 0){ // finish
            finishSession(clientSocket, &in_session);
        }
        else{
            printf("%s", "Client done goofed, abort mission.");
            exit(EXIT_FAILURE);
        }
    }

    return;
}
示例#2
0
void *new_session(void *arg) {
    
    connection_t * conn;
    char acctName[100];
    char buffer[256];
    char *buf;
    long n;
    int rc;
    //long addr = 0;
    
    if (!arg)
        pthread_exit(0);
    conn = (connection_t *)arg;
    
    
    while(1){
        int x, index;
        double amount;
        double balance;
        bzero(buffer,256);
        bzero(acctName,100);
        n = read(conn->sock,buffer,255);
        if (n < 0){
            printf("Error reading from socket\n");
            pthread_exit(NULL);
        }
        
        if(strncasecmp(buffer, "Exit", 4) == 0){
            
            if(conn->session > -1)
                bank->accounts[conn->session].session = -1;
            
            conn->session = -1;
            
            printf("Client %d exited\n", conn->sock);
            n = write(conn->sock, "Exiting...", 10);
            close(conn->sock);
            free(conn);
            pthread_exit(NULL);
        }
        if(strncasecmp(buffer, "Open", 4) == 0){
            //n = write(conn->sock,"Open",4);
            
            
            buf = buffer + 4;
            
            sscanf(buf, "%s", acctName);
            
            if(addAccount(acctName) == 0){
                printf("Client %d added account: %s\n", conn->sock, acctName);
                n = write(conn->sock,"Added account\n", 30);
            }else{
                printf("Client %d could not add account: %s. The account already exists or there is no space available.\n", conn->sock, acctName);
                n = write(conn->sock, "Error: could not add account. The account already exists or there is no space available.\n", 100);
            }
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
            
        } else if(strncasecmp(buffer, "Start", 5) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Start",5);
            
            if(conn->session != -1){
                printf("Client %d tried to start an account but client is already in session\n", conn->pid);
                n = write(conn->sock,"Client is already in session\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer + 5;
            
            sscanf(buf, "%s", acctName);
            
            x = startAccount(acctName);
            
            if(x >= 0){
                printf("Client %d started account: %s\n", conn->pid, acctName);
                n = write(conn->sock,"Started account\n", 30);
                conn->session = x;
            }else if(x == -1){
                printf("Client %d could not start account: %s. Account does not exist.\n", conn->pid, acctName);
                n = write(conn->sock, "Error: could not start account. Account does not exist.\n", 100);
            }else{
                printf("Client %d could not start account: %s. Account is in session.\n", conn->pid, acctName);
                n = write(conn->sock, "Error: could not start account. Account is in session.\n", 100);
                
            }
            
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else if(strncasecmp(buffer, "Credit", 6) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Credit",6);
            
            if(conn->session == -1){
                printf("Client %d tried to credit account, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer + 6;
            
            sscanf(buf, "%lf", &amount);
            
            creditAccount(conn->session, amount);
            
            printf("Client %d deposited $%.2f\n", conn->sock, amount);
            n = write(conn->sock, "Credited account.\n", 30);
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else if(strncasecmp(buffer, "Debit", 5) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Debit",5);
            
            if(conn->session == -1){
                printf("Client %d tried to debit account, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer + 5;
            
            sscanf(buf, "%lf", &amount);
            
            x = debitAccount(conn->session, amount);
            
            if(x == 0){
                printf("Client %d withdrew $%.2f\n", conn->sock, amount);
                n = write(conn->sock, "Debited account.\n", 30);
            }else{
                printf("Client %d tried to debit account, but does not have enough funds.\n", conn->sock, amount);
                n = write(conn->sock, "Not enough funds.\n", 30);
            }
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
            
        } else if(strncasecmp(buffer, "Balance", 7) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Balance",7);
            
            if(conn->session == -1){
                printf("Client %d requested balance, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer;
            
            index = conn->session;
            
            //printf("%d\n", index);
            
            balance = bank->accounts[index].balance;
            
            printf("Client %d requested balance.\n", conn->sock);
            
            printf("Printed balance of %.2f\n", balance);
            
            sprintf(buf, "Balance for account %s is $%.2f\n", &(bank->accounts[conn->session].name), &balance);
            
            n = write(conn->sock, buf, 100);
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else if(strncasecmp(buffer, "Finish", 6) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Finish",6);
            
            if(conn->session == -1){
                printf("Client %d tried to finish session, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            bank->accounts[conn->session].session = -1;
            
            conn->session = -1;
            
            
            printf("Client %d finished session.\n", conn->sock);
            n = write(conn->sock, "Finished session\n", 30);
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else {
            printf("Invalid command\n");
            n = write(conn->sock,"Invalid commmand", 16);
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        }
    }
}