当前位置: 技术问答>linux和unix
高分相送!!! 在UNIX/QNX下在程序中如何实现FTP客户端?
来源: 互联网 发布时间:2014-12-04
本文导语: 如何在程序中实现FTP的客户端,使之能够从FTP服务端下载文件? 最好是源代码,相关链接也可以! 凡是有参考价值的,一定送高分。 | //Filename:FtpFunction.c //Designed by ZhouLifa From Computer Resea...
如何在程序中实现FTP的客户端,使之能够从FTP服务端下载文件?
最好是源代码,相关链接也可以!
凡是有参考价值的,一定送高分。
最好是源代码,相关链接也可以!
凡是有参考价值的,一定送高分。
|
//Filename:FtpFunction.c
//Designed by ZhouLifa From Computer Research Institution, South China University.
//Date:Aug 15, 2001
//Any bug please contact me at zhoulifa@yahoo.com
#include "FtpFunction.h"
int connect_server( char * host, int port ) {
int ctrl_sock = 0;
unsigned long addr;
struct sockaddr_in sin;
char buf[512] = "";
int result = 0;
int len = 0;
if ( (addr = get_ipaddr( host )) == 0 ) return -1;
ctrl_sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( ctrl_sock == -1 ) return -1;
memset( (char *)&sin, 0, sizeof(struct sockaddr_in) );
sin.sin_family = AF_INET;
sin.sin_port = htons( port );
sin.sin_addr.s_addr = htonl( addr );
if( connect(ctrl_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 ) {
close( ctrl_sock );
return -1;
}
if(TimeOut(ctrl_sock, 300) != 0) {
return -1;
}
len = recv( ctrl_sock, buf, 512, 0 );
buf[len] = 0;
sscanf( buf, "%d", &result );
if ( result != 220 ) {
close( ctrl_sock );
return -1;
}
return ctrl_sock;
}
unsigned long get_ipaddr( char * host ) {
return ipstr2long( host );
}
unsigned long ipstr2long( char * ip_str ) {
char buf[6];
char * ptr;
int i;
int count;
unsigned long ipaddr;
int cur_byte;
ipaddr = 0;
for ( i = 0; i
//Designed by ZhouLifa From Computer Research Institution, South China University.
//Date:Aug 15, 2001
//Any bug please contact me at zhoulifa@yahoo.com
#include "FtpFunction.h"
int connect_server( char * host, int port ) {
int ctrl_sock = 0;
unsigned long addr;
struct sockaddr_in sin;
char buf[512] = "";
int result = 0;
int len = 0;
if ( (addr = get_ipaddr( host )) == 0 ) return -1;
ctrl_sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( ctrl_sock == -1 ) return -1;
memset( (char *)&sin, 0, sizeof(struct sockaddr_in) );
sin.sin_family = AF_INET;
sin.sin_port = htons( port );
sin.sin_addr.s_addr = htonl( addr );
if( connect(ctrl_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 ) {
close( ctrl_sock );
return -1;
}
if(TimeOut(ctrl_sock, 300) != 0) {
return -1;
}
len = recv( ctrl_sock, buf, 512, 0 );
buf[len] = 0;
sscanf( buf, "%d", &result );
if ( result != 220 ) {
close( ctrl_sock );
return -1;
}
return ctrl_sock;
}
unsigned long get_ipaddr( char * host ) {
return ipstr2long( host );
}
unsigned long ipstr2long( char * ip_str ) {
char buf[6];
char * ptr;
int i;
int count;
unsigned long ipaddr;
int cur_byte;
ipaddr = 0;
for ( i = 0; i