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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 19th, 2004, 08:01 PM
sujatha_jan sujatha_jan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 sujatha_jan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ftp client program not working - need fix in data connection

Code:
<script language="Javascript">
<!--
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define N_COMMANDS 6 
#define S_PORT 21
#define SALEN 16 
#define TIMEOUT 10 


int main(int argc, char **argv)
{
int sock = 0; 
int nread = 0;
int x = 0; 
int in_sz[N_COMMANDS];
int data = -1 ;
int s ;
int sc ;
size_t len;
size_t fromlen ;
char buf[16384];
struct sockaddr_in data_addr;
struct sockaddr_in myctladdr;
struct sockaddr_in sa;
struct sockaddr_in from;
struct sockaddr_in srv_addr;
struct timeval ts;

char *command[] =
{
"USER sjanardh\r\n",
"PASS welcome\r\n",
"SYST\r\n",
"TYPE I\r\n",
"PORT\r\n",
"LIST\r\n"
};

if(argc != 2) printf("Usage: %s <ip>\n", argv[0]), exit(1);


bzero(&sa, SALEN), bzero(buf, 4096), bzero(&ts, sizeof(struct timeval));

/* setup address structure */
sa.sin_family = AF_INET;
sa.sin_port = htons(S_PORT);
sa.sin_addr.s_addr = inet_addr(argv[1]);

ts.tv_sec = TIMEOUT;

/* get the size of each command without NULL terminator and reset x */
for(x=0; x < N_COMMANDS; x++)
in_sz[x] = strlen(command[x]);
x = 0;

printf(".-~'`ftp_find`'~-.\n"); 
/* create socket */
if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
perror("sock"), exit(0);

/* set timeout option */
/*
if( (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &ts, sizeof(ts))) != 0)
perror("setsockopt"), exit(0);
*/

/*
open named pipe for reading and wait for input from other process
only wait for 1 hour at max then save and shutdown
*/

/*
connect to server
*/

if( (connect(sock, (struct sockaddr *)&sa, SALEN)) < 0)
perror("con"), exit(0);
else
printf("connected to %s...\n", argv[1]);

/* 
This is for the data connection to follow
*/
len = sizeof (myctladdr);
if (getsockname(sock, (struct sockaddr *)&myctladdr, &len) < 0) 
{
perror("ftp: getsockname");
(void) close(sock);
return ((int) (char *)0); 
}

while(x <= N_COMMANDS)
{
sleep(1); 

if(x < N_COMMANDS) 
{
if( (nread = write(sock, command[x], in_sz[x])) < 1)
perror("write"), exit(0);

printf("sending command:%s", command[x]);
}

/*
a timeout indicates success if all commands have been sent,,,, USUALLY
*/
if( (nread = recv(sock, buf, 4096, 0)) < 1)
{
perror("read"), exit(0);
}

/*
if a command isnt accepted/fails then the response code starts with a 
4 or 5 like HTTP
*/
if( (buf[0] == '4') || (buf[0] == '5'))
printf("\ncommand rejected, moving on to next target\n"), exit(0);
else
printf("%s...\n",buf);

if (strcmp("PORT\r\n",command[x]) == 0)
{
data_addr = myctladdr;
/* let system pick one */
data_addr.sin_port = 0; 
data = socket(AF_INET, SOCK_STREAM, 0);
if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) 
{
perror("ftp: bind");
(void) close(data) ;
data = -1 ;
}
len = sizeof (data_addr);
if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) 
{
perror("ftp: getsockname");
(void) close(data) ;
data = -1 ; 
}
if (listen(data, 1) < 0)
perror("ftp: listen");
printf("Data connection part in PORT gone through \n");

/*
sc = socket(AF_INET, SOCK_STREAM, 0);
srv_addr = data_addr ; 
srv_addr.sin_port = 20 ;
len = sizeof(srv_addr) ;
if ( connect(sc,(struct sockaddr *)&srv_addr, len ) < 0) 
perror("con"), exit(0);
else
printf("connected to client...\n");
*/
}

if (strcmp("LIST\r\n",command[x]) ==0 )
{
s = accept(data, (struct sockaddr *) &from, &fromlen);
if (s < 0) {
perror("ftp: accept");
(void) close(s); 
data = -1;
return (NULL);
}
if( (nread = recv(data, buf, 4096, 0)) < 1)
{
perror("read"), exit(0);
}
data =s ;
(void) close(s) ;
}  
x++;
}

/*
status and cleanup
*/
if(x == N_COMMANDS)
printf("Success, logging %s as pub\n", argv[1]);
else
printf("Failure\n");

exit(1);
}
//-->
</script>

Last edited by sujatha_jan : May 19th, 2004 at 08:03 PM. Reason: Problem area in bright orange

Reply With Quote
  #2  
Old May 19th, 2004, 10:28 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
u should look at my site again, i've updated alot of that ftp stuff w/ alot better code i think ur problem is that w/ the port command u are supposed to send the port that u will be listening on. the server has no way of knowing what port to connect back to u on if u don't.
http://www.networknewz.com/2003/0922.html
if u dont want to learn the ftp protocol, check out the ftp library i made it makes this stuff pretty easy. http://www.1nfamus.netfirms.com/ftplib.tar.gz

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > ftp client program not working - need fix in data connection


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 | 
  
 





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