Send UDP Received Message Confirmation


Good afternoon, I have the code for UDPListen, which makes PC1 send a message to PC3 through an interlocutor (PC2) , however, after the message reaches PC3, it has to send it to PC1 to confirm that it received the message , however I don't understand how to do that part. Here is the UDPListen 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]);

  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");
  //cria socket UDP
 sockfd = socket(AF_INET,SOCK_DGRAM,0);
 //permite broadcast
 r=setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &v, sizeof(v));

 //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));

 //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])); 
  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

PC2 code, 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]);
    //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);

            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");

Someone can help me?


This code is not very well done. Only PC2 is sending/receiving packets. From what I understand the message has to be sent by Unicast and not Broadcast. I have similar code (not quite what you need). I did it in college while I had a chair in Networking. If you want, send me a message and I'll give you the code and explain what it does and what you should change to get what you want. I did UDP with broadcast, but you can easily switch to Unicast

Scroll to Top