Guest User

Untitled

a guest
Mar 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. # include<sys/socket.h>
  2. # include<sys/time.h>
  3. # include<arpa/inet.h>
  4. # include<string.h>
  5. # include<stdio.h>
  6. # define BUFFERSIZE 1024
  7. # define SERVERADDR "127.0.0.1"
  8. int main ()
  9. {
  10. int sock1, sock2,pid,b;
  11. int node, PORT1, PORT2;
  12. int bind_status , bytes_received , bytes_sent ;
  13. char send_buf[BUFFERSIZE], recv_buf[BUFFERSIZE] ;
  14. struct sockaddr_in s_server1,s_client1,s_server2,s_client2 ;
  15. int si_len1 = sizeof (s_client1) ;
  16. int si_len2 = sizeof (s_client2) ;
  17. struct timeval tv1,tv2;
  18. double time_diff;
  19. printf("Enter the node terminal:- A-1,B-2,C-3,D-4 n");
  20. scanf("%d",&node);
  21. //printf("%d", node);
  22.  
  23.  
  24. if (node == 1)
  25. {
  26. PORT1 = 5000; //sending port
  27. PORT2 = 5003; //receiving port
  28. }
  29. if (node == 2)
  30. {
  31. PORT1 = 5001;
  32. PORT2 = 5000;
  33. }
  34. if (node == 3)
  35. {
  36. PORT1 = 5002;
  37. PORT2 = 5001;
  38. }
  39. if (node == 4)
  40. {
  41. PORT1 = 5003;
  42. PORT2 = 5002;
  43. }
  44. // Creating the socket
  45. sock1 = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) ;
  46. if (sock1 < 0)
  47. {
  48. printf("Socket creation failed. n");
  49. }
  50. else
  51. {
  52. printf ("Socket created with descriptor %dn ", sock1 ) ;
  53. }
  54. sock2 = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) ;
  55. if (sock2 < 0)
  56. {
  57. printf("Socket creation failed. n");
  58. }
  59. else
  60. {
  61. printf ("Socket created with descriptor %dn ", sock2 ) ;
  62. }
  63. // Initializing and binding the socket
  64. s_server1 .sin_family= AF_INET ;
  65. s_server1 .sin_port=htons(PORT2);
  66. s_server1 .sin_addr.s_addr=htonl(INADDR_ANY);
  67.  
  68. bind_status =bind(sock1,(struct sockaddr*)&s_server1,sizeof (s_server1));
  69.  
  70. if(bind_status< 0)
  71. {
  72. printf(" Binding socket failed. n" ) ;
  73. }
  74.  
  75. else
  76. {
  77. printf("Binding socket successful. n") ;
  78.  
  79. }
  80. if(!inet_aton(SERVERADDR, &s_server1.sin_addr))
  81. {
  82. printf("IP network format conversion failed");
  83. return 1;
  84. }
  85.  
  86. else{
  87. printf("IP network format conversion successful.n");
  88. }
  89. s_server2.sin_family =AF_INET;
  90. s_server2.sin_port =htons(PORT1);
  91. printf("port %d assigned for sending n",PORT1);
  92.  
  93.  
  94.  
  95. printf(" Are you initiating the conversation? If Yes: 1, no: 0n");
  96. scanf("%d", &b);
  97. printf(" b is %dn", b);
  98. //while(1)
  99. //{
  100. if (b==1)
  101. {
  102. printf("Message to send: ");
  103. scanf("%s",send_buf);
  104. sprintf(send_buf, "%dhello", node);
  105. printf("%sn", send_buf);
  106. strcpy (recv_buf,send_buf) ;
  107. //}
  108.  
  109.  
  110.  
  111. // pid = fork();
  112. //if (pid == 0)
  113. //{
  114.  
  115. // Server wait for a packet
  116. //if (b==1)
  117. //{
  118. bytes_sent=sendto(sock2,send_buf, sizeof(send_buf),0,
  119. (struct sockaddr*)&s_server2, si_len2);
  120. gettimeofday(&tv1,NULL);
  121. printf ( "%d bytes of data sent : %s n " , bytes_sent , send_buf) ;
  122. //strcpy (recv_buf,send_buf) ;
  123. printf("recv_buff is %sn",recv_buf);
  124. bytes_received=recvfrom(sock1,recv_buf,sizeof(recv_buf),0,(struct
  125. sockaddr*)&s_server1,&si_len1);
  126. gettimeofday(&tv2,NULL);
  127. printf("hello");
  128. printf( "%d bytes of data received : %sn", bytes_received,recv_buf ) ;
  129. time_diff=(tv2.tv_sec*1000000+tv2.tv_usec)-
  130. (tv1.tv_sec*1000000+tv1.tv_usec);
  131. time_diff/=1000;
  132. printf("Time difference=%lfn",time_diff);
  133. }
  134. else
  135. {
  136. printf("waiting for messagesn");
  137.  
  138. //fflush(stdout);
  139. bytes_received=recvfrom(sock1,recv_buf,sizeof(recv_buf),0,(struct
  140. sockaddr*)&s_server1,&si_len1);
  141. printf( "1024 bytes of data received : %sn", recv_buf ) ;
  142. fflush(stdout);
  143. strcpy (send_buf,recv_buf) ;
  144. bytes_sent=sendto(sock2,send_buf, sizeof(send_buf),0,(struct
  145. sockaddr*)&s_server2, si_len2);
  146. printf ( "1024 bytes of data sent : %s n " , send_buf) ;
  147. fflush(stdout);
  148. }
  149. //}
  150.  
  151. //}
  152. return 0 ;
  153. }
Add Comment
Please, Sign In to add comment