Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/resource.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <sys/types.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #include <unistd.h>
  12.  
  13. #include <dirent.h>
  14.  
  15. //OWN LIBRARIES
  16. #include "memory.h"
  17. #include "help.h"
  18. #include "connection.h"
  19.  
  20.  
  21. //CONSTANTS AND ERROR MESSAGES
  22. #define SOCKET 7777
  23. #define MEMORY_CAPACITY 1024
  24. #define ERROR_ARGUMENT "ERROR - incorrect arguments.\n"
  25. #define ERROR_SOCKET "ERROR - Socket couldn\'t be created.\n"
  26. #define ERROR_COMMAND "ERROR - Unknown command.\n"
  27.  
  28. //MEMEROY POINTER
  29. char MEMORY[MEMORY_CAPACITY];
  30.  
  31. //DECLARATION OF USED FUNCTIONS
  32. void info(); //extern function from help.h
  33. void memory_allocation(char mem[MEMORY_CAPACITY]); //exten function from memory.h
  34. void initialization(); //exten function from memory.h
  35. int starting_arguments(char *s, char *t, char *l, char *c, char *d, int argc, char **argv); //function for checking initial arguments for program
  36. int execute_command(char *command); //function for executing commands
  37. void print_command(char *buffer, int who); //print command + source of command (socket or standard input)
  38. void clear_buffer(char *buffer);
  39. void clear_socket(int socket);
  40. void listdir(const char *name, int indent);
  41.  
  42. /*
  43. * 1-help
  44. * 2-halt
  45. * 3-quit
  46. * 4-info
  47. * 5-make*/
  48. void server() {
  49. int newsockfd = connection_server(SOCKET);
  50. char buffer[256];
  51. int n = 0, i,k=1;
  52.  
  53. fd_set master;
  54. fd_set read_fds;
  55.  
  56. FD_ZERO(&master);
  57. FD_ZERO(&read_fds);
  58.  
  59. FD_SET(0, &master);
  60. FD_SET(newsockfd, &master);
  61.  
  62. printf("Waiting for command...\n");
  63. while (1) {
  64. read_fds = master;
  65. //function for checking both inputs (socket and standard input)
  66. if (select(newsockfd + 1, &read_fds, NULL, NULL, NULL) == -1) {
  67. printf("select:");
  68. exit(1);
  69. }
  70. //READING FOM SOCKET
  71. if (FD_ISSET(newsockfd, &read_fds)) {
  72. int value;
  73. n = read(newsockfd, buffer, 256);
  74. printf("%d \n",buffer[0]);
  75. switch(buffer[0]){
  76. case(1):
  77. //help
  78. vypis_help();
  79. break;
  80. case(2):
  81. printf("halt");
  82. break;
  83. case(3):
  84. printf("quit");
  85. break;
  86. case(4):
  87. printf("info");
  88. k*=2;
  89. break;
  90. case(5):
  91. printf("make");
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. int client() {
  100. int sockfd = connection_client(SOCKET), n, i;
  101. char command[20];
  102. char buffer[256];
  103. char position;
  104. FILE *f;
  105.  
  106. //SERVER IS OFFLINE
  107. if (sockfd == -1)
  108. return 0;
  109.  
  110. while (1) {
  111. bzero(command, 20);
  112. printf("Prikaz: ");
  113. scanf("%s", command);
  114. if(strcmp(command,"help")==0) {
  115. command[0] = 1;
  116. command[1] = '\0';
  117. }else{
  118. if(strcmp(command,"halt")==0){
  119. command[0] = 2;
  120. command[1] = '\0';
  121. }else{
  122. if(strcmp(command,"quit")==0){
  123. command[0] = 3;
  124. command[1] = '\0';
  125. }else{
  126. if(strcmp(command,"info")==0){
  127. command[0] = 4;
  128. command[1] = '\0';
  129. }else{
  130. if(strcmp(command,"make")==0) {
  131. command[0] = 5;
  132. command[1] = '\0';
  133. }
  134. }
  135. }
  136. }
  137. }
  138.  
  139. n = write(sockfd, command, strlen(command));
  140. if (n < 0)
  141. printf("ERROR - writing to socket");
  142. if(command[0]==3)
  143. return 0;
  144. }
  145.  
  146. return 0;
  147. }
  148. int main(){
  149. char c;
  150. vypis_help();
  151. scanf("%c",&c);
  152. if(c=='s'){
  153. server();
  154. }
  155. else{
  156. client();
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement