|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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/ |
|
#4
|
|||
|
|||
|
Quote:
i'll give that a look, thanks |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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). |
|
#7
|
|||
|
|||
|
thank you for the code msschoolbus. i think it may be a little above my knowledge tho. i couldnt follow it.
|
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
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
|
|
#10
|
||||
|
||||
|
i emailed you an interesting program i wrote as well. not exactly an ftp "client" but you'll probably find it helpful.
![]() |
|
#11
|
||||
|
||||
|
if you search the msdn library for ftp functions you will find several available on the windows platform. They all use the wininet.dll
|
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > how to ftp in c program? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|