• 大小: 21KB
    文件类型: .rar
    金币: 2
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: 其他
  • 标签:

资源简介

1,功能有点类似于ftp的简单文件传输。 2,客户端在Windows段编译运行。 3,服务器端在Linux段编译运行。

资源截图

代码片段和文件信息

#include “ftp.h“

struct enbedded_cmd cmd_list[] = 
{
{ “down“  srv_ftp_down  } 
{ “up“    srv_ftp_up    } 
{ “pwd“   srv_ftp_pwd   } 
{ “cd“    srv_ftp_cd    } 
{ “ls“    srv_ftp_ls    } 
{ “quit“  srv_ftp_quit  }
{ “\0\0“  NULL      }
};

static void *do_ftp(int client_fd);
static void free_cmd(char **cmd);
static char **recv_cmd(int sock);

int  main()
{
int sockfdclient_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
int sin_size = 0;
int on = 1;
if((sockfd = socket(AF_INETSOCK_STREAM0)) == -1)
{
perror(“socket create error“);
exit(1);
}
// fcntl(sockfdF_SETFLO_NONBLOCK);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero)8);
setsockopt(sockfdSOL_SOCKETSO_REUSEADDR&onsizeof(on));
if(bind(sockfd(struct sockaddr *)&my_addrsizeof(struct sockaddr))== -1)
{
perror(“bind error“);
exit(1);
}

if(listen(sockfd5) == -1)
{
perror(“listen error“);
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd(struct sockaddr *)&remote_addr&sin_size))==-1)
{
perror(“accept error“);
close(client_fd);
exit(0);
}
fcntl(client_fdF_SETFLO_NONBLOCK);
printf(“received a connect\n“);
pthread_t thread;
if(0 != pthread_create(&threadNULLdo_ftp(void*)(client_fd)))
perror(“create thread error“);
}
return 0;
}

void *do_ftp(int sock)
{
char **cmd;
int i=0;

while(1)
{
cmd = recv_cmd(sock);
i = 0;
while(cmd_list[i].func)
{
  if(!strncmp(cmd[0]cmd_list[i].cmdstrlen(cmd_list[i].cmd)))
{
cmd_list[i].func(sockcmd);
break;
}
++i;
}
free_cmd(cmd);
}
}

/*recv a cmd from the clnthe step is first recv how many argv the client had input
 * and then request the necessary resourceif 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 lenimessage;
int nbytes;
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;
for(i = 0 ; i< len ;++i)
{
cmd[i] = (char *)malloc(MAX_ARG_LEN);
if(cmd[i] == NULL)
{
free_cmd(cmd);
goto ERROR;
}
}

send_message(sockINIT_OK);
for(i= 0 ;i < len ;++i)
{
nbytes = ftp_recv_line(sockcmd[i]);
cmd[i][nbytes]=0;
}
cmd[i] = NULL;
return cmd;
ERROR:
send_message(sockINIT_ERROR);
return NULL;
}

/*recv a filethere 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 errorthen out;
 * */
int ftp_recv_file(int sockcha

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2017  2009-11-23 02:44  finish_ftp\linux_server\ftp.h

     文件       5115  2009-11-23 02:44  finish_ftp\linux_server\server.c

     文件       7356  2009-11-23 02:44  finish_ftp\linux_server\ftp.c

     文件       5233  2009-11-28 15:40  finish_ftp\windows_client\client.c

     文件       8718  2009-11-28 15:40  finish_ftp\windows_client\ftp.c

     文件       1912  2009-11-28 02:00  finish_ftp\windows_client\ftp.h

     文件       5059  2009-11-28 02:03  finish_ftp\windows_client\server.c

     文件       4435  2009-11-27 22:20  finish_ftp\windows_client\winftp.dsp

     文件        535  2009-11-25 19:18  finish_ftp\windows_client\winftp.dsw

     文件      58368  2009-12-01 15:34  finish_ftp\windows_client\winftp.ncb

     文件      53760  2009-12-01 15:34  finish_ftp\windows_client\winftp.opt

     文件        246  2009-12-01 15:34  finish_ftp\windows_client\winftp.plg

     目录          0  2009-12-01 15:33  finish_ftp\linux_server

     目录          0  2009-12-01 15:34  finish_ftp\windows_client

     目录          0  2009-11-23 02:44  finish_ftp

----------- ---------  ---------- -----  ----

               152754                    15


评论

共有 条评论

相关资源