Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include<sys/socket.h>
- # include<sys/time.h>
- # include<arpa/inet.h>
- # include<string.h>
- # include<stdio.h>
- # define BUFFERSIZE 1024
- # define SERVERADDR "127.0.0.1"
- int main ()
- {
- int sock1, sock2,pid,b;
- int node, PORT1, PORT2;
- int bind_status , bytes_received , bytes_sent ;
- char send_buf[BUFFERSIZE], recv_buf[BUFFERSIZE] ;
- struct sockaddr_in s_server1,s_client1,s_server2,s_client2 ;
- int si_len1 = sizeof (s_client1) ;
- int si_len2 = sizeof (s_client2) ;
- struct timeval tv1,tv2;
- double time_diff;
- printf("Enter the node terminal:- A-1,B-2,C-3,D-4 n");
- scanf("%d",&node);
- //printf("%d", node);
- if (node == 1)
- {
- PORT1 = 5000; //sending port
- PORT2 = 5003; //receiving port
- }
- if (node == 2)
- {
- PORT1 = 5001;
- PORT2 = 5000;
- }
- if (node == 3)
- {
- PORT1 = 5002;
- PORT2 = 5001;
- }
- if (node == 4)
- {
- PORT1 = 5003;
- PORT2 = 5002;
- }
- // Creating the socket
- sock1 = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) ;
- if (sock1 < 0)
- {
- printf("Socket creation failed. n");
- }
- else
- {
- printf ("Socket created with descriptor %dn ", sock1 ) ;
- }
- sock2 = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) ;
- if (sock2 < 0)
- {
- printf("Socket creation failed. n");
- }
- else
- {
- printf ("Socket created with descriptor %dn ", sock2 ) ;
- }
- // Initializing and binding the socket
- s_server1 .sin_family= AF_INET ;
- s_server1 .sin_port=htons(PORT2);
- s_server1 .sin_addr.s_addr=htonl(INADDR_ANY);
- bind_status =bind(sock1,(struct sockaddr*)&s_server1,sizeof (s_server1));
- if(bind_status< 0)
- {
- printf(" Binding socket failed. n" ) ;
- }
- else
- {
- printf("Binding socket successful. n") ;
- }
- if(!inet_aton(SERVERADDR, &s_server1.sin_addr))
- {
- printf("IP network format conversion failed");
- return 1;
- }
- else{
- printf("IP network format conversion successful.n");
- }
- s_server2.sin_family =AF_INET;
- s_server2.sin_port =htons(PORT1);
- printf("port %d assigned for sending n",PORT1);
- printf(" Are you initiating the conversation? If Yes: 1, no: 0n");
- scanf("%d", &b);
- printf(" b is %dn", b);
- //while(1)
- //{
- if (b==1)
- {
- printf("Message to send: ");
- scanf("%s",send_buf);
- sprintf(send_buf, "%dhello", node);
- printf("%sn", send_buf);
- strcpy (recv_buf,send_buf) ;
- //}
- // pid = fork();
- //if (pid == 0)
- //{
- // Server wait for a packet
- //if (b==1)
- //{
- bytes_sent=sendto(sock2,send_buf, sizeof(send_buf),0,
- (struct sockaddr*)&s_server2, si_len2);
- gettimeofday(&tv1,NULL);
- printf ( "%d bytes of data sent : %s n " , bytes_sent , send_buf) ;
- //strcpy (recv_buf,send_buf) ;
- printf("recv_buff is %sn",recv_buf);
- bytes_received=recvfrom(sock1,recv_buf,sizeof(recv_buf),0,(struct
- sockaddr*)&s_server1,&si_len1);
- gettimeofday(&tv2,NULL);
- printf("hello");
- printf( "%d bytes of data received : %sn", bytes_received,recv_buf ) ;
- time_diff=(tv2.tv_sec*1000000+tv2.tv_usec)-
- (tv1.tv_sec*1000000+tv1.tv_usec);
- time_diff/=1000;
- printf("Time difference=%lfn",time_diff);
- }
- else
- {
- printf("waiting for messagesn");
- //fflush(stdout);
- bytes_received=recvfrom(sock1,recv_buf,sizeof(recv_buf),0,(struct
- sockaddr*)&s_server1,&si_len1);
- printf( "1024 bytes of data received : %sn", recv_buf ) ;
- fflush(stdout);
- strcpy (send_buf,recv_buf) ;
- bytes_sent=sendto(sock2,send_buf, sizeof(send_buf),0,(struct
- sockaddr*)&s_server2, si_len2);
- printf ( "1024 bytes of data sent : %s n " , send_buf) ;
- fflush(stdout);
- }
- //}
- //}
- return 0 ;
- }
Add Comment
Please, Sign In to add comment