Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <pthread.h>
  9. //#include "list.h"
  10.  
  11. #define port_1 htons(5577);
  12. #define port_2 htons(1234);
  13. int sockfd_me;
  14. struct sockaddr_in meAddr;
  15. char buffer_me[1024];
  16. int me_addr_size;
  17.  
  18. int sockfd_other;
  19. struct sockaddr_in otherAddr;
  20. char buffer_other[1024];
  21. int other_addr_size;
  22.  
  23.  
  24. char str_send[20];
  25. int addr_size;
  26.  
  27. pthread_mutex_t mutex;
  28. pthread_cond_t cond;
  29.  
  30. pthread_mutex_t mutex2;
  31. pthread_cond_t cond2;
  32.  
  33. pthread_mutex_t mutex3;
  34. pthread_cond_t cond3;
  35.  
  36. void* get_input()
  37. {
  38. pthread_mutex_lock(&mutex2);
  39. pthread_mutex_lock(&mutex);
  40. while(buffer_other[1]==0){
  41.  
  42. pthread_cond_wait(&cond, &mutex);
  43. printf("enter: ");
  44. scanf("%[^\n]", str_send);
  45. pthread_mutex_lock(&mutex3);
  46. if(str_send[1]!=0)
  47. {
  48. pthread_mutex_unlock(&mutex3);
  49. pthread_cond_signal(&cond3);
  50. }
  51.  
  52. }
  53.  
  54. pthread_mutex_unlock(&mutex2);
  55. pthread_cond_signal(&cond2);
  56.  
  57. pthread_mutex_unlock(&mutex);
  58. pthread_exit(0);
  59.  
  60.  
  61.  
  62. }
  63.  
  64. void* send_msg(void* str_send[])
  65. {
  66. pthread_mutex_lock(&mutex3);
  67. while(str_send[1]==0){
  68. pthread_cond_wait(&cond3, &mutex3);
  69. }
  70. strcpy(buffer_me, str_send);
  71. sendto(sockfd_other, buffer_me, 1024, 0, (struct sockaddr*)&otherAddr, sizeof(otherAddr));
  72. memset(buffer_me, '\0', sizeof(buffer_me));
  73. pthread_mutex_unlock(&mutex3);
  74. pthread_exit(0);
  75. }
  76.  
  77. void* print_msg()
  78. {
  79.  
  80. pthread_mutex_lock(&mutex2);
  81. while(buffer_other[1]==0){
  82. pthread_cond_wait(&cond2, &mutex2);
  83. }
  84. // do what we want
  85. printf("[+]printing from print: %s \n", buffer_other);
  86. memset(buffer_other, '\0', sizeof(buffer_other));
  87.  
  88. pthread_exit(0);
  89. }
  90.  
  91.  
  92.  
  93. void* receive_msg()
  94. {
  95. pthread_mutex_lock(&mutex);
  96.  
  97. //-----code------
  98. addr_size=sizeof(otherAddr);
  99. recvfrom(sockfd_me, buffer_other, 1024, 0, (struct sockaddr*)& otherAddr, &addr_size);
  100. //------------
  101.  
  102.  
  103. if(buffer_other[1]!=0){
  104. pthread_mutex_unlock(&mutex);
  105. pthread_cond_signal(&cond);
  106. }
  107. pthread_exit(0);
  108. }
  109.  
  110.  
  111. void main(){
  112. //the other port
  113. sockfd_other = socket(AF_INET, SOCK_DGRAM, 0);
  114. otherAddr.sin_family = AF_INET;
  115. otherAddr.sin_port = port_2;
  116. otherAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  117.  
  118. //my port
  119. sockfd_me = socket(AF_INET, SOCK_DGRAM, 0);
  120. meAddr.sin_family = AF_INET;
  121. meAddr.sin_port = port_1;
  122. meAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  123.  
  124. bind(sockfd_me, (struct sockaddr*)&meAddr, sizeof(meAddr));
  125.  
  126. //bind(sockfd_receiver, (struct sockaddr*)&receiverAddr, sizeof(receiverAddr));
  127.  
  128.  
  129. //get input thread
  130. pthread_t thread_input;
  131. pthread_attr_t attr;
  132. pthread_attr_init(&attr);
  133. pthread_create(&thread_input, &attr, &get_input, NULL);
  134.  
  135.  
  136. //send message thread
  137. pthread_t thread_send;
  138. pthread_attr_t attr1;
  139. pthread_attr_init(&attr1);
  140. pthread_create(&thread_send, &attr1, &send_msg, &str_send);
  141.  
  142.  
  143. //receive message thread
  144. pthread_t thread_receive;
  145. pthread_attr_t attr3;
  146. pthread_attr_init(&attr3);
  147. pthread_create(&thread_receive, &attr3, &receive_msg, NULL);
  148.  
  149.  
  150. //print thread
  151. pthread_t thread_print;
  152. pthread_attr_t attr2;
  153. pthread_attr_init(&attr2);
  154. pthread_create(&thread_print, &attr2, &print_msg, NULL);
  155.  
  156.  
  157. pthread_join(thread_receive,NULL);
  158. pthread_join(thread_input,NULL);
  159. pthread_join(thread_send,NULL);
  160. pthread_join(thread_print,NULL);
  161.  
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement