//简易ftp客户端
#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <pthread.h>

#include <sys/stat.h>

#include <fcntl.h>

typedef struct sockaddr* sockaddrp;

//命令传输

int sockfd;

//数据传输

int sockfd1;

char buf[4096] = {};

//地址

char buf1[4096] = {};

//帐号、密码

char buf2[4096] = {};

//存放命令名

char tem1[255] = {};

//存放文件名

char tem2[255] = {};

struct sockaddr_in new_addr = {AF_INET};

void func()

{

 sprintf(buf,"PASV\n");

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 int a[4];

    int b[2];

    printf("%s\n",buf);

    sscanf(buf,"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)",&a[0],&a[1],&a[2],&a[3],&b[0],&b[1]);

    int msg = b[0]*256+b[1];

 sockfd1 = socket(AF_INET,SOCK_STREAM,0);



 new_addr.sin_port = htons(msg);

 new_addr.sin_addr.s_addr = inet_addr("192.168.6.66");

 connect(sockfd1,(sockaddrp)&new_addr,sizeof(new_addr));

}

//目录

void ls_run()

{



 sprintf(buf,"LIST\n"); 

 send(sockfd,buf,strlen(buf),0);



 bzero(buf,sizeof(buf));

 recv(sockfd1,buf,sizeof(buf),0);

 printf("server:%s\n",buf);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server:%s\n",buf);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server:%s\n",buf);



}

//进入目录

void cd_run()

{

//-----------------------------------------------------



 sprintf(buf,"CWD %s\n",tem2); 

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 sprintf(buf,"PWD\n"); 

 send(sockfd,buf,strlen(buf),0);



 bzero(buf,sizeof(buf));

 recv(sockfd1,buf,sizeof(buf),0);

 printf("server1:%s",buf);



 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server2:%s\n",buf);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server3:%s\n",buf);

 sprintf(buf1,"%s",buf);

 func(); 

//------------------------------------------------------- 

 sprintf(buf,"LIST\n"); 

 send(sockfd,buf,strlen(buf),0);



 bzero(buf,sizeof(buf));

 recv(sockfd1,buf,sizeof(buf),0);

 printf("%s",buf);



 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server1:%s\n",buf);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server2:%s\n",buf);



}

//下载

void get_run()

{

 func();

 sprintf(buf,"RETR %s\n",tem2); 

 send(sockfd,buf,strlen(buf),0);

 printf("%s",buf);

 bzero(buf,sizeof(buf));

 recv(sockfd1,buf,sizeof(buf),0);



 int tmp=sizeof(new_addr);

 int fd = open(tem2,O_WRONLY|O_CREAT|O_TRUNC,0644);



 write(fd,buf,strlen(buf));

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("%s",buf);



 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 close(fd);

}

//上传

void put_run()

{

 char inf[1024] = {};

 func();

 int fd = open(tem2,O_RDONLY);

 read(fd,inf,strlen(buf));

 close(fd);



 sprintf(buf,"STOR %s\n",tem2); 

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("%s",buf);

 sprintf(buf,"%s\n",inf); 

 send(sockfd1,buf,sizeof(buf),0);

 close(sockfd1);



 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("%s",buf);







}

//删除

void delete_run()

{

 func();

 sprintf(buf,"DELE %s\n",tem2);

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("%s",buf);

//-------------------------------------------------



 sprintf(buf,"LIST\n"); 

 send(sockfd,buf,strlen(buf),0);



 bzero(buf,sizeof(buf));

 recv(sockfd1,buf,sizeof(buf),0);

 printf("%s",buf);



 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server1:%s\n",buf);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server2:%s\n",buf);

}



//登陆

void login(void)

{



 recv(sockfd,buf,sizeof(buf),0);

 printf("server:%s",buf);

//用户帐号输入

 printf("输入用户名:");

 gets(buf2);

 sprintf(buf,"%s %s\n","USER",buf2);

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 //printf("server:%s",buf);

//用户密码输入

 printf("输入密码:");

 gets(buf2);

 sprintf(buf,"%s %s\n","PASS",buf2);

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server:%s",buf);





 sprintf(buf,"SYST\n");

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server:%s",buf);

 sprintf(buf,"FEAT\n");

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("server:%s",buf);

 sprintf(buf,"OPTS UTF8 ON\n");

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("%s",buf);

 sprintf(buf,"PWD\n");

 send(sockfd,buf,strlen(buf),0);

 bzero(buf,sizeof(buf));

 recv(sockfd,buf,sizeof(buf),0);

 printf("%s",buf);



//功能

 while(1)

 { 

  printf("ftp> ");

  //获取命令 

  gets(buf);

  //命令分支

  if(!strcmp(buf,"ls"))

  {

   func();

   ls_run();

   close(sockfd1);

   continue;

  }

  if(!strcmp(buf,"bye"))

  {

   break; 

  }

  sscanf(buf,"%s %s",tem1,tem2);

  if(!strcmp(tem1,"cd") )

  { 



   cd_run();

   close(sockfd1);

   continue;

  }



  if(!strcmp(tem1,"get"))

  {

   get_run();

   close(sockfd1);

   continue;

  }

  if(!strcmp(tem1,"put"))

  {



   put_run();



   continue;

  }

  if(!strcmp(tem1,"delete"))

  {

   delete_run();

   close(sockfd1);

   continue;

  }

 }

}

