Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<pthread.h>
  5. #include<sys/socket.h>
  6. #include<sys/types.h>
  7. #include<netdb.h>
  8. #include<netinet/in.h>
  9.  
  10. struct socketStructure {
  11.  
  12. int sockfd, newsockfd, portno, clilen, n;
  13. char buffer[256], message[256];
  14. struct sockaddr_in serv_addr, cli_addr;
  15.  
  16.  
  17. };
  18. struct socketStructure sockStructure;
  19.  
  20. void *CreateSocket(void *entrypoint) {
  21.  
  22. sockStructure.sockfd = socket(AF_INET, SOCK_STREAM, 0);
  23. if(sockStructure.sockfd < 0) {
  24.  
  25. perror("Error opening socket...\n");
  26. exit(1);
  27.  
  28.  
  29. }
  30. printf("Socket created sucessfully...\n");
  31.  
  32. }
  33.  
  34. void *CreateSocketStructure(void *entrypoint) {
  35.  
  36. bzero((char *)&sockStructure.serv_addr, sizeof(sockStructure.serv_addr));
  37.  
  38. sockStructure.portno = 8073;
  39. sockStructure.serv_addr.sin_family = AF_INET;
  40. sockStructure.serv_addr.sin_addr.s_addr = INADDR_ANY;
  41. sockStructure.serv_addr.sin_port = htons(sockStructure.portno);
  42.  
  43.  
  44.  
  45. }
  46.  
  47.  
  48. void *bindToAddress(void *entrypoint) {
  49.  
  50. if(bind(sockStructure.sockfd, (struct sockaddr *)&sockStructure.serv_addr, sizeof(sockStructure.serv_addr)) < 0) {
  51.  
  52. perror("ERROR ON Binding!\n");
  53. exit(1);
  54.  
  55. }
  56.  
  57.  
  58. }
  59.  
  60.  
  61. void *listenToClients(void *entrypoint) {
  62.  
  63. listen(sockStructure.sockfd, 5);
  64. sockStructure.clilen = sizeof(sockStructure.cli_addr);
  65.  
  66. }
  67.  
  68. void *ReadAndWriteSocket(void *entrypoint) {
  69.  
  70. if((sockStructure.newsockfd = accept(sockStructure.sockfd, (struct sockaddr *)&sockStructure.cli_addr, &sockStructure.clilen)) < 0) {
  71.  
  72. perror("ERROR on accept...\n");
  73. exit(1);
  74.  
  75. }
  76.  
  77. while(1) {
  78.  
  79. bzero(sockStructure.message, 255);
  80. printf("Cmd >> ");
  81. scanf("%255s", &sockStructure.message);
  82. send(sockStructure.newsockfd, sockStructure.message, strlen(sockStructure.message), 0);
  83.  
  84. bzero(sockStructure.buffer, 256);
  85. sockStructure.n = recv(sockStructure.newsockfd, sockStructure.buffer, 255, 0);
  86. if(sockStructure.n > 0) {
  87.  
  88. printf("Message: %s\nBytes: %d", sockStructure.buffer, sockStructure.n);
  89.  
  90. }
  91. else if(sockStructure.n < 0) {
  92.  
  93. perror("ERROR receiving n bytes...\n");
  94. exit(1);
  95.  
  96. }
  97. else {
  98.  
  99. printf("Client disconnected...\n");
  100. exit(1);
  101.  
  102. }
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109. }
  110.  
  111. int main(int argc, char* arg[]) {
  112.  
  113. pthread_t threadOne;
  114. int threadX = 123;
  115. pthread_t threadTwo;
  116. int threadY = 123;
  117. pthread_t threadThree;
  118. int threadZ = 123;
  119. pthread_t threadFour;
  120. int threadT = 123;
  121. pthread_t threadFive;
  122. int threadL = 123;
  123.  
  124. if(pthread_create(&threadOne, NULL, CreateSocket, &threadX)) {
  125.  
  126. perror("ERROR starting thread...\n");
  127. exit(1);
  128.  
  129. }
  130.  
  131. if(pthread_create(&threadTwo, NULL, CreateSocketStructure, &threadY)) {
  132.  
  133. perror("Error starting thread two...\n");
  134. exit(1);
  135.  
  136. }
  137.  
  138. if(pthread_create(&threadThree, NULL, bindToAddress, &threadZ)) {
  139.  
  140. perror("Error starting thread three...\n");
  141. exit(1);
  142.  
  143. }
  144.  
  145. if(pthread_create(&threadFour, NULL, listenToClients, &threadT)) {
  146.  
  147. perror("Error starting thread four...\n");
  148. exit(1);
  149.  
  150. }
  151.  
  152. if(pthread_create(&threadFive, NULL, ReadAndWriteSocket, &threadL)) {
  153.  
  154. perror("Error starting thread six...\n");
  155. exit(1);
  156.  
  157. }
  158.  
  159. pthread_join(threadOne, NULL);
  160. pthread_join(threadTwo, NULL);
  161. pthread_join(threadThree, NULL);
  162. pthread_join(threadFour, NULL);
  163. pthread_join(threadFive, NULL);
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement