Chinaunix首页 | 论坛 | 博客
  • 博客访问: 972409
  • 博文数量: 403
  • 博客积分: 27
  • 博客等级: 民兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-25 22:20
文章分类

全部博文(403)

文章存档

2016年(3)

2015年(16)

2014年(163)

2013年(222)

分类: LINUX

2013-12-31 16:46:33

原文地址:linux下使用socket传图片 作者:any_wind

server端:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h> //sockaddr_in

  7. #define PORT 6675

  8. int main()
  9. {
  10.         int socket_fd;
  11.         int new_fd;
  12.         //建立套接字
  13.         socket_fd=socket(AF_INET,SOCK_STREAM,0);// int socket(int domain, int type, int protocol);
  14.         printf("socket_fd is %d\n",socket_fd);
  15.         
  16.         //初始化设置地址和端口号
  17.     struct sockaddr_in server_addr;

  18.         memset(&server_addr,0,sizeof(server_addr));
  19.         server_addr.sin_family=AF_INET;
  20.         server_addr.sin_port=htons(PORT);
  21.         server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  22.     memset(&(server_addr.sin_zero),0,8);
  23.         //设置端口可重用
  24.         int contain;
  25.         setsockopt(socket_fd,SOL_SOCKET, SO_REUSEADDR, &contain, sizeof(int));
  26.         
  27.         //绑定fd和本机地址
  28.         int res=bind(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
  29.         printf("bind res is %d\n",res);
  30.         if(res==-1)
  31.                 return -1;

  32.         if(listen(socket_fd,20)==-1)
  33.         {
  34.                 printf("Failed to listen!\n");
  35.                 return -1;
  36.         }
  37.         printf("after listen\n");

  38.         while(1)
  39.         {
  40.             struct sockaddr_in client_addr;
  41.         int size=sizeof(client_addr);
  42.         new_fd=accept(socket_fd, (struct sockaddr *)&client_addr, &size);
  43.                 if(new_fd==-1)
  44.                 {
  45.                         printf("wait for client to connect!\n");
  46.                         sleep(1);
  47.                         continue;
  48.                 }
  49.                 printf("A connect and the new fd is %d\n",new_fd);
  50.         /*
  51.                 while(1)
  52.                 {
  53.          char input[100];
  54.                         char output[100];

  55.          res=read(new_fd,input,100);
  56.          input[res]=0;
  57.          printf("client says '%s'\n",input);
  58.                         
  59.                         gets(output);
  60.                         res=write(new_fd,output,strlen(output));
  61.                 }
  62.         */
  63.         
  64.                 //while(1)
  65.                 {
  66.                         FILE *in;
  67.                         char input[100];
  68.                         res=read(new_fd,input,100);
  69.                         input[res]=0;
  70.                         char path[100];
  71.                         sprintf(path,"/work/myproject/study/photo/get/%s",input);
  72.                         printf("the path is %s\n",path);
  73.                         in=fopen(path,"w");
  74.                         printf("after path\n");
  75.                         char photo[10];
  76.                         while(1)
  77.                         {
  78.                                 memset(photo,0,10);
  79.                                 res=read(new_fd,photo,10);
  80.                                 if(res==0)
  81.                                         return 0;
  82.                                 photo[res]=0;
  83.                                 int d=atoi(photo);
  84.                                 printf("data %d\n",d);
  85.                                 fputc(d,in);
  86.                         }
  87.                 }
  88.         }
  89.     close(socket_fd);
  90.     close(new_fd);

  91. }

client端:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <string.h>

  7. #define SERVER_IP "127.0.0.1"
  8. #define PORT 6675

  9. int main()
  10. {
  11.         int socket_fd;
  12.     socket_fd=socket(AF_INET,SOCK_STREAM,0);

  13.     struct sockaddr_in server_addr;
  14.         memset(&server_addr,0,sizeof(server_addr));
  15.         server_addr.sin_family=AF_INET;
  16.         server_addr.sin_port=htons(PORT);
  17.         server_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
  18.         memset(&(server_addr.sin_zero),0,8);

  19.         int res =connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
  20.         printf("connect res is %d\n",res);
  21.         
  22.         /*
  23.         while(1)
  24.         {
  25.                 printf("send send ---->\n");
  26.                 char input[100];
  27.                 char output[100];

  28.                 memset(input,0,sizeof(input));
  29.                 gets(input);
  30.                 res=write(socket_fd,input,strlen(input));
  31.                 printf("the write resoult is %d\n",res);
  32.                 
  33.                 res=read(socket_fd,output,100);
  34.                 output[res]=0;
  35.                 printf("server says:'%s'\n",output);
  36.                 sleep(1);
  37.         }
  38.         */
  39.         char path[100];
  40.         printf("please give the path of the photo\n");
  41.         char input[100];
  42.         gets(input);
  43.         sprintf(path,"/work/myproject/study/photo/%s",input);
  44.         write(socket_fd,input,strlen(input));

  45.         FILE *out=fopen(path,"r");
  46.         FILE *new=fopen("/work/myproject/study/photo/get/new.jpg","w");
  47.         int c;
  48.         sleep(1);
  49.         while((c=fgetc(out))!=EOF)
  50.         {
  51.      char photo[100];
  52.         sprintf(photo,"%d",c);
  53.         int b=atoi(photo);
  54.         fputc(b,new);
  55.                 printf("data %d\n",b);
  56.                 write(socket_fd,photo,strlen(photo));
  57.                 usleep(1000);
  58.     }
  59.         char *end="#";
  60.         write(socket_fd,end,strlen(end));
  61.         close(socket_fd);
  62. }
阅读(2001) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~