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?
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
– IanMoone
Just change the next to last line, where you have n = path... to send the package back to the source. Change the destination address.
– wensiso
Change to this? n= sendto (Sock, buf,MAX_BUFFER,0,(struct sockaddr *) &FROM_ADDR, &fromlen);
– IanMoone
Yes, exactly.
– wensiso