博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix - "tcp & tcpm"
阅读量:6240 次
发布时间:2019-06-22

本文共 3100 字,大约阅读时间需要 10 分钟。

Brief introduction about "tcp & tcpm" in basic unix.

This is a homework of unix class which is all about copy a file, not the TCP(Transmission Control Protocol).

tcp:

Base on read(), write(), lseek() and open() methods to copy a file under unix enviroment. This is an example:

tcp file1 file2 (./a.out file1 file2)
tcp file1 dir
tcp file1 dir/file2
If target is a file, then copy the source into target file. If target is a directory file1 would copyed into this dir.

tcpm:

Copy a file via mmap() and memcpy() instead of read() and write() to make the same usage like tcp.

How to achive tcp:

Here are 2 cores in this case:

1. copy file1 to file2

2. if argv[2] is a dir, copy file1 into this dir.

1.copy a file is wirtten in APUE at Part3.9.

And make sure before read/write you have make sure the files are opened.

while((read_size = read(fd_src_file, n, BUFFSIEZ)) > 0){        if ((write_size = write(fd_dest_file, n, read_size)) != read_size){            perror("write dest_file fail");            exit(1);        }    }

2.copy in target directory

Change avg[2] from "/User/.../Desktop" to "/User/.../Desktop/file2".

This method is not the perfect, even a little bit funny. If you have a better idea you can tell me.

if (S_ISDIR(buf.st_mode)){        // change the directory if file copy path is not current dir        sprintf(copy_file_name,"%s%s%s",argv[2],"/",argv[1]);        printf("change dir to %s\n", copy_file_name);    }

CODE:

#include 
#include
#include
#include
#include
#include
#define BUFFSIEZ 512int main(int argc, char const *argv[]){ int fd_src_file, fd_dest_file; int read_size, write_size; char n[BUFFSIEZ]; char copy_file_name[50]; struct stat buf; // check input if (argc != 3){ printf("a.out src_file to dest_file...\n"); exit(1); } strcpy(copy_file_name, argv[2]); if (lstat(argv[2], &buf) < 0) { perror("lstat error"); } if ((fd_src_file = open(argv[1],O_RDWR,0774)) < 0) { perror("open src_file fail"); exit(1); } else printf("open file ok.\n"); if (S_ISDIR(buf.st_mode)){ // change the directory if file copy path is not current dir sprintf(copy_file_name,"%s%s%s",argv[2],"/",argv[1]); printf("change dir to %s\n", copy_file_name); } if ((fd_dest_file = open(copy_file_name,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR,0600)) < 0){ perror("create dest_file fail"); exit(1); } while((read_size = read(fd_src_file, n, BUFFSIEZ)) > 0){ if ((write_size = write(fd_dest_file, n, read_size)) != read_size){ perror("write dest_file fail"); exit(1); } } if (read_size < 0) { fprintf(stderr, "read error\n"); exit(1); } printf("read/write ok\n"); return 0;}

How to achive tcpm:

Search for mmap() and memcpy is the best way to understand how do these method works. Here is a better blog which introduces using mmap() to copy a file.

http://blog.chinaunix.net/uid-20662363-id-1904142.html

At last

Only when I start to write did I understang the patient I have to pay for my blogs. Greet to all those great tech-bloggers!

转载于:https://www.cnblogs.com/22Kon/p/6713387.html

你可能感兴趣的文章
【深入Linux块设备驱动层次之一】整体层次
查看>>
Linux登录安全及用户操作审计 ,linux下清理日志脚本
查看>>
一个JS文件中引入另一个JS文件
查看>>
Laravel中pluck的使用——返回指定的字段值信息列表
查看>>
TCP Segment Offload(TSO)的实现原理浅析
查看>>
Android官方开发文档Training系列课程中文版:多样屏幕之支持不同的屏幕尺寸
查看>>
Redmine部署
查看>>
五种开源协议比较:BSD,Apache,GPL,LGPL,MIT
查看>>
Linux上编译hadoop-2.7.1的libhdfs.so和libhdfs.a
查看>>
mysql中函数greatest 与MAX区别
查看>>
ORA-01843: 无效的月份
查看>>
Centos网络管理(三)-网络配置相关
查看>>
php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理
查看>>
编写iptables模块实现不连续IP地址的DNAT-POOL
查看>>
IOS真机测试
查看>>
linux下刻录光盘所发生的问题及解决办法
查看>>
IIS负载均衡-Application Request Route详解第一篇: ARR介绍
查看>>
IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm
查看>>
源码安装程序包
查看>>
ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary
查看>>