资源简介
在linux上用C实现FTP服务器,可以上传文件和下载文件。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEFAULT_FTP_PORT 21
extern int h_errno;
char user[64]; //ftp usr
char passwd[64]; //ftp passwd
//ftp server address
struct sockaddr_in ftp_server local_host;
struct hostent * server_hostent;
int sock_control;
int mode = 1; //ftp mode 0 is PORT 1 is PASV;
//echo_off and echo_on for get usr password from stdin
static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0TCSANOW&new_settings);
return;
}
void echo_on(void)
{
tcsetattr(0TCSANOW&stored_settings);
return;
}
void cmd_err_exit(char * err_msg int err_code)
{
printf(“%s\n“ err_msg);
exit(err_code);
}
int fill_host_addr(char * host_ip_addr struct sockaddr_in * host int port)
{
if(port <= 0 || port > 65535)
return 254;
bzero(host sizeof(struct sockaddr_in));
host->sin_family = AF_INET;
if(inet_addr(host_ip_addr) != -1)
{
host->sin_addr.s_addr = inet_addr(host_ip_addr);
}
else
{
if((server_hostent = gethostbyname(host_ip_addr)) != 0)
{
memcpy(&host->sin_addr server_hostent->h_addr\
sizeof(host->sin_addr));
}
else return 253;
}
host->sin_port = htons(port);
return 1;
}
int xconnect(struct sockaddr_in *s_addr int type)
{
struct timeval outtime;
int set;
int s = socket(AF_INET SOCK_STREAM 0);
if(s < 0)
cmd_err_exit(“creat socket error!“ 249);
//set outtime for the control socket
if(type == 1)
{
outtime.tv_sec = 0;
outtime.tv_usec = 300000;
}
else
{
outtime.tv_sec = 5;
outtime.tv_usec = 0;
}
set = setsockopt(s SOL_SOCKETSO_RCVTIMEO &outtimesizeof(outtime));
if(set !=0)
{
printf(“set socket %s errno:%d\n“strerror(errno)errno);
cmd_err_exit(“set socket“ 1);
}
//connect to the server
if (connect(s(struct sockaddr *)s_addrsizeof(struct sockaddr_in)) < 0)
{
printf(“Can‘t connect to server %s port %d\n“\
inet_ntoa(s_addr->sin_addr)ntohs(ftp_server.sin_port));
exit(252);
}
return s;
}
//send command to server with sock_fd
int ftp_send_cmd(const char *s1 const char *s2 int sock_fd)
{
char send_buf[256];
int send_err len;
if(s1)
{
strcpy(send_bufs1);
if(s2)
{
strcat(send_buf s2);
strcat(send_buf“\r\n“);
len = strlen(send_buf);
send_err = send(sock_fd send_buf len 0);
}
else
{
strcat(send_buf“\r\n“);
len = strlen(send_buf);
send_err = send(sock
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1151 2009-11-08 02:56 FTP\ftp.h
文件 30383 2009-11-08 06:52 FTP\ftp_cli
文件 27839 2009-11-08 04:31 FTP\ftp_client.c
文件 24762 2009-11-08 04:21 FTP\ftp_server.c
文件 27190 2009-11-08 06:52 FTP\ftp_srv
文件 182 2009-11-08 06:28 FTP\makefile
文件 309 2009-11-29 18:11 FTP\readme.txt
目录 0 2012-11-23 17:32 FTP
----------- --------- ---------- ----- ----
111816 8
评论
共有 条评论