Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/08/2013, 10:16
Avatar de jja
jja
 
Fecha de Ingreso: diciembre-2010
Ubicación: BCN
Mensajes: 47
Antigüedad: 13 años, 4 meses
Puntos: 0
Pregunta Problema con cliente FTP en C

Hola amigos, estoy picandome un cliente ftp en C desde 0, para subir archivos a un FTP server. Llevo el siguiente código de momento, he conseguido autenticarme en el servidor ftp, pero al enviar el fichero archivo.txt, me lo crea vacío en el servidor, y no lo sube y me sale en el servidor como archivo vacio de 0bytes.

Alguien sabria decirme en que estoy fallando?

Código C:
Ver original
  1. #pragma comment(lib, "wsock32.lib")
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <Winsock.h>
  5.  
  6. WSADATA ws;
  7. char buf[10000];
  8. void output(char *str)
  9. {
  10.       FILE *fp = fopen("output.txt", "a+");
  11.       fprintf(fp, "%s\n", str);
  12.       fclose(fp);
  13. }
  14.  
  15. SOCKET ConnectFTP(char* ftpname, int port)
  16. {
  17.       WSAStartup(0x101, &ws);
  18.       // Open up a socket for out TCP/IP session
  19.       SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
  20.       // Set up socket information.
  21.       struct sockaddr_in a = {AF_INET, htons(port)};
  22.       // Get the ip address of our ftp
  23.       struct hostent *h = gethostbyname(ftpname);
  24.       a.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)h->h_addr)));
  25.       // Actually connect to the server
  26.       connect(s, (struct sockaddr *)&a, sizeof(a));
  27.       return s;
  28. }
  29.  
  30. void receiving(SOCKET s, char* string)
  31. {
  32.       char aa[1000] = {'/0'};
  33.       int ii = recv(s, aa, sizeof(aa), 0);
  34.       sprintf(buf, "~%s~", aa);
  35.       output(buf);
  36.       if(string !=0)
  37.             strcpy(string, aa);
  38. }
  39.  
  40. void sending(SOCKET s, char* verb)
  41. {
  42.       strcpy(buf, verb);
  43.       strcat(buf, "\r\n");
  44.       output("Sending: ");
  45.       output(buf);
  46.       send(s, buf, strlen(buf), 0);
  47. }
  48.  
  49. int _stdcall WinMain(HINSTANCE i, HINSTANCE j, char* k, int l)
  50. {
  51.       printf("Arrancando...\n");
  52.       SOCKET s1 = ConnectFTP("ftp.XXXXXXXXXXXXX.com", 21);
  53.       receiving(s1,0);
  54.       printf("Enviando usuario...\n");
  55.       sending(s1, "USER XXXXXX");
  56.       receiving(s1,0);
  57.       printf("Enviando contrasena...\n");
  58.       sending(s1, "PASS XXXYYYZZZ");
  59.       receiving(s1,0);
  60.       sending(s1, "CWD web");
  61.       sending(s1, "PASV");
  62.       char szString[1000];
  63.       receiving(s1, szString);
  64.       printf("Sending ...\n");
  65.       sending(s1, "STOR archivo.txt");
  66.       sending(s1, "QUIT");
  67.       receiving(s1,0);
  68.       return 0;
  69. }


El programa no me devuelve ningún error, a priori parece que funciona bien, aunque no sube el archivo, solo crea uno con el mismo nombre varcío

Gracias de antemano
__________________
El supremo arte de la guerra es someter al enemigo sin luchar.

Sun Tzu