tcp传输图片(发送端使用sendfile函数)
来源:互联网 发布:智盟软件电话 编辑:程序博客网 时间:2024/05/29 16:33
客户端client向服务器server发送一幅图片,使用tcp传输:
服务器代码:
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <error.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define SER_PORT 10500
- #define MAX_LEN 512
- int main(int argc,char**argv)
- {
- int listensd,connectsd;
- struct sockaddr_in server;
- struct sockaddr_in client;
- char buf[MAX_LEN];
- FILE *fp;
- int i;
- char *filename = "2new.jpg ";
- listensd=socket(AF_INET,SOCK_STREAM,0);
- if(listensd< 0)
- {
- perror( "Creating socket failed: ");
- exit(1);
- }
- int opt=1;
- setsockopt(listensd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
- bzero(&server,sizeof(server));
- server.sin_family = AF_INET;
- server.sin_port = htons(SER_PORT);
- server.sin_addr.s_addr= htonl(INADDR_ANY);
- if(bind(listensd,(struct sockaddr*)&server,sizeof(server))< 0)
- {
- perror( "Bind error. ");
- exit(1);
- }
- if(listen(listensd,5)==-1)
- {
- perror( "listen() error\n ");
- exit(1);
- }
- fp=fopen( filename,"ab");
- if(fp==NULL)
- {
- printf( "File open error!\n ");
- exit(1);
- }
- int rn ;
- int sin_len=sizeof(client);
- if((connectsd= accept(listensd,(struct sockaddr*)&client,&sin_len))< 0)
- {
- printf( "Can 't accept the request!\n ");
- exit(1);
- }
- while(1)
- {
- rn=recv(connectsd,buf,MAX_LEN,0);
- if(rn< 0)
- {
- printf( "Can 't receive file!\n ");
- exit(1);
- }
- buf[rn]='\0';
-
- if(rn!= 0)
- {
- fwrite(buf,1,512,fp);
- bzero(buf,sizeof(buf));
- }
- else
- {
- printf("receive over.\n");
- break;
- }
- }
- close(connectsd);
- fclose(fp);
- close(listensd);
- return 0;
- }
客户端代码:
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <string.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <sys/sendfile.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #define SER_PORT 10500
- #define MAX_LEN 512
- int main(int argc,char**argv)
- {
- int sd;
- int rn;
- struct sockaddr_in client,server;
- struct hostent *h;
- int fq;
- int i;
- char ch;
- struct stat st;
- char *filename = "2.jpg";
- int len = 0;
- sd=socket(AF_INET,SOCK_STREAM,0);
- if(sd< 0)
- {
- printf( "Creating socket error!\n ");
- exit(1);
- }
- if(argc< 2)
- {
- printf( "Not enough parameter!\n ");
- exit(1);
- }
- h=gethostbyname(argv[1]);
- if(h==NULL)
- {
- printf( "Can 't get hostname ");
- exit(1);
- }
- bzero(&server,sizeof(server));
- server.sin_family=h-> h_addrtype;
- server.sin_port=htons(SER_PORT);
- server.sin_addr = *((struct in_addr*)h-> h_addr);
- int opt=1;
- setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
- if(connect(sd,(struct sockaddr*)&server,sizeof(server))< 0)
- {
- printf( "Connect Error!\n ");
- exit(1);
- }
- fq = open(filename, O_RDONLY);
- if( fq< 0 )
- {
- perror("file error");
- exit(1);
- }
- stat(filename,&st);//获取文件大小
- len = st.st_size;
- if(sendfile(sd,fq,0,len)< 0)
- {
- perror("send error");
- exit(1);
- }
-
- close(sd);
- close(fq);
- return 0;
- }
图片必须与程序在同一个目录,即图片须在当前目录下,输出结果是,会在当前目录下新生成一张图片,与原图片相同
阅读全文
0 0
- tcp传输图片(发送端使用sendfile函数)
- Linux下利用sendfile函数传输文件
- 利用Socket.Send发送信息、Socket.SendFile传输文件
- 使用sendfile() 提高网络文件发送性能
- 使用 sendfile() 提升网络文件发送性能
- 使用 sendfile() 提升网络文件发送性能
- TCP传输大文件(图片、文档)
- QT--TCP ----传输图片(buf型)
- TCP传输大文件(图片、文档)
- sendfile函数
- sendfile函数
- 服务器sendfile传输文件
- TCP传输协议使用
- TCP传输图片及多线程
- 使用TCP协议实现客户端向服务器发送图片
- C# tcp发送消息和传输文件
- 适合新手的网络编程,用socket传文件(本例传的是jpg图片) 用到sendfile函数
- 如何使用TCP向远端SERVER传输数据 (发送与接收)
- 2.线性表—单链表
- c++中一些处理i/o流和文件流信息的方法总结
- LeetCode2——Add Two Numbers(两个链表中的数字相加,形成新链表)
- CSS 实现隐藏滚动条同时又可以滚动(转)
- STL unique()函数学习
- tcp传输图片(发送端使用sendfile函数)
- 接口MD5签名校验工具类
- MyBatis插入语句返回主键值
- Windows上搭建React-Native开发环境
- 猫都能学会的Unity3D Shader入门指南(二)
- SVG字体模糊的解决方法
- Android之Monkey全参数(包含隐藏参数)
- linux下使用socket传图片
- 1214 圆桌会议