Send message confirmation received UDP

Asked

Viewed 139 times

5

Good afternoon, I have the code for Udplisten, which causes PC1 to send a message to PC3 via an interlocutor (PC2), however, after the message reaches PC3, it has to send to PC1 to confirm that it has received the message, but I do not understand how to do that part. Here is Udplisten’s code

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
/*
 Aguarda por datagramas UDP no porto especificado
 Digitar <Ctl><c> para abandonar
 */
#define exit_on_error(s,m) if (s < 0) { perror(m); exit(1); }
#define MAX_BUFFER 512

int main(int argc, char* argv[]){
 int sockfd, size_addr;
 struct sockaddr_in remote_addr;
 struct sockaddr_in local_addr;
 char ip_s[INET_ADDRSTRLEN]; //IP em dotted decimal notation
 int bytes,r,v=1;
 char buf[MAX_BUFFER];

 //verifica argumentos
 if (argc != 2) {
  printf("USAGE: %s <porto>\n",argv[0]);
  exit(1);}

  for(i=0; i < strlen(agrv[1]); i++) //IP argv[1]
  {
   if(!isdigit(argv[1][i])) //IP argv[1]
   {
       printf("<porto> -> Apenas numeros inteiros!\n\n\n");
       exit(1);
   }
  }
  //cria socket UDP
 sockfd = socket(AF_INET,SOCK_DGRAM,0);
 exit_on_error(sockfd,"Error:socket()");
 //permite broadcast
 r=setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &v, sizeof(v));
 exit_on_error(r,"Error:setsockopt()");

 //regista (associa) socket localmente (IP+porto)
 //necessarias permissões de root
 bzero((char *)&local_addr, sizeof(local_addr));//Coloca a zero toda a estrutura
 local_addr.sin_family = AF_INET;
 local_addr.sin_addr.s_addr = htonl(INADDR_ANY); //IP Local
 local_addr.sin_port = htons(atoi(argv[1])); //Porto
 r=bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
 exit_on_error(r,"Error:bind()");

 //limpa var remote_addr
 bzero((char *)&remote_addr, sizeof(remote_addr));

 // aguarda por datagrama UDP
 while (1){
  printf("Aguarda UDP no porto %d\n",atoi(argv[1])); 
  size_addr=sizeof(remote_addr);
  bytes = recvfrom(sockfd, buf, MAX_BUFFER-1, 0, (struct sockaddr *)&remote_addr, &size_addr);
  // escreve origem e dados recebidos
  inet_ntop(AF_INET,&(remote_addr.sin_addr),ip_s, sizeof ip_s);
  printf("Recebido de: %s\n",ip_s);
  printf("Porto: %d\n",ntohs(remote_addr.sin_port));
  printf("Bytes lidos: %d\n", bytes);
  buf[bytes]=0; //Null terminated string
  printf("Dados: %s\n", buf);
 }
 //fecha socket
 close(sockfd);
}

Code of PC2, Udpgw:

#include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <string.h>
 #define exit_on_error(s,m) if (s < 0) { perror(m); exit(1); }
 #define MAX_BUFFER 512

 int main(int argc, char* argv[])
 {
    int sock, length, fromlen, r, send_sock, sendlen, V = 1, n; //int n declarado
    struct sockaddr_in SERVER_ADDR;
    struct sockaddr_in FROM_ADDR;
    struct sockaddr_in SENDTO_ADDR;
    //struct sockaddr_in LOCALMENTE_addr;
    char buf[MAX_BUFFER];

    //verifica argumentos
    if (argc != 4)
    {
        printf("USAGE: %s <port_2> <ip_broadcast> <port_3/4>\n",argv[0]);
        exit(1);
    }
    //cria socket UDP
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock < 0) printf("SOCKET");
    length = sizeof(SERVER_ADDR);
    bzero(&SERVER_ADDR, length);
    SERVER_ADDR.sin_family = AF_INET;
    SERVER_ADDR.sin_addr.s_addr = INADDR_ANY;
    SERVER_ADDR.sin_port = htons(atoi(argv[1]));

    setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &V, sizeof(V));
    sendlen = sizeof(SENDTO_ADDR);
    bzero(&SENDTO_ADDR, sendlen);
    SENDTO_ADDR.sin_family = AF_INET;
    SENDTO_ADDR.sin_addr.s_addr = inet_addr(argv[2]);
    SENDTO_ADDR.sin_port = htons(atoi(argv[3]));

    if(bind(sock, (struct sockaddr *)&SERVER_ADDR,length) < 0) printf("BIND:  ");

    fromlen = sizeof(struct sockaddr_in);

    while(1)
    {
            printf("Wait for the next message...!\n");
            bzero(&buf, MAX_BUFFER);
            n = recvfrom(sock,buf,MAX_BUFFER,0,(struct sockaddr *) &FROM_ADDR, &fromlen);

         if (n < 0) printf("RECVFROM\n");
             printf("Mensagem recebida: [%s], bytes recebidos: [%d]\n",buf,n);

         n = sendto (sock, buf,MAX_BUFFER,0,(struct sockaddr *) &SENDTO_ADDR, sendlen);
             if (n < 0) printf("SENDTO\n");
    }
 }

Can someone help me?

2 answers

3

In your distributed system both the PC1 node and the PC2 and PC3 nodes should be able to send and receive UDP packets.

For now, the only node that is capable of both receiving and receiving a UDP package is node PC2, which I believe is running the Udpbw program.

In this case, the solution for me is simple: make an adaptation of the program that is running on PC2 so that it can work on PC1 and PC3.

This task would be easier for the program to be organized in functions. Ideally, do not put the entire program in the function main. The program should be modularized using functions. Creating a function enviarPacote() and a function receberPacote(), for example.

As the program that runs on PC2 already has the function of receiving and sending packages, just change the order and the sending and receiving parameters adapting its operation to the other nodes.

  • My doubt is even how to make the code for it to send the confirmation that it received the message. I will put the code of PC2, Udpgw

  • Just change the next to last line, where you have n = path... to send the package back to the source. Change the destination address.

  • Change to this? n= sendto (Sock, buf,MAX_BUFFER,0,(struct sockaddr *) &FROM_ADDR, &fromlen);

  • Yes, exactly.

3


That code isn’t quite right. Only PC2 is sending/receiving packages. From what I understand, the message has to be sent by Unicast, not Broadcast. I have a similar code (not quite what you need). I did in college while I had a Networking chair. If you intend, send me message that I will provide you the code and explain what it does and what you should change to get what you intend. I did UDP with broadcast, but easily you can change to Unicast

  • Thank you very much.

  • 1

    I’ll be waiting for contact if you still want the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.