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

全部博文(403)

文章存档

2016年(3)

2015年(16)

2014年(163)

2013年(222)

分类: LINUX

2013-12-31 16:46:13

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 6679

  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.                 //while(1)
  51.                 {
  52.                         FILE *in;
  53.                         char input[100];
  54.                         res=read(new_fd,input,100);
  55.                         input[res]=0;
  56.                         char path[100];
  57.                         sprintf(path,"/work/myproject/study/photo/get/%s",input);
  58.                         printf("the path is %s\n",path);
  59.                         in=fopen(path,"w");
  60.                         printf("after path\n");
  61.                         char photo[200];
  62.                         while(1)
  63.                         {
  64.                                 res=read(new_fd,photo,200);
  65.                                 if(res==0)
  66.                                  return 0;
  67.                 fwrite(photo,sizeof(char),res,in);
  68.                         }
  69.                 }
  70.         }
  71.     close(socket_fd);
  72.     close(new_fd);

  73. }

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 6679

  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.         char path[100];
  23.         printf("please give the path of the photo\n");
  24.         char input[100];
  25.         gets(input);
  26.         sprintf(path,"/work/myproject/study/photo/%s",input);
  27.     write(socket_fd,input,strlen(input));

  28.         FILE *out=fopen(path,"r");
  29.         sprintf(path,"/work/myproject/study/photo/copy_%s",input);
  30.         FILE *new=fopen(path,"w");
  31.         usleep(1000);
  32.     int nread;
  33.     char buff[100];
  34.     while((nread=fread(buff,sizeof(char),100,out))>0)
  35.     {
  36.         fwrite(buff,sizeof(char),nread,new);
  37.         write(socket_fd,buff,nread);    
  38.     }
  39.         close(socket_fd);
  40. }

Makefile:

  1. all: server client
  2. server: server.c
  3.     gcc server.c -o server
  4. client: client.c
  5.     gcc client.c -o client
  6. clean:
  7.     rm -rf server client
阅读(1392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~