SunQuest
           FTP Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsSystem AdministrationFTP Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old September 17th, 2003, 04:41 PM
samlab samlab is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 33 samlab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
how to ftp in c program?

ok, if youve read my other post you know im having some problems with my c program running on Sun. so, i thought if i can run my program on windows and still access the files on the Sun machine then id be in buisness. is there a way to ftp a file into your program in C? i know that it is possible in VB. I searched on the web and found nothing.

Reply With Quote
  #2  
Old September 17th, 2003, 04:59 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,775 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 12 m 22 sec
Reputation Power: 420
I take it you mean that you want to be able to read the contents of the file on the Sun into your program on Windows. Shouldn't you be able to make the file's directory on the Sun "shareable" via CIFS (eg, Samba) and then map a Windows drive to it? I've got my Linux box set up for that, as is our network server at work, which is running SunOS.

As for VB, I think you probably were using a control that performed the FTP.

Reply With Quote
  #3  
Old September 18th, 2003, 06:16 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
cURL is a nice little library for handling things like ftp and http from your program. They have a sourceforge project, so search there first.

Pretty simple interface, provided you're cool with using callback functions.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
  #4  
Old September 18th, 2003, 07:46 AM
samlab samlab is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 33 samlab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
Originally posted by ClayDowling
cURL is a nice little library for handling things like ftp and http from your program. They have a sourceforge project, so search there first.

Pretty simple interface, provided you're cool with using callback functions.

i'll give that a look, thanks

Reply With Quote
  #5  
Old September 18th, 2003, 11:13 AM
mschoolbus mschoolbus is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 2 mschoolbus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to mschoolbus
Lightbulb

I managed to act as an FTP client in C just using sockets... Its really not too bad.

Now of course the client I made wasn't compatible with ALL ftp servers I am quite sure, I just needed to get it to connect to a solaris box. But it did work testing on some windows ftp server...

remember to wait for the server to send you ALL of the data it needs to, before your client starts spitting commands at it. Take a look at the return code numbers it sends you also. And one very important thing... Be sure to strcat(ftp_cmd,"\r\n"); before you send it, those are the termination characters...
Code:
int ftpRecvResponse(int sock, char * buf) {
   int i;

   if (recv(sock, buf, 5120, 0) == -1) {//receive the data
      perror("recv");
      return 0;;
   }
   for(i=(strlen(buf) - 1);i>0;i--) {
      if(buf[i]=='.' || buf[i]=='\r') { 
         buf[i+1]='\0';
         break;
      }
   }
   printf("%s\n",buf); //print response to the screen
   return 0;
}

int ftpNewCmd(int sock, char * buf, char * cmd, char * param) {
   strcpy(buf,cmd);
   if(strlen(param) > 0) {
      strcat(buf," ");
      strcat(buf,param);
   }
   strcat(buf,"\r\n");
   printf("*%s",buf); //print the cmd to the screen
   if(send(sock, buf, strlen(buf), 0) == -1) {
      perror("send");
      return 0;
   }
   //clear the buffer
   return 0;
}

int ftpConvertAddy(char * buf, char * hostname, int * port) {
   unsigned int i,t=0;
   int flag=0,decCtr=0,tport1,tport2;
   char tmpPort[6];
   //example data in quotes below:
   //"227 Listening on (149,122,52,162,4,20)"
   //4 * 256 + 20 = 1044
   for(i=0;i<strlen(buf);i++) {
      if(buf[i]=='(') {
         flag = 1;
         i++;
      }
      if(buf[i]==')') {
         hostname[t]='\0';
         break;
      }
      if(flag==1) {
         if(buf[i] != ',') {
            hostname[t]=buf[i];
            t++;
         } else {
            hostname[t]='.';
            t++;
         }
      }
   }
   t=0;
   for(i=0;i<strlen(hostname);i++) {
      if(hostname[i]=='.')
         decCtr++;
      if(decCtr==4 && hostname[i]!='.') {
         tmpPort[t]=hostname[i];
         t++;
         if(hostname[i+1]=='.') {
            tmpPort[t]='\0';
            tport1=atoi(tmpPort);
            t=0;
         }
      }
      if(decCtr==5 && hostname[i]!='.') { 
         tmpPort[t]=hostname[i];
         t++;
         if(hostname[i+1]=='\0') {
            tmpPort[t]='\0';
            tport2=atoi(tmpPort);
            t=0;
         }
      }
   }
   *port = tport1 * 256 + tport2;
   decCtr=0;
   for(i=0;i<strlen(hostname);i++) {
      if(hostname[i]=='.') {
         decCtr++;
      }
      if(decCtr>3)
         hostname[i]='\0';
   }
   return 0;
}

int ftpSendFile(char * buf, char * host, int port) {
   int sd;
   struct sockaddr_in pin;
   struct hostent *hp;

   if ((hp = gethostbyname(host)) == 0) {
      perror("gethostbyname");
      return -1;
   }
   memset(&pin, 0, sizeof(pin));
   pin.sin_family = AF_INET;
   pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
   pin.sin_port = htons(port);
   if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("socket");
      return -1;
   }
   if (connect(sd,(struct sockaddr *)  &pin, sizeof(pin)) == -1) {
      perror("connect");
      return -1;
   }
   if(send(sd, buf, strlen(buf), 0) == -1) {
      perror("send");
      return -1;
   }
   closesocket(sd); //close the socket
   return 0;
}

