Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: ankora45 en 12 Octubre 2012, 11:26 am



Título: ayuda con sockets
Publicado por: ankora45 en 12 Octubre 2012, 11:26 am
hola veran hice un programa que te pide la web y un puerto y te saca por pantalla la ip(v4) del servidor donde este alojada la pagina sin embargo cuando lo inicio me pide la web y el puerto hasta ahi todo bien pero despues me sale el siguiente error "core dump"  el compilador ni me da problemas ni alertas ademas trabajo en ubuntu ayudenme plz
Código
  1. #include <sys/socket.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  15.  int stat = fcntl(sockfd, F_GETFL, 0);
  16.  char web[20];
  17.  int puertosd;
  18.      printf("Escriba el nombre de la web (www.nombre.extension)\n");
  19.      fgets(web, 20, stdin );
  20.      printf("Escriba el puero\n");
  21.      scanf("%d",&puertosd);
  22.      fcntl(sockfd, F_SETFL, stat | O_NONBLOCK);
  23.      struct hostent *host = gethostbyname(web);
  24.      struct sockaddr_in sock;
  25.      sock.sin_family = AF_INET;
  26.      sock.sin_port = htons(puertosd);
  27.      sock.sin_addr.s_addr = inet_addr(host->h_addr);
  28.      connect(sockfd, (struct sockaddr*) &sock, sizeof(sock));
  29.      printf("Nombre: %s\n", host->h_name);
  30.      printf("Direccion IP(v4) del servidor: %s\n", inet_ntoa(*((struct in_addr *)host->h_addr)));
  31.      close(sockfd);
  32.  
  33.  
  34.  
  35. return 0;
  36. }


Título: Re: ayuda con sockets
Publicado por: soyloqbuskas en 12 Octubre 2012, 11:55 am
ˇBuenas ankora45!

El problema lo tienes en esta linea:
Código
  1. sock.sin_addr.s_addr = inet_addr(host->h_addr);

Mira este enlace que hablan sobre tu problema
http://foro.elhacker.net/programacion_cc/asignar_ip_a_una_sockaddrin-t217251.0.html (http://foro.elhacker.net/programacion_cc/asignar_ip_a_una_sockaddrin-t217251.0.html)

Un saludo.


Título: Re: ayuda con sockets
Publicado por: ankora45 en 12 Octubre 2012, 12:10 pm
Gracias por la ayuda pero sigo sin entenderlo ya que corrijo la linea pero me sigue saltando incluso no me deja compilar


Título: Re: ayuda con sockets
Publicado por: soyloqbuskas en 12 Octubre 2012, 15:48 pm
Bueno creo que ya he dado con el problema...

En realidad esta en la funcion htons(). La funcion htons(u_16) recibe un numero de 16 bits, pero tu le estas pasando un int puertosd que es de 32 bits, por eso te da fallo de segmentacion. Para solucionarlo debes declararte el puertosd como unsigned short puertosd. Y luego ten cuidado con el scanf(puertosd) porque ya no puedes usar %d para leer creo que debes hacerlo asi scanf("%hd", &puertosd);

Un saludo.


Título: Re: ayuda con sockets
Publicado por: ankora45 en 13 Octubre 2012, 14:02 pm
Me sigue saltando el error segment faul (core dumped)
Código
  1. #include <sys/socket.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  15.  int stat = fcntl(sockfd, F_GETFL, 0);
  16.  char web[20];
  17.  unsigned short int puertosd;
  18.      printf("Escriba el nombre de la web (www.nombre.extension)\n");
  19.      fgets(web, 20, stdin );
  20.      printf("Escriba el puerto\n");
  21.      scanf("%hd",&puertosd);
  22.      fcntl(sockfd, F_SETFL, stat | O_NONBLOCK);
  23.      struct hostent *host = gethostbyname(web);
  24.      struct sockaddr_in sock;
  25.      sock.sin_family = AF_INET;
  26.      sock.sin_port = htons(puertosd);
  27.      sock.sin_addr.s_addr = inet_addr(host->h_addr);
  28.      connect(sockfd, (struct sockaddr*) &sock, sizeof(sock));
  29.      printf("Nombre: %s\n", host->h_name);
  30.      printf("Direccion IP(v4) del servidor: %s\n", inet_ntoa(*((struct in_addr *)host->h_addr)));
  31.      close(sockfd);
  32.  
  33.  
  34.  
  35. return 0;
  36. }