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:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old November 19th, 2003, 04:08 AM
Waldmeister_ii Waldmeister_ii is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 9 Waldmeister_ii User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question C: ftpclient LIST (beginner) problem

Hello.

I am new to C, have to build a basic ftp client for universtity. But I have some big problems, and hope you can help me.
When I try to use the LIST command, I get the following message:

425 Unable to build data connection: Connection refused

Here is what I have done so far (in a shortend version):
....
struct sockaddr_in servAddr;
struct sockaddr_in clientAddr;
struct sockaddr_in csAddr;
...


/* set values for (sockaddr_in) servAdrr */
...
/* create socket */
controlSocket = socket(AF_INET, SOCK_STREAM, 0);

/* connect to server */
connect(controlSocket, (struct sockaddr *) &servAddr, sizeof(servAddr))

/* create SOCKET dataSocket */
dataSocket = socket( AF_INET, SOCK_STREAM, 0 )

/* set values for (sockaddr_in) clientAdrr */
bzero((char *) &clientAddr, sizeof(clientAddr));
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddr.sin_port = htons(0);

/* BIND socket to (sockaddr_in) clientAddr */
bind(dataSocket,(struct sockaddr *) &clientAddr, sizeof(clientAddr));

/* LISTEN on data_socket */
listen(dataSocket, numberOfConn);

/* send USER command to server over control connection */
strcpy(command,"USER blah");
send(controlSocket, command, strlen(command), 0);

/* send PASS command to server over control connection */
strcpy(command,"PASS blah");
send(controlSocket, command, strlen(command), 0);

/* send PORT command to server over control connection */
gethostname(port_name, PORTBUFFER);
pt_clientAddress = gethostbyname(port_name);
host_id = * ((int *) (pt_clientAddress->h_addr_list[0]));
host_id = htonl(host_id);
sprintf(command, "PORT %d,%d,%d,%d,%d,%d\r\n",
(host_id & 0xFF000000) >> 24,
(host_id & 0x00FF0000) >> 16,
(host_id & 0x0000FF00) >> 8,
(host_id & 0x000000FF),
(clientAddr.sin_port & 0xFF00) >> 8,
(clientAddr.sin_port & 0x00FF));
send(controlSocket, command, strlen(command), 0);

/* send MODE command to server over control connection */
strcpy(command,"MODE S\r\n");
send(controlSocket,command, strlen(command), 0);

/* send TYPE command to server over control connection */
strcpy(command,"TYPE A\r\n");
send(controlSocket,command, strlen(command), 0);

/* send LIST command to server over control connection */
strcpy(command,"LIST\r\n");
send(controlSocket,command, strlen(command), 0);

/* create loop to incoming connections */
while(1) {
*sin_size = sizeof(struct sockaddr_in);
if( (fd = accept(dataSocket,(struct sockaddr *) &csAddr, sin_size )) == -1 ) {
fprintf(stderr, "ERROR: can't acceopt connections to dataSocket.\n");
exit(-1);
}
}


So, for all ftp-commands I send, I get a possitive reply from the server. Then comes "LIST", and I get the "425 Unable to build data connection: Connection refused" reply. Can someone of you please tell me, where my error is? What did I initialize wrong?

Thanks in advanced for your help

Greetings

Henning

Reply With Quote
  #2  
Old November 19th, 2003, 05:25 AM
peenie's Avatar
peenie peenie is offline
Google Relay Server
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: ur MOM
Posts: 1,811 peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)peenie User rank is First Lieutenant (10000 - 20000 Reputation Level)  Folding Points: 9953 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 1 Day 15 h 22 m 4 sec
Reputation Power: 197
Send a message via AIM to peenie Send a message via MSN to peenie
I can only think of a couple of things off the top of my head, although I'm not really an expert:

1) You are behind a firewall and the incoming connections can't get through. You need to pick a port for dataSocket rather than letting the machine pick, and then allow incoming connections on that port through the firewall, or

2) You are not sending the right info with the PORT command. Try using getsockname() on dataSocket to get the information rather than gethostname(). Also, if you are on a LAN, you might be sending your LAN IP in the port command rather than the WAN one, which the FTP server won't care about if it's on a different network than you, or

3) One of your socket calls is failing. Maybe you should check them for errors.

Regardless, the problem is that the FTP server can't connect to the address/port that you specified with the PORT command. That much I know.

J.C.

Reply With Quote
  #3  
Old November 19th, 2003, 06:48 AM
Waldmeister_ii Waldmeister_ii is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 9 Waldmeister_ii User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Thanks, but still....

Hey, thanks for your reply!
But I still got some problems. I now use the getsockname() function, in this way:

...
/* set values for (sockaddr_in) clientAdrr */
bzero((char *) &clientAddr, sizeof(clientAddr));
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddr.sin_port = htons(0); //create with a random port number

/* BIND socket to (sockaddr_in) clientAddr */
if( bind(dataSocket,(struct sockaddr *) &clientAddr, sizeof(clientAddr)) == -1 ) {
fprintf(stderr, "ERROR: bind() of myAddr to socket failed!\n");
exit(-1);
}

/* GETSOCKNAME: get the client socket name */
*sin_size = sizeof(clientAddr);
if( ( getsockname(dataSocket, (struct sockaddr *)&clientAddr, sin_size)) == -1 ) {
fprintf(stderr, "ERROR: can't get clientSocketname.\n");
exit(-1);
}

....

But if I try to get the adress like this:

clientAddr.sin_addr;

I just get a bunch of zeros, and no nice address.
Did I forget to use something, to initialize something?

Please help me again!

Thanks

Henning

Reply With Quote
  #4  
Old November 19th, 2003, 01:55 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
getsockname returns the local port number assigned after binding to random port, not the address. if you want the address you have to connect/accept the socket(or new socket for accept) and then call getsocketname. the reason? when you bind to INADDR_ANY you are telling the kernel you're willing to accept a connection on any address configured on this host. if you had a multi-homed host, this means at the time of binding you have no idea which address the connection will come in on. so you have to wait until you have completed the connection and actually know which address was used.

ps. if you're interested i wrote an ftp library a couple weeks ago. it's on my site found below.

Last edited by infamous41md : November 19th, 2003 at 02:32 PM.

Reply With Quote
  #5  
Old November 19th, 2003, 02:38 PM
Waldmeister_ii Waldmeister_ii is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 9 Waldmeister_ii User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok man, thank you very much. I will try this tomorrow and let you know if it works for me But I guess it will.

Thanks again for the fast reply

bye

Henning

Reply With Quote
  #6  
Old November 20th, 2003, 05:42 AM
Waldmeister_ii Waldmeister_ii is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 9 Waldmeister_ii User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb

Ok, getsocketname() didn't work, since there is no function getsocketname() !?!
I used the following stuff to get my IP address:

hostent *pt_serverAddress;
gethostname((char *)hostname, sizeof(hostname));
if( (pt_serverAddress = gethostbyname((char *)hostname)) == NULL ) {
fprintf(stderr, "ERROR: couldn't get ip address of host by using gethostbyname().");
}

With that it works fine.

Thanks for your help guys

bye

Henning

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > C: ftpclient LIST (beginner) problem


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 | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway