Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <sys/mman.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12.  
  13. #define PORT 29012
  14. #define SIZE (20 * sizeof(int))
  15. #define FILEPATH "clients.bin"
  16.  
  17. struct shared_data {
  18. int data;
  19. int clients[20];
  20. };
  21.  
  22. int main(){
  23.  
  24. int sockfd, ret;
  25. struct sockaddr_in serverAddr;
  26. int newSocket;
  27. struct sockaddr_in newAddr;
  28. socklen_t addr_size;
  29. char buffer[1024];
  30. pid_t childpid;
  31. int fd, result;
  32. int number=-1;
  33. ///////////////
  34. struct shared_data *shared_clients, *child;
  35. const char *shm_name = "projekt";
  36. int shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
  37. int child_fd;
  38. int cli_number = 0;
  39. ftruncate(shm_fd, sizeof(struct shared_data));
  40. shared_clients = mmap(0, sizeof(struct shared_data), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  41. if (shared_clients == MAP_FAILED) {
  42. printf("mmap - failed.\n");
  43. return -1;
  44. }
  45. shared_clients->data = 0;
  46.  
  47. //int *clients = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  48.  
  49. /* wspoldzielony plik z klientami
  50. fd = open(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
  51. if (fd == -1) {
  52. perror("Error opening file for writing");
  53. exit(EXIT_FAILURE);
  54. }
  55. lseek(fd, SIZE-1, SEEK_SET);
  56. result = lseek(fd, SIZE-1, SEEK_SET);
  57. if (result == -1) {
  58. close(fd);
  59. perror("Error calling lseek() to 'stretch' the file");
  60. exit(EXIT_FAILURE);
  61. }
  62. write(fd, "", 1);
  63. result = write(fd, "", 1);
  64. if (result != 1) {
  65. close(fd);
  66. perror("Error writing last byte of the file");
  67. exit(EXIT_FAILURE);
  68. }
  69. clients = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  70. if (clients == MAP_FAILED) {
  71. close(fd);
  72. perror("Error mmapping the file");
  73. exit(EXIT_FAILURE);
  74. }
  75. if (munmap(clients, SIZE) == -1) {
  76. perror("Error un-mmapping the file");
  77. /* Decide here whether to close(fd) and exit() or not. Depends...
  78. }
  79. close(fd); */
  80. ////////////////////////////////////////
  81.  
  82. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  83. if(sockfd < 0){
  84. printf("Error in connection.\n");
  85. exit(1);
  86. }
  87. printf("Server Socket is created.\n");
  88.  
  89. memset(&serverAddr, '\0', sizeof(serverAddr));
  90. serverAddr.sin_family = AF_INET;
  91. serverAddr.sin_port = htons(PORT);
  92. serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  93.  
  94. ret = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
  95. if(ret < 0){
  96. printf("Error in binding.\n");
  97. exit(1);
  98. }
  99. printf("Bind to port %d\n", PORT);
  100.  
  101. if(listen(sockfd, 10) == 0){
  102. printf("Listening....\n");
  103. }else{
  104. printf("Error in binding.\n");
  105. }
  106.  
  107.  
  108. while(1){
  109.  
  110. newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
  111. if(newSocket < 0){
  112. exit(1);
  113. }
  114. cli_number++;
  115. printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
  116. /************************************/
  117. sprintf(shared_clients->clients[cli_number-1], newSocket);
  118. shared_clients->data = cli_number;
  119. /************************************/
  120.  
  121.  
  122.  
  123. // ten kawa\u0142ek zmieni\u0142am //
  124. /*fd = open(FILEPATH, O_RDWR | O_CREAT);
  125. number=number+1;
  126. clients[number] = newSocket;
  127. for (int i=0; i<=number; ++i)
  128. printf("c: %d: %d\n", i, clients[i]);
  129. if (munmap(clients, SIZE) == -1) {
  130. perror("Error un-mmapping the file");
  131. /* Decide here whether to close(fd) and exit() or not. Depends...
  132. }
  133. close(fd); */
  134.  
  135. ////////////////////////////
  136.  
  137.  
  138. if((childpid = fork()) == 0){
  139. close(sockfd);
  140. child_fd = shm_open(shm_name, O_RDONLY, 0666);
  141. child = mmap(0, sizeof(struct shared_data), PROT_READ , MAP_SHARED, child_fd, 0);
  142. if (child == MAP_FAILED) {
  143. printf("mmap - failed (in child).\n");
  144. return -1;
  145. }
  146. int my_clients = child->data;
  147.  
  148. while(1){
  149. recv(newSocket, buffer, 1024, 0);
  150. if(strcmp(buffer, ":exit") == 0){
  151. printf("Disconnected from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
  152. break;
  153. }else{
  154. printf("Client: %s\n", buffer);
  155. printf("Number of logged clients: %s\n", child->data);
  156.  
  157. //// ten kawa\u0142ek zmieni\u0142am ///
  158. /*fd = open(FILEPATH, O_RDONLY);
  159. if (fd == -1) {
  160. perror("Error opening file for reading");
  161. exit(EXIT_FAILURE);
  162. }
  163. clients = mmap(0, SIZE, PROT_READ, MAP_SHARED, fd, 0);
  164. if (clients == MAP_FAILED) {
  165. close(fd);
  166. perror("Error mmapping the file");
  167. exit(EXIT_FAILURE);
  168. }*/
  169. for(int i=0; i < child->data; i=i+1) {
  170. send(child->clients[i], buffer, strlen(buffer), 0);
  171. }
  172. /*
  173. if (munmap(clients, SIZE) == -1) {
  174. perror("Error un-mmapping the file");
  175. }
  176. close(fd);*/
  177. //send(newSocket, buffer, strlen(buffer), 0);
  178. //////////////////////////////
  179. bzero(buffer, sizeof(buffer));
  180. }
  181. }
  182. }
  183. }
  184.  
  185. close(newSocket);
  186.  
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement