客户端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;
- }
图片必须与程序在同一个目录,即图片须在当前目录下,输出结果是,会在当前目录下新生成一张图片,与原图片相同。
阅读(9559) | 评论(0) | 转发(1) |