代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
文件传输
作者:
tb_engineer
/ 发布于
2014/9/20
/
601
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT 6000 #define LISTENQ 20 #define BUFFSIZE 4096 #define FILE_NAME_MAX_SIZE 512 int main(int argc, char **argv[]) { //Input the file name char filename[FILE_NAME_MAX_SIZE]; bzero(filename,FILE_NAME_MAX_SIZE); printf("Please input the file name you wana to send:"); scanf("%s",&filename); getchar(); //Create socket int sockfd,connfd; struct sockaddr_in svraddr,clientaddr; bzero(&svraddr,sizeof(svraddr)); svraddr.sin_family=AF_INET; svraddr.sin_addr.s_addr=htonl(INADDR_ANY); svraddr.sin_port=htons(PORT); sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { perror("socket"); exit(1); } //bind if(bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr))<0) { perror("bind"); exit(1); } //listen if(listen(sockfd,LISTENQ)<0) { perror("listen"); exit(1); } while(1) { socklen_t length=sizeof(clientaddr); //accept connfd=accept(sockfd,(struct sockaddr*)&clientaddr,&length); if(connfd<0) { perror("connect"); exit(1); } //send file imformation char buff[BUFFSIZE]; int count; bzero(buff,BUFFSIZE); strncpy(buff,filename,strlen(filename)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(filename)); printf("buff=[%s]\n",buff); count=send(connfd,buff,BUFFSIZE,0); if(count<0) { perror("Send file imformation"); exit(1); } //read file FILE *fd=fopen(filename,"rb"); if(fd==NULL) { printf("File :%s not found!\n",filename); } else { bzero(buff,BUFFSIZE); int file_block_length=0; while((file_block_length=fread(buff,sizeof(char),BUFFSIZE,fd))>0) { printf("file_block_length:%d\n",file_block_length); if(send(connfd,buff,file_block_length,0)<0) { perror("Send"); exit(1); } bzero(buff,BUFFSIZE); } fclose(fd); printf("Transfer file finished !\n"); } close(connfd); } close(sockfd); return 0; } client.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT 6000 #define LISTENQ 20 #define BUFFSIZE 4096 #define FILE_NAME_MAX_SIZE 512 int main(int argc, char **argv[]) { int clientfd; if(argc!=2) { fprintf(stderr,"Usage:./fileclient <IP_Address>\n"); exit(1); } struct sockaddr_in clientaddr; bzero(&clientaddr,sizeof(clientaddr)); clientaddr.sin_family=AF_INET; clientaddr.sin_addr.s_addr=htons(INADDR_ANY); clientaddr.sin_port=htons(0); clientfd=socket(AF_INET,SOCK_STREAM,0); if(clientfd<0) { perror("socket"); exit(1); } if(bind(clientfd,(struct sockaddr*)&clientaddr,sizeof(clientaddr))<0) { perror("bind"); exit(1); } struct sockaddr_in svraddr; bzero(&svraddr,sizeof(svraddr)); if(inet_aton(argv[1],&svraddr.sin_addr)==0) { perror("inet_aton"); exit(1); } svraddr.sin_family=AF_INET; svraddr.sin_port=htons(PORT); socklen_t svraddrlen=sizeof(svraddr); if(connect(clientfd,(struct sockaddr*)&svraddr,svraddrlen)<0) { perror("connect"); exit(1); } //recv file imformation char buff[BUFFSIZE]; char filename[FILE_NAME_MAX_SIZE]; int count; bzero(buff,BUFFSIZE); count=recv(clientfd,buff,BUFFSIZE,0); if(count<0) { perror("recv"); exit(1); } strncpy(filename,buff,strlen(buff)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buff)); printf("Preparing recv file : %s from %s \n",filename,argv[1]); //recv file FILE *fd=fopen(filename,"wb+"); if(NULL==fd) { perror("open"); exit(1); } bzero(buff,BUFFSIZE); int length=0; while(length=recv(clientfd,buff,BUFFSIZE,0)) { if(length<0) { perror("recv"); exit(1); } int writelen=fwrite(buff,sizeof(char),length,fd); if(writelen<length) { perror("write"); exit(1); } bzero(buff,BUFFSIZE); } printf("Receieved file:%s from %s finished!\n",filename,argv[1]); fclose(fd); close(clientfd); return 0; }
试试其它关键字
文件传输
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
tb_engineer
贡献的其它代码
(
3
)
.
Android中模拟手势
.
同步访问post方式
.
文件传输
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3