unix下用c语言实现一个基于ftp的文件传输的client 200
需求:提供server的ip地址,账号和密码,使client可以和server相互传.txt的文本文件。小弟没玩过网络编程,希望个位大虾给予指点。越具体越好,可以加分。再...
需求:提供server的ip地址,账号和密码,使client可以和server相互传.txt的文本文件。小弟没玩过网络编程,希望个位大虾给予指点。越具体越好,可以加分。
再补充:linux下的方法先暂时不讨论,就说说unix下的。据说和socket有关系。服务端是别人设计的,我只管客户端;不是我不想管,是我不能管,希望个位理解。所以我必须要设计个client,去和server相连接。 展开
再补充:linux下的方法先暂时不讨论,就说说unix下的。据说和socket有关系。服务端是别人设计的,我只管客户端;不是我不想管,是我不能管,希望个位理解。所以我必须要设计个client,去和server相连接。 展开
展开全部
貌似 Linux 下可以直接使用 ftp 命令
Red Hat 9.0自带VSFTP,你配置好后,别人可
以直接通过终端上传下载文件,至于怎么用C
语言实现,不好意思,哥们儿不会。
用C的system函数调用 ftp 命令
unix下面也有ftp啊
int ftpcmd(int sockftp,char *fmt,...) //cmd
{
va_list vp;
int err,len;
int i,j,n,p;
if (ftpio == NULL)
{
ftpio = fdopen(sockftp,"r"); //socket->file pointer
if (ftpio == NULL)
return -1;
}
if (fmt)
{
va_start(vp,fmt);
len = vsprintf(ftp_buf,fmt,vp);
ftp_buf[len++] = '\r';
ftp_buf[len++]='\n';
write(sockftp,ftp_buf,len); //buf->socket
}
n=read(sockftp,ftp_buffer,sizeof(ftp_buffer));
printf("the buff is %s\n",ftp_buffer);
if(fmt=="PASV") //被动模式下提取需要连接服务端的端口
{
ftp_buffer[n-3]='\0';
str1=ftp_buffer+39;
str2=strchr(str1,',');
i=atoi(str2+1);
n=0;
while(str1[n]!=',')
{
str3[n]=str1[n];
n++;
}
j=atoi(str3);
p=j*256+i;
return p;
}
return 1;
}
int ftp()
{
char *host="ftp.tust.edu.cn";
char *user="ejia";
char *pass="ejia2094ejia";
char *filename="net";
char *savefile="net";
int sockftp = -1,sockdata = -1,sockxfer = -1;
struct sockaddr_in addr;
unsigned long hostip;
int len,tmp,err,err1;
int retval = -1;
int savefd;
char *del=":";
char *port;
struct hostent *he;
hostip = inet_addr(host); //ip
sockftp = socket(AF_INET,SOCK_STREAM,0); //control socket
if (sockftp == -1)
goto out;
addr.sin_family = PF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = hostip;
if (connect(sockftp,(struct sockaddr *)&addr,sizeof(addr)) == -1) //wait for connect
goto out;
err = ftpcmd(sockftp,NULL); //connect ftp server
err = ftpcmd(sockftp,"USER %s",user);
err = ftpcmd(sockftp,"PASS %s",pass);
err = ftpcmd(sockftp,"TYPE I");
err1 = ftpcmd(sockftp,"PASV");
tmp = sizeof(addr);
sockdata = socket(AF_INET,SOCK_STREAM,0); //data socket
if (sockdata == -1)
goto out;
memset(&addr,0,tmp);
addr.sin_family = AF_INET;
addr.sin_port = htons(err1);
addr.sin_addr.s_addr = inet_addr(host);;
sockxfer = connect(sockdata,&addr,tmp);
if (sockxfer == -1)
goto out;
err=ftpcmd(sockftp,"RETR %s",filename);
/*savefd = open(savefile,O_RDWR | O_CREAT); //create file
if (savefd == -1)
goto out; */
len=read(sockdata,ftp_buf,sizeof(ftp_buf));
client_ip=strtok(ftp_buf,del);
port=strtok(NULL,del);
client_port=atoi(port);
printf("the cilentip is %s,the port is %d\n",client_ip,client_port);
/*write(savefd,ftp_buf,len);
close(savefd);*/
out:
close(sockxfer);
close(sockdata);
close(sockftp);
}
注意一下四楼没包含库文件
<sys/un.h>
<netinet/in.h>
<sys/socket.h>
<netdb.h>
<arpa/inet.h>
当然还有<unistd.h>
介绍本书《高级UNIX编程》外文翻译的,
里面讲得很详细。
Red Hat 9.0自带VSFTP,你配置好后,别人可
以直接通过终端上传下载文件,至于怎么用C
语言实现,不好意思,哥们儿不会。
用C的system函数调用 ftp 命令
unix下面也有ftp啊
int ftpcmd(int sockftp,char *fmt,...) //cmd
{
va_list vp;
int err,len;
int i,j,n,p;
if (ftpio == NULL)
{
ftpio = fdopen(sockftp,"r"); //socket->file pointer
if (ftpio == NULL)
return -1;
}
if (fmt)
{
va_start(vp,fmt);
len = vsprintf(ftp_buf,fmt,vp);
ftp_buf[len++] = '\r';
ftp_buf[len++]='\n';
write(sockftp,ftp_buf,len); //buf->socket
}
n=read(sockftp,ftp_buffer,sizeof(ftp_buffer));
printf("the buff is %s\n",ftp_buffer);
if(fmt=="PASV") //被动模式下提取需要连接服务端的端口
{
ftp_buffer[n-3]='\0';
str1=ftp_buffer+39;
str2=strchr(str1,',');
i=atoi(str2+1);
n=0;
while(str1[n]!=',')
{
str3[n]=str1[n];
n++;
}
j=atoi(str3);
p=j*256+i;
return p;
}
return 1;
}
int ftp()
{
char *host="ftp.tust.edu.cn";
char *user="ejia";
char *pass="ejia2094ejia";
char *filename="net";
char *savefile="net";
int sockftp = -1,sockdata = -1,sockxfer = -1;
struct sockaddr_in addr;
unsigned long hostip;
int len,tmp,err,err1;
int retval = -1;
int savefd;
char *del=":";
char *port;
struct hostent *he;
hostip = inet_addr(host); //ip
sockftp = socket(AF_INET,SOCK_STREAM,0); //control socket
if (sockftp == -1)
goto out;
addr.sin_family = PF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = hostip;
if (connect(sockftp,(struct sockaddr *)&addr,sizeof(addr)) == -1) //wait for connect
goto out;
err = ftpcmd(sockftp,NULL); //connect ftp server
err = ftpcmd(sockftp,"USER %s",user);
err = ftpcmd(sockftp,"PASS %s",pass);
err = ftpcmd(sockftp,"TYPE I");
err1 = ftpcmd(sockftp,"PASV");
tmp = sizeof(addr);
sockdata = socket(AF_INET,SOCK_STREAM,0); //data socket
if (sockdata == -1)
goto out;
memset(&addr,0,tmp);
addr.sin_family = AF_INET;
addr.sin_port = htons(err1);
addr.sin_addr.s_addr = inet_addr(host);;
sockxfer = connect(sockdata,&addr,tmp);
if (sockxfer == -1)
goto out;
err=ftpcmd(sockftp,"RETR %s",filename);
/*savefd = open(savefile,O_RDWR | O_CREAT); //create file
if (savefd == -1)
goto out; */
len=read(sockdata,ftp_buf,sizeof(ftp_buf));
client_ip=strtok(ftp_buf,del);
port=strtok(NULL,del);
client_port=atoi(port);
printf("the cilentip is %s,the port is %d\n",client_ip,client_port);
/*write(savefd,ftp_buf,len);
close(savefd);*/
out:
close(sockxfer);
close(sockdata);
close(sockftp);
}
注意一下四楼没包含库文件
<sys/un.h>
<netinet/in.h>
<sys/socket.h>
<netdb.h>
<arpa/inet.h>
当然还有<unistd.h>
介绍本书《高级UNIX编程》外文翻译的,
里面讲得很详细。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
unix下面也有ftp啊
----------------------------------------------------------
下面转一个参考:
int ftpcmd(int sockftp,char *fmt,...) //cmd
{
va_list vp;
int err,len;
int i,j,n,p;
if (ftpio == NULL)
{
ftpio = fdopen(sockftp,"r"); //socket->file pointer
if (ftpio == NULL)
return -1;
}
if (fmt)
{
va_start(vp,fmt);
len = vsprintf(ftp_buf,fmt,vp);
ftp_buf[len++] = '\r';
ftp_buf[len++]='\n';
write(sockftp,ftp_buf,len); //buf->socket
}
n=read(sockftp,ftp_buffer,sizeof(ftp_buffer));
printf("the buff is %s\n",ftp_buffer);
if(fmt=="PASV") //被动模式下提取需要连接服务端的端口
{
ftp_buffer[n-3]='\0';
str1=ftp_buffer+39;
str2=strchr(str1,',');
i=atoi(str2+1);
n=0;
while(str1[n]!=',')
{
str3[n]=str1[n];
n++;
}
j=atoi(str3);
p=j*256+i;
return p;
}
return 1;
}
int ftp()
{
char *host="ftp.tust.edu.cn";
char *user="ejia";
char *pass="ejia2094ejia";
char *filename="net";
char *savefile="net";
int sockftp = -1,sockdata = -1,sockxfer = -1;
struct sockaddr_in addr;
unsigned long hostip;
int len,tmp,err,err1;
int retval = -1;
int savefd;
char *del=":";
char *port;
struct hostent *he;
hostip = inet_addr(host); //ip
sockftp = socket(AF_INET,SOCK_STREAM,0); //control socket
if (sockftp == -1)
goto out;
addr.sin_family = PF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = hostip;
if (connect(sockftp,(struct sockaddr *)&addr,sizeof(addr)) == -1) //wait for connect
goto out;
err = ftpcmd(sockftp,NULL); //connect ftp server
err = ftpcmd(sockftp,"USER %s",user);
err = ftpcmd(sockftp,"PASS %s",pass);
err = ftpcmd(sockftp,"TYPE I");
err1 = ftpcmd(sockftp,"PASV");
tmp = sizeof(addr);
sockdata = socket(AF_INET,SOCK_STREAM,0); //data socket
if (sockdata == -1)
goto out;
memset(&addr,0,tmp);
addr.sin_family = AF_INET;
addr.sin_port = htons(err1);
addr.sin_addr.s_addr = inet_addr(host);;
sockxfer = connect(sockdata,&addr,tmp);
if (sockxfer == -1)
goto out;
err=ftpcmd(sockftp,"RETR %s",filename);
/*savefd = open(savefile,O_RDWR | O_CREAT); //create file
if (savefd == -1)
goto out; */
len=read(sockdata,ftp_buf,sizeof(ftp_buf));
client_ip=strtok(ftp_buf,del);
port=strtok(NULL,del);
client_port=atoi(port);
printf("the cilentip is %s,the port is %d\n",client_ip,client_port);
/*write(savefd,ftp_buf,len);
close(savefd);*/
out:
close(sockxfer);
close(sockdata);
close(sockftp);
}
----------------------------------------------------------
下面转一个参考:
int ftpcmd(int sockftp,char *fmt,...) //cmd
{
va_list vp;
int err,len;
int i,j,n,p;
if (ftpio == NULL)
{
ftpio = fdopen(sockftp,"r"); //socket->file pointer
if (ftpio == NULL)
return -1;
}
if (fmt)
{
va_start(vp,fmt);
len = vsprintf(ftp_buf,fmt,vp);
ftp_buf[len++] = '\r';
ftp_buf[len++]='\n';
write(sockftp,ftp_buf,len); //buf->socket
}
n=read(sockftp,ftp_buffer,sizeof(ftp_buffer));
printf("the buff is %s\n",ftp_buffer);
if(fmt=="PASV") //被动模式下提取需要连接服务端的端口
{
ftp_buffer[n-3]='\0';
str1=ftp_buffer+39;
str2=strchr(str1,',');
i=atoi(str2+1);
n=0;
while(str1[n]!=',')
{
str3[n]=str1[n];
n++;
}
j=atoi(str3);
p=j*256+i;
return p;
}
return 1;
}
int ftp()
{
char *host="ftp.tust.edu.cn";
char *user="ejia";
char *pass="ejia2094ejia";
char *filename="net";
char *savefile="net";
int sockftp = -1,sockdata = -1,sockxfer = -1;
struct sockaddr_in addr;
unsigned long hostip;
int len,tmp,err,err1;
int retval = -1;
int savefd;
char *del=":";
char *port;
struct hostent *he;
hostip = inet_addr(host); //ip
sockftp = socket(AF_INET,SOCK_STREAM,0); //control socket
if (sockftp == -1)
goto out;
addr.sin_family = PF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = hostip;
if (connect(sockftp,(struct sockaddr *)&addr,sizeof(addr)) == -1) //wait for connect
goto out;
err = ftpcmd(sockftp,NULL); //connect ftp server
err = ftpcmd(sockftp,"USER %s",user);
err = ftpcmd(sockftp,"PASS %s",pass);
err = ftpcmd(sockftp,"TYPE I");
err1 = ftpcmd(sockftp,"PASV");
tmp = sizeof(addr);
sockdata = socket(AF_INET,SOCK_STREAM,0); //data socket
if (sockdata == -1)
goto out;
memset(&addr,0,tmp);
addr.sin_family = AF_INET;
addr.sin_port = htons(err1);
addr.sin_addr.s_addr = inet_addr(host);;
sockxfer = connect(sockdata,&addr,tmp);
if (sockxfer == -1)
goto out;
err=ftpcmd(sockftp,"RETR %s",filename);
/*savefd = open(savefile,O_RDWR | O_CREAT); //create file
if (savefd == -1)
goto out; */
len=read(sockdata,ftp_buf,sizeof(ftp_buf));
client_ip=strtok(ftp_buf,del);
port=strtok(NULL,del);
client_port=atoi(port);
printf("the cilentip is %s,the port is %d\n",client_ip,client_port);
/*write(savefd,ftp_buf,len);
close(savefd);*/
out:
close(sockxfer);
close(sockdata);
close(sockftp);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Red Hat 9.0自带VSFTP,你配置好后,别人可
以直接通过终端上传下载文件,至于怎么用C
语言实现,不好意思,哥们儿不会。
以直接通过终端上传下载文件,至于怎么用C
语言实现,不好意思,哥们儿不会。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
注意一下四楼没包含库文件
<sys/un.h>
<netinet/in.h>
<sys/socket.h>
<netdb.h>
<arpa/inet.h>
当然还有<unistd.h>
介绍本书《高级UNIX编程》外文翻译的,
里面讲得很详细。
<sys/un.h>
<netinet/in.h>
<sys/socket.h>
<netdb.h>
<arpa/inet.h>
当然还有<unistd.h>
介绍本书《高级UNIX编程》外文翻译的,
里面讲得很详细。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用C的system函数调用 ftp 命令
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询