int ftpProcess(char *hostname, char *username, char *password, char *file, char *filename, char *path) {
   int sd;
   struct sockaddr_in pin;
   struct hostent *hp;
   char tmpHost[100];
   int tmpPort;
   char buf[5120];

   if ((hp = gethostbyname(hostname)) == 0) {
      perror("gethostbyname");
      return -1;
   }

   memset(&pin, 0, sizeof(pin));
   pin.sin_family = AF_INET;
   pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
   pin.sin_port = htons(FTPPORT);

   if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("socket");
      return -1;
   }
   if (connect(sd,(struct sockaddr *)  &pin, sizeof(pin)) == -1) {
      perror("connect");
      return -1;
   }
   if(ftpRecvResponse(sd,buf)==1) {  //wait for ftp server to start talking
    if(strncmp(buf,"220",3)==0) {  //make sure it ends with a 220
     if(ftpNewCmd(sd,buf,"USER",username)==1) {  //issue the command to login
      if(ftpRecvResponse(sd,buf)==1) {  //wait for response
       if(strncmp(buf,"331",3)==0) {  //make sure response is a 331
        if(ftpNewCmd(sd,buf,"PASS",password)==1) {  //send your password
         if(ftpRecvResponse(sd,buf)==1) { //wait for response
          if(strncmp(buf,"230",3)==0) { //make sure its a 230
           if(ftpNewCmd(sd,buf,"PASV","")==1) {  //notify server we want to pass a file
            if(ftpRecvResponse(sd,buf)==1) {  //wait for response
             if(strncmp(buf,"227",3)==0) {  //make sure it starts with a 227
              ftpConvertAddy(buf,tmpHost,&tmpPort); //then convert the return to usable data
              if(ftpNewCmd(sd,buf,"STOR",filename)==1) {  //set the file name AND
               ftpSendFile(file,tmpHost,tmpPort);
               if(ftpRecvResponse(sd,buf)==1) {        //wait for a response
                if(strncmp(buf,"150",3)==0) {  //make sure its a 150 so we know the server got it all
                 if(ftpRecvResponse(sd,buf)==1) {
                  if(strncmp(buf,"226",3)==0) {
                  .......
//close all if()s or do it some better way, close the socket when done.

i know its not the prettiest, but it works quite well. I plan on cleaning it up and making an example to throw out there for those who don't like 3rd party libraries...

PM or email me if you need anything else.

Last edited by mschoolbus : September 18th, 2003 at 02:08 PM.

Reply With Quote
  #6  
Old September 18th, 2003, 01:59 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,775 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 12 m 22 sec
Reputation Power: 420
mschoolbus:
Please edit your post and enclose the code in code tags so that it will be readable.

Code tags are analogous to HTML tags, except that you use square brackets [] instead of "angle brackets" <> . The starting code tag uses the keyword "code" (without the quotation marks) and the ending tag uses "/code" (again without the quotation marks).

Code tags retain your indentation, but does nothing more. The php tags also provides syntax highlight colors similar to many code editors.

For readers of untagged code:
A trick I use to restore the indentation is to click on the quote button, then copy the code in the quote and paste it into an editor. The indentation is there, but the forum just doesn't show it (must be an HTML thing).

Reply With Quote
  #7  
Old September 18th, 2003, 02:02 PM
samlab samlab is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 33 samlab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
thank you for the code msschoolbus. i think it may be a little above my knowledge tho. i couldnt follow it.

Reply With Quote
  #8  
Old September 18th, 2003, 02:26 PM
mschoolbus mschoolbus is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 2 mschoolbus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to mschoolbus
Samlab Sorry you couldn't follow it. I actually plan on cleaning that code up and making a more useful header file.

I sort of write things in sort of an obscure way the first time around, so I will be looking at it when I have a little break from working one of these days... I just sort of went at it with vi and gcc one day, then had to convert it to be used with VC++ for a batch program, with no user input.

As soon as I get something useful together, I will be sure to submit it. Any requests for making it? I know how tough it can be to find decent, simple examples at times. I would gladly offer any help or examples I can.

Anyone feel free to contact me via AIM ("mschoolbus") if you have any questions, need a little help, or are interested in development of a multi-threaded communications server.

Reply With Quote
  #9  
Old September 18th, 2003, 02:29 PM
samlab samlab is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 33 samlab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
when you get get a chance and you get your final code together can you email it to me please. id like to check it out. my email address is samlabarbara@comcast.net. thanks

Reply With Quote
  #10  
Old September 18th, 2003, 03:25 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
i emailed you an interesting program i wrote as well. not exactly an ftp "client" but you'll probably find it helpful.

Reply With Quote
  #11  
Old September 18th, 2003, 09:55 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,829 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 23 h 5 m 8 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
if you search the msdn library for ftp functions you will find several available on the windows platform. They all use the wininet.dll

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > how to ftp in c program?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |