Advertisement
Guest User

Untitled

a guest
May 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <unistd.h>
  9. #include <iostream>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>
  12. #include <sys/time.h>
  13. #include <fcntl.h>
  14. #include "../common/messageType.h"
  15. #include "../common/message.h"
  16. #include "MessageHandler.h"
  17. #include "../http_module/HttpHandler.h"
  18.  
  19.  
  20. #define DET_NMB 3
  21. #define MAX_SOCK 20
  22. int main(int argc, char *argv[])
  23. {
  24. if(argc<1)
  25. {
  26. printf("Usage:\n ./MANAGER <DETECTOR_IP>\n");
  27. return 0;
  28. }
  29. /* Variables */
  30. fd_set readfds;
  31. FD_ZERO (&readfds);
  32.  
  33. int sock[20];
  34. struct hostent *hp;
  35. struct message *msg;
  36. struct timeval times;
  37. times.tv_sec=0;
  38. times.tv_usec=10000;
  39. msg = (struct message*)malloc(sizeof(struct message));
  40. MessageHandler handler;
  41.  
  42. struct sockaddr_in servers [20];
  43. int ip[] = {1,0,168,192};
  44. char buf[16];
  45.  
  46. /* Create http handler thread */
  47. pthread_t id;
  48. int err;
  49. err = pthread_create(&(id), NULL, &httpHandlerStart, NULL);
  50. if (err != 0)
  51. perror("Can't create http thread\n");
  52. else
  53. printf("Http thread created successfully\n");
  54.  
  55. /* Create socket*/
  56. int servcount = 0;
  57. int i=0;
  58. for(i=1; i<255; i++)
  59. {
  60. fd_set fds_connect;
  61. FD_ZERO (&fds_connect);
  62. ip[0] = i;
  63. //printf("Client started\n");
  64. sock[servcount]=socket(AF_INET, SOCK_STREAM, 0);
  65. fcntl(sock[servcount], F_SETFL, O_NONBLOCK);
  66. if(sock[servcount]<0)
  67. {
  68. //perror("Failed to create socket.\n");
  69. close(sock[servcount]);
  70. continue;
  71. }
  72. //printf("Socket created.\n");
  73. servers[servcount].sin_family = AF_INET;
  74. snprintf(buf, 16, "%d.%d.%d.%d", ip[3],ip[2],ip[1],ip[0]);
  75. hp= gethostbyname (buf);
  76. if(hp == 0)
  77. {
  78. //perror("GetHostName failed. \n");
  79. close(sock[servcount]);
  80. continue;
  81. }
  82. // printf("Hostname received.\n");
  83.  
  84. memcpy(&servers[servcount].sin_addr, hp->h_addr, hp->h_length);
  85. servers[servcount].sin_port = 2137;
  86. printf("Address: %s\n",inet_ntoa(servers[servcount].sin_addr));
  87. int ret = connect(sock[servcount], (struct sockaddr*) &servers[servcount], sizeof (servers[servcount]));
  88. fd_set fd;
  89. FD_ZERO(&fd);
  90. FD_SET(sock[servcount],&fd);
  91. ret = select(FD_SETSIZE, NULL, &fd, NULL, &times);
  92. times.tv_usec=10000;
  93. if(ret <1)
  94. {
  95. printf("Connect failed!\n");
  96. close(sock[servcount]);
  97. continue;
  98. }
  99. printf("Successfully connected to: %s\n",hp->h_name);
  100. servcount++;
  101. }
  102. int end = 0;
  103. int rval;
  104. int j=0;
  105. for(j=0; j< servcount; j++)
  106. {
  107. FD_SET(sock[j],&readfds);
  108. }
  109. sleep(1);
  110. while(end != 1)
  111. {
  112.  
  113. i = 0;
  114. select(FD_SETSIZE, &readfds,NULL,NULL,NULL);
  115. for (i=0; i<servcount; i++)
  116. {
  117. if(sock[i]!=-1 && FD_ISSET(sock[i],&readfds))
  118. {
  119. if((rval=recv(sock[i], msg, sizeof(struct message), 0)) <0) {
  120. perror("reading stream message error");
  121. FD_CLR(sock[i],&readfds);
  122. sock[i]=-1;
  123. }
  124. else if(rval==0)
  125. {
  126. printf("Ending connection\n");
  127. end=1;
  128. }
  129. else {
  130. handler.handle(msg);
  131. }
  132. }
  133. }
  134. }
  135. for (i=0;i<servcount;i++)
  136. {
  137. close(sock[i]);
  138. }
  139. free(msg);
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement