Chinaunix首页 | 论坛 | 博客
  • 博客访问: 361065
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-09-07 14:52:38

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
阅读(2631) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~