#include "ftp.h"
struct enbedded_cmd cmd_list[] =
{
{ "down" , cln_ftp_down } ,
{ "up" , cln_ftp_up } ,
{ "pwd" , cln_ftp_pwd } ,
{ "cd" , cln_ftp_cd } ,
{ "ls" , cln_ftp_ls } ,
{ "quit" , cln_ftp_quit } ,
{ "\0\0" , NULL }
};
static void do_ftp(int client_fd);
static void free_cmd(char **cmd);
static char **recv_cmd(int sock);
static char **format2cmd(char *buf);
static char **get_cmd();
int cmdlen(char **cmd);
int main()
{
WSADATA wsaData;
struct sockaddr_in server;
char addr[15];
int sock;
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret)
{
printf("start up socket error\n");
return -1;
}
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==INVALID_SOCKET)
{
printf("create socket error\n");
return -1;
}
printf("input connet address : ");
gets(addr);
server.sin_family=AF_INET;
server.sin_port=htons(SERVPORT);
server.sin_addr.s_addr = inet_addr(addr);
memset(&(server.sin_zero),0,8);
if(connect(sock,(struct sockaddr*)&server,sizeof(server)) == SOCKET_ERROR)
{
perror("connect error in main");
closesocket(sock);
return -1;
}
printf("connect succes\n");
do_ftp(sock);
}
void do_ftp(int sock)
{
char **cmd;
int i=0,cmd_len;
while(1)
{
cmd = get_cmd();
cmd_len = cmdlen(cmd);
send_cmd(sock,cmd,cmd_len);
i = 0;
while(cmd_list[i].func)
{
if(!strncmp(cmd[0],cmd_list[i].cmd,strlen(cmd_list[i].cmd )))
{
cmd_list[i].func(sock,cmd);
break;
}
++i;
}
free_cmd(cmd);
}
}
int send_cmd(int sock,char **argv,int len)
{
int message,i = 0;
send_message(sock,INIT_OK);
send_message(sock,len);
recv_message(sock,&message);
if(message != INIT_OK)
return -1;
while(argv[i])
{
ftp_send_line(sock,argv[i],strlen(argv[i]));
++i;
}
return 0;
}
char **get_cmd()
{
char buf[256],**cmd = NULL;
int nbytes = 0;
printf("--");
fflush(stdout);
while(cmd == NULL)
{
fgets(buf,256,stdin);
if(buf[0] == 0) return get_cmd();
cmd = format2cmd(buf);
}
return cmd;
}
char **format2cmd(char *buf)
{
char **cmd, *arg_begin,*arg_end;
int i = 0,cmd_size = 10,cmd_pos=0;
while(buf[i] == ' ') ++i;
arg_begin = arg_end = &buf[i];
cmd = (char **)malloc(sizeof(char *)* (cmd_size+1));
while(1)
{
if((buf[i] == ' ') || (buf[i] == 0) || (buf[i] == '\n'))
{
arg_end = &buf[i];
if(arg_end != arg_begin)
{
cmd[cmd_pos]= (char *)malloc( arg_end-arg_begin + 1);
strncpy(cmd[cmd_pos],arg_begin,arg_end-arg_begin);
cmd[cmd_pos][arg_end-arg_begin] = 0;
++cmd_pos;
cmd[cmd_pos] = 0;
}
if(buf[i] == 0)
break;
while(buf[++i]==' ') ;
arg_begin = arg_end = &buf[i];
continue;
}
++i;
}
return cmd;
}
int cmdlen(char **cmd)
{
int i = 0;
while(cmd[i]) ++i;
return i;
}
/*recv a cmd from the cln,the step is first recv how many argv the client had input,
* and then request the necessary resource,if errer send a error message to client ,
* after that send a INIT_OK message to client to send the real cmd */
char **recv_cmd(int sock)
{
int len,i,message;
char **cmd;
recv_message(sock,&message); /*ensure the other side init ok*/
if(message != INIT_OK)
return NULL;
recv_message(sock,&len);
cmd = (char **)malloc(sizeof(char *) * (len + 1));
if(cmd == NULL)
goto ERROR_LAB;
for(i = 0 ; i< len ;++i)
{
cmd[i] = (char *)malloc(MAX_ARG_LEN);
if(cmd[i] == NULL)
{
free_cmd(cmd);
goto ERROR_LAB;
}
}
send_message(sock,INIT_OK);
for(i= 0 ;i < len ;++i)
ftp_recv_line(sock,cmd[i]);
cmd[i] = NULL;
return cmd;
ERROR_LAB:
send_message(sock,INIT_ERROR);
return NULL;
}
/*recv a file,there are several steps :
* 1 . recv a message from other side and ensure it is ready to send a file;
* 2 . request the necessary resource ,if error send a error message to other side and out;
* 3 . send a INIT_OK message to the other side and begin to recv data;
* 4 . when recv a line less than buf_size it mean a file send over or recv error,then out;
* */
int ftp_recv_file(int sock,char *filename)
{
int buf_len = FILE_BUF_SIZE,nbytes,message;
int file_len;
FILE *fp;
char *buf;
if((fp = fopen(filename,"wb")) == NULL)
{
perror("open file error on send_file");
goto ERROR_LAB;
}
recv_message(sock,&message);
if(message == FILE_ZERO)
{
fclose(fp);
return 0;
}
else if(message != INIT_OK)
return -1;
if(NULL == (buf = (char *)malloc(FILE_BUF_SIZE)))
{
printf("out of memory in send_file\n");
fclose(fp);
goto ERROR_LAB;
}
send_message(sock,INIT_OK);
recv_message(sock,&file_len);
do
{
nbytes = ftp_recv_line(sock,buf);
fwrite(buf,1,nbytes,fp);
file_len -= nbytes;
}while(file_len > 0);
fclose(fp);
free(buf);
if(nbytes == -1)
return -1;
return 0;
ERROR_LAB:
send_message(sock,INIT_ERROR);
return -1;
}
/* send a file to other side ,there are several steps :
* 1 . request the necessary resource,if error send a error message to other side;
* 2 . send a INIT_OK message to other side;
* 3 . wait and recv a message from other side,if not INIT_OK,send a error message and out;
* 4 . send a the file data;
* */
int ftp_send_file(int sock,char *filename)
{
int buf_len,nbytes,message;
FILE *fp;
long file_len;
char *buf;
if((fp = fopen(filename,"rb")) == NULL)
{
perror("open file error on send_file");
goto ERROR_LAB;
}
if(fseek(fp,0,SEEK_END))
{
perror("set file end error");
goto ERROR_LAB;
}
file_len = ftell(fp);
fseek(fp,0,SEEK_SET);
if(file_len == 0)
{
send_message(sock,FILE_ZERO);
return 0;
}
else if(file_len < FILE_BUF_SIZE)
buf_len = file_len;
else
buf_len = FILE_BUF_SIZE;
if(NULL == (buf = (char *)malloc(buf_len)))
{
printf("out of memory in send_file\n");
fclose(fp);
goto ERROR_LAB;
}
send_message(sock,INIT_OK);
recv_message(sock,&message);
if(message != INIT_OK)
return -1;
send_message(sock,file_len);
while((nbytes = fread(buf,1,buf_len,fp)) != -1)
{
ftp_send_line(sock,buf,nbytes);
file_len -= nbytes;
if(file_len <= 0)
goto SUCCESS_LAB;
}
SUCCESS_LAB:
fclose(fp);
free(buf);
if(nbytes == -1)
{
ftp_send_line(sock,"\0",1);
return -1;
}
return 0;
ERROR_LAB:
send_message(sock,INIT_ERROR);
return -1;
}
int ftp_send_line(int sock,char *buf,int len)
{
int send_bytes = 0,bytes = 0;
int err;
fd_set fs;
FD_ZERO(&fs);
FD_SET(sock,&fs);
send_message(sock,len);
bytes = 0;
while(send_bytes != len)
{
if(-1 == select(sock+1,NULL,&fs, NULL,NULL)) /*wait and recv the data */
{
perror("select error in recv_len");
exit(1);
}
if((bytes = send(sock,buf + send_bytes,len - send_bytes,0)) == -1 )
{
err = WSAGetLastError ();
if(err != WSAEWOULDBLOCK)
{
perror("send line data error in send_line");
exit(1);
}
continue;
}
send_bytes += bytes;
}
return send_bytes;
}
int ftp_recv_line(int sock,char *buf)
{
int len = 0,nbytes = 0,recv_bytes = 0,bytes;
int err;
fd_set fs;
FD_ZERO(&fs);
FD_SET(sock,&fs);
recv_message(sock,&len);
while(recv_bytes != len)
{
if(-1 == select(sock+1,&fs,NULL,NULL,NULL)) /*wait and recv the data */
{
perror("select error in recv_len");
exit(1);
}
if((bytes = recv(sock,buf + recv_bytes ,len - recv_bytes,0)) == -1)
{
err = WSAGetLastError();
if(err == WSAEWOULDBLOCK)
continue;
printf("recv erorr in recv_line\n");
exit(1);
}
recv_bytes += bytes;
}
return len;
}
void send_message(int sock,int message)
{
int send_bytes = 0,bytes;
int err;
fd_set fs;
FD_ZERO(&fs);
FD_SET(sock,&fs);
message = htonl(message);
while(send_bytes != sizeof(message))
{
if(-1 == select(sock+1,NULL,&fs,NULL,NULL))
{
perror("select error in recv_message");
exit(1);
}
if((bytes=send(sock,((char *)&message)+send_bytes,sizeof(message)-send_bytes,0))==-1)
{
err = WSAGetLastError();
if(err == WSAEWOULDBLOCK)
continue;
perror("send error in se
没有合适的资源?快使用搜索试试~ 我知道了~
linux为服务器,Windows为客户端的简单文件传输程序
共12个文件
c:5个
h:2个
ncb:1个
5星 · 超过95%的资源 需积分: 16 149 下载量 28 浏览量
2009-12-01
15:38:54
上传
评论 2
收藏 21KB RAR 举报
温馨提示
1,功能有点类似于ftp的简单文件传输。 2,客户端在Windows段编译运行。 3,服务器端在Linux段编译运行。
资源推荐
资源详情
资源评论
收起资源包目录
finish_ftp.rar (12个子文件)
finish_ftp
linux_server
server.c 5KB
ftp.c 7KB
ftp.h 2KB
windows_client
client.c 5KB
winftp.dsp 4KB
winftp.plg 246B
server.c 5KB
winftp.ncb 57KB
ftp.c 9KB
ftp.h 2KB
winftp.dsw 535B
winftp.opt 53KB
共 12 条
- 1
LiHuiKang
- 粉丝: 2
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页