Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28204
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-28 16:20
文章分类
文章存档

2015年(4)

2014年(2)

我的朋友

分类: LINUX

2015-05-15 11:31:17

客户端client向服务器server发送一幅图片,使用tcp传输:
 
服务器代码:
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <error.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>

  10. #define SER_PORT 10500
  11. #define MAX_LEN 512

  12. int main(int argc,char **argv)
  13. {
  14.     int listensd,connectsd;
  15.     struct sockaddr_in server;
  16.     struct sockaddr_in client;
  17.     char buf[MAX_LEN];
  18.     FILE *fp;
  19.     int i;
  20.     char *filename = "2new.jpg ";

  21.     listensd=socket(AF_INET,SOCK_STREAM,0);
  22.     if(listensd < 0)
  23.     {
  24.         perror( "Creating socket failed: ");
  25.         exit(1);
  26.     }

  27.     int opt=1;
  28.     setsockopt(listensd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

  29.     bzero(&server,sizeof(server));
  30.     server.sin_family = AF_INET;
  31.     server.sin_port = htons(SER_PORT);
  32.     server.sin_addr.s_addr = htonl(INADDR_ANY);

  33.     if(bind(listensd,(struct sockaddr *)&server,sizeof(server)) < 0)
  34.     {
  35.         perror( "Bind error. ");
  36.         exit(1);
  37.     }

  38.     if(listen(listensd,5) == -1)
  39.     {
  40.         perror( "listen() error\n ");
  41.         exit(1);
  42.     }

  43.     fp=fopen( filename, "ab");
  44.     if(fp == NULL)
  45.     {
  46.         printf( "File open error!\n ");
  47.         exit(1);
  48.     }

  49.     int rn ;
  50.     int sin_len=sizeof(client);
  51.     if((connectsd = accept(listensd,(struct sockaddr *)&client,&sin_len)) < 0)
  52.     {
  53.         printf( "Can 't accept the request!\n ");
  54.         exit(1);
  55.     }

  56.     while(1)
  57.     {
  58.         rn=recv(connectsd,buf,MAX_LEN,0);
  59.         if(rn < 0)
  60.         {
  61.             printf( "Can 't receive file!\n ");
  62.             exit(1);
  63.         }

  64.         buf[rn]= '\0 ';
  65.             
  66.         if(rn != 0)
  67.         {
  68.             fwrite(buf,1,512,fp);
  69.             bzero(buf,sizeof(buf));     
  70.         }
  71.         else
  72.         {
  73.             printf("receive over.\n");
  74.             break;
  75.         }                                        
  76.     }
  77.     close(connectsd);
  78.     fclose(fp);
  79.     close(listensd);
  80.     return 0;
  81. }
客户端代码:
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include <sys/sendfile.h>
  11. #include <sys/ioctl.h>
  12. #include <fcntl.h>
  13. #include <sys/stat.h>

  14. #define SER_PORT 10500
  15. #define MAX_LEN 512

  16. int main(int argc,char **argv)
  17. {
  18.     int sd;
  19.     int rn;
  20.     struct sockaddr_in client,server;
  21.     struct hostent *h;
  22.     int fq;
  23.     int i;
  24.     char ch;
  25.     struct stat st;
  26.     char *filename = "2.jpg";
  27.     int len = 0;

  28.     sd=socket(AF_INET,SOCK_STREAM,0);
  29.     if(sd < 0)
  30.     {    
  31.         printf( "Creating socket error!\n ");
  32.         exit(1);
  33.     }

  34.     if(argc < 2)
  35.     {
  36.         printf( "Not enough parameter!\n ");
  37.         exit(1);
  38.     }

  39.     h=gethostbyname(argv[1]);
  40.     if(h == NULL)
  41.     {
  42.         printf( "Can 't get hostname ");
  43.         exit(1);
  44.     }

  45.     bzero(&server,sizeof(server));
  46.     server.sin_family=h-> h_addrtype;
  47.     server.sin_port=htons(SER_PORT);
  48.     server.sin_addr = *((struct in_addr *)h-> h_addr);

  49.     int opt=1;
  50.     setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

  51.     if(connect(sd,(struct sockaddr *)&server,sizeof(server)) < 0)
  52.     {
  53.         printf( "Connect Error!\n ");
  54.         exit(1);
  55.     }

  56.     fq = open(filename , O_RDONLY);
  57.     if( fq < 0 )
  58.     {
  59.         perror("file error");
  60.         exit(1);
  61.     }

  62.     stat(filename,&st);//获取文件大小
  63.     len = st.st_size;

  64.     if(sendfile(sd,fq,0,len) < 0)
  65.     {
  66.         perror("send error");
  67.         exit(1);
  68.     }
  69.     
  70.     close(sd);
  71.     close(fq);
  72.     return 0;
  73. }
图片必须与程序在同一个目录,即图片须在当前目录下,输出结果是,会在当前目录下新生成一张图片,与原图片相同。
阅读(1648) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~