mbah_bejo

clientgerry.c

Jun 17th, 2021
1,939
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <stdlib.h>
  4. #include <netinet/in.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <arpa/inet.h>
  8. #define PORT 8080
  9.  
  10. int createSocket(){
  11.     int network_socket;
  12.     if ((network_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  13.         printf("\n Socket creation error \n");
  14.         return -1;
  15.     }
  16.     return network_socket;
  17. }
  18.  
  19. void sqlCmdCreate(int network_socket){
  20.     char package[300]={0};
  21.     char command[20]={0};
  22.     scanf(" %s", command);
  23.  
  24.     if(strcmp(command, "DATABASE") == 0){
  25.         char dbName[255]={0};
  26.         scanf(" %s", dbName);
  27.  
  28.         strcpy(package, "create_db ");
  29.         strcat(package, dbName);
  30.  
  31.  
  32.         send(network_socket , package , strlen(package) , 0);
  33.     }
  34. }
  35.  
  36. int main(int argc, char const *argv[]) {
  37.  
  38.     /*SOCKET CONFIGURATION*/
  39.     struct sockaddr_in address;
  40.  
  41.     int network_socket = createSocket();
  42.     int valread;
  43.  
  44.     struct sockaddr_in serv_addr;
  45.  
  46.     memset(&serv_addr, '0', sizeof(serv_addr));
  47.  
  48.     serv_addr.sin_family = AF_INET;
  49.     serv_addr.sin_port = htons(PORT);
  50.  
  51.     if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) {
  52.         printf("\nInvalid address/ Address not supported \n");
  53.         return -1;
  54.     }
  55.  
  56.     if (connect(network_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  57.         printf("\nConnection Failed \n");
  58.         return -1;
  59.     }
  60.  
  61.     /*SOCKET CONFIGURATION END*/
  62.  
  63.     while(1){
  64.         char command[20]={0};
  65.         command[0] = '\0';
  66.         scanf(" %s", command);
  67.  
  68.         if(strcmp(command, "CREATE") == 0){
  69.             sqlCmdCreate(network_socket);
  70.             continue;
  71.         }
  72.  
  73.         send(network_socket , "failed" , strlen("failed") , 0);
  74.     }
  75.  
  76.     /*send(network_socket , hello , strlen(hello) , 0 );*/
  77.     /*printf("Hello message sent\n");*/
  78.     /*valread = read( network_socket , buffer, 1024);*/
  79.     /*printf("%s\n",buffer );*/
  80.  
  81.     return 0;
  82. }
  83.  
RAW Paste Data