cliente.c
Código:
Aparentemente, el cliente hace su sendto() correctamente, sin embargo, en el servidor..
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <linux/if_ether.h>
int main (int argc, char *argv[]){
int sock_cli, longitud, n;
struct sockaddr_in dir_serv;
sock_cli = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sock_cli < 0){
printf("Error en la funcion socket() \n");
exit(1);
}
bzero(&dir_serv, sizeof(dir_serv));
dir_serv.sin_family = AF_INET;
dir_serv.sin_port=htons(4000);
dir_serv.sin_addr.s_addr=htonl(INADDR_ANY);
longitud = sizeof (dir_serv);
n = sendto(sock_cli, "prueba raw", 256, 0, (struct sockaddr *) &dir_serv, sizeof(dir_serv));
if (n == -1){
printf("Error en sendto() \n");
exit(1);
}
printf("Valor de n: %d \n", n);
close(sock_cli);
exit(0);
}
servidor.c
Código:
El programa se queda esperando en recvfrom() algo que nunca le llega.#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <string.h>
int main (int argc, char *argv[]){
int sock_serv, sock_cli, rbind, n;
socklen_t longitud;
struct sockaddr_in dir_serv, cliaddr;
char buff[256];
sock_serv = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sock_serv < 0){
printf("Error en la funcion socket() \n");
exit(1);
}
bzero(&dir_serv, sizeof(dir_serv));
dir_serv.sin_family = AF_INET;
dir_serv.sin_port=htons(4000);
dir_serv.sin_addr.s_addr=htonl(INADDR_ANY);
longitud = sizeof(dir_serv);
rbind = bind(sock_serv, (struct sockaddr *) &dir_serv, longitud);
if (rbind == -1){
printf("Error en la funcion bind() \n");
exit(1);
}
printf("Voy a ejecutar recvfrom() \n");
n = recvfrom(sock_serv, buff, 256,0,(struct sockaddr *) &cliaddr, &longitud);
if (n > 0){
printf("He recibido algo\n");
}
printf("Valor de n: %d\n", n);
close (sock_serv);
exit(1);
}
Aparte de que las pruebas las hago en el propio PC (para que no falle por causas externas), he probado a cambiar la signatura de la función socket por la de UDP, ya que el código serviría "socket (AF_INET, SOCK_DGRAM, 0)" y me he encontrado con que la comunicación se produce correctamente y se recibe en el servidor. Sin embargo en RAW me falla, ¿alguien me echa una mano?
Gracias y saludos!!