//主函数

int main(int argc,char* argv[])

{ 

 //socket

 sockfd = socket(AF_INET,SOCK_STREAM,0);

 if(0 > sockfd)

 {

  perror("socket");

  return -1;

 }

 struct sockaddr_in addr = {AF_INET};

 addr.sin_port = htons(21);

 addr.sin_addr.s_addr = inet_addr(argv[1]);

 int ret = connect(sockfd,(sockaddrp)&addr,sizeof(addr));

 if(0 > ret)

 {

  perror("connect");

  return -1;

 }



 login();





 close(sockfd);

}

1. printf 格式化输入输出

printf     %[flags] [width] [.prec] [将多个文本变成3个文书,文件的开创文件与公事的读取。hlL] type

澳门金沙国际 1

澳门金沙国际 2

printf(“%*d\n”,6,123); 把6改成参数,其实是 printf(“%6d\n”,123);

澳门金沙国际 3

澳门金沙国际 4

int num; 

printf(“%d%n\n”,12345,&num); 

当printf 到那边时曾经出口了有点个字符。填到变量num中。

public void createFile() throws IOException{
        File file=new File("c:/myFileTest/test1.doc");
        FileOutputStream fos=new FileOutputStream(file);//构建FileOutputStream对象,文件不存在会自动新建
        OutputStreamWriter osWriter=new OutputStreamWriter(fos, "utf-8");
        //构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
        osWriter.append("这个是中文输入啦啦");
        //写入到缓冲区
        osWriter.append("\r\n");
        //换行
        osWriter.append("this is English input");
        //刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
        osWriter.close();
        //关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
        fos.close();
        //关闭输出流,释放系统资源
    }

    public void readFile() throws IOException{
        File file=new File("c:/myFileTest/test1.doc");
        FileInputStream fis=new FileInputStream(file);//创建FileInputStream对象
        InputStreamReader isReader=new InputStreamReader(fis,"utf-8");
        //创建InputStreamReader对象,编码与写入相同
        StringBuffer sb=new StringBuffer();
        while(isReader.ready()){
            sb.append((char)isReader.read());
            // 转成char加到StringBuffer对象中
        }
        System.out.println(sb.toString());
        isReader.close();
        // 关闭读取流
        fis.close();
        // 关闭输入流,释放系统资源
    }

    /**
     * inputStream和outputStream的用法
     * 该方法不好用,因为会出现乱码
     */
    public void InputStreamAndOutputStream() throws IOException{
        //创建文件流
        byte bwrite[]={11,43,23,57};
        OutputStream os=new FileOutputStream("c:/myFileTest/testa.txt");
        for(byte a:bwrite){
            os.write(a);
        }
        os.close();

        //读取文件流
        InputStream is=new FileInputStream("c:/myFileTest/testa.txt");
        int size=is.available();
        for(int i=0;i<size;i++){
            System.out.println((char)is.read());
        }
        is.close();
    }

    /**
     * 创建文件夹
     */
    public void createDir(){

        String dirName="c:/yin/yan/ling";
        File f=new File(dirName);
        f.mkdirs();//mkdirs()方法创建一个文件夹和它的所有父文件夹。


        /*File类中有两个方法可以用来创建文件夹:
        mkdir( )方法创建一个文件夹,成功则返回true,失败则返回false。失败表明File对象指定的路径已经存在,或者由于整个路径还不存在,该文件夹不能被创建。
        mkdirs()方法创建一个文件夹和它的所有父文件夹。*/
    }
    /**
     * 读取目录
     */
    @Test
    public void DirList(){
        String dirName="c:/yin";
        File f=new File(dirName);


        //判断是否文件夹
        if(f.isDirectory()){

            System.out.println("directory of ::"+dirName);
            String s[]=f.list();
            System.out.println("the list length is "+s.length);

            for(int i=0;i<s.length;i++){

                File f1=new File(dirName+"/"+s[i]);

                if(f1.isDirectory()){
                    System.out.println("directory of :::"+s[i]);
                }else{
                    System.out.println(s[i]+" is not directory");
                }

            }
        }else{
            System.out.println(dirName+" is not a directory");
        }
    }

@echo on
for /r %%a in (*.cs) do type “%%a” >>%%~dpanewcsv.txt

2. scanf 格式化输入输出

scanf    %[flag] type

澳门金沙国际 5

scanf(“%*d%d”,&num);   跳过输入的率先个整数

澳门金沙国际 6

%[^,] 读逗号前的有着东西

  

3. printf 和 scanf 的重回值

澳门金沙国际 7

scanf 再次来到读入了多少个变量。

4.文件输入输出

澳门金沙国际 8

澳门金沙国际 9

澳门金沙国际 10

澳门金沙国际 ,在 w 和 a 后加 x 幸免对已部分文件破坏。

5.二进制文件

澳门金沙国际 11

澳门金沙国际 12

澳门金沙国际 13

澳门金沙国际 14

澳门金沙国际 15

澳门金沙国际 16

澳门金沙国际 17

澳门金沙国际 18

相关文章