Chinaunix首页 | 论坛 | 博客
  • 博客访问: 178312
  • 博文数量: 38
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 346
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-04 00:06
文章分类

全部博文(38)

文章存档

2016年(3)

2015年(15)

2014年(16)

2013年(4)

我的朋友

分类: LINUX

2014-03-04 19:30:56

head.h 

点击(此处)折叠或打开

  1. #ifndef _HEAD_H_
  2. #define _HEAD_H_ 1

  3. #define handler_error(msg) do{\
  4.         perror(msg);exit(EXIT_FAILURE);}while(0)

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <pthread.h>
  14. #include <semaphore.h>
  15. #include <signal.h>
  16. #include <sys/ipc.h>
  17. #include <sys/shm.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <dirent.h>
  22. #include <sys/un.h>
  23. #include <sqlite3.h>

  24. #endif
server.c
    

点击(此处)折叠或打开

  1. #include "head.h"


  2. void *do_client(void *arg)
  3. {
  4.     int n;
  5.     char buf[1024];
  6.     int sockfd = * ((int *)arg);
  7.     
  8.     while(1)
  9.     {
  10.         //接受数据
  11.         n = read(sockfd,buf,sizeof(buf) - 1);
  12.         buf[n] = '\0';

  13.         printf("Recv %d bytes : %s.\n",n,buf);

  14.         //发回去
  15.         write(sockfd,buf,n);
  16.         
  17.         if(strncmp(buf,"quit",4) == 0)
  18.             break;

  19.     }
  20.     
  21.     free(arg);

  22.     pthread_exit(NULL);
  23. }


  24. //./server ip port
  25. int main(int argc, const char *argv[])
  26. {
  27.     int pid;
  28.     struct sockaddr_in peer_addr;
  29.     socklen_t addrlen = sizeof(struct sockaddr);
  30.     int listen_fd;
  31.     struct sockaddr_in server_addr;
  32.     
  33.     if(argc < 3)
  34.     {
  35.         fprintf(stderr,"Usage : %s ip port.\n",argv[1]);
  36.         exit(EXIT_FAILURE);
  37.     }
  38.     
  39.      listen_fd = socket(AF_INET,SOCK_STREAM,0);
  40.      if(listen_fd < 0){
  41.          handler_error("socket");
  42.      }

  43.     
  44.      memset(&server_addr,0,sizeof(server_addr));
  45.      server_addr.sin_family = AF_INET;
  46.      server_addr.sin_port = htons(atoi(argv[2]));
  47.      server_addr.sin_addr.s_addr = inet_addr(argv[1]);

  48.      if(bind(listen_fd,(struct sockaddr *)&server_addr,sizeof(server_addr)) < 0)
  49.      {
  50.          handler_error("bind");
  51.      }

  52.      listen(listen_fd,10);

  53.      printf("listen ...\n");
  54.      int ret;
  55.      pthread_t tid;
  56.     
  57.      int *pconnect_fd;


  58.      while(1)
  59.      {    
  60.        

  61.         pconnect_fd = (int *)malloc(sizeof(int));

  62.         *pconnect_fd = accept(listen_fd,(struct sockaddr *)&peer_addr,&addrlen);
  63.         if(connect_fd < 0)
  64.             handler_error("accept");
  65.         
  66.         printf("*****************************\n");
  67.         printf("Peer Ip :%s.\n",inet_ntoa(peer_addr.sin_addr));
  68.         printf("Peer Port:%d.\n",ntohs(peer_addr.sin_port));
  69.         printf("*****************************\n");
  70.         
  71.         //创建线程和客户端进行收发数据
  72.         ret = pthread_create(&tid,NULL,do_client,pconnect_fd);    
  73.         if(ret != 0){
  74.             fprintf(stderr,"Fail to pthread_create : %s.\n",strerror(ret));
  75.             exit(EXIT_FAILURE);
  76.         }

  77.          //让线程结束的时候,系统自动回收未释放的资源
  78.         pthread_detach(tid);
  79.      }

  80.     return 0;
  81. }
client.c


点击(此处)折叠或打开

  1. #include "head.h"

  2. //./client ip port
  3. int main(int argc, const char *argv[])
  4. {
  5.     int n;
  6.     char buf[1024];
  7.     int sockfd;
  8.     struct sockaddr_in server_addr;
  9.  
  10.     sockfd = socket(AF_INET,SOCK_STREAM,0);
  11.     if(sockfd < 0){
  12.         handler_error("socket");
  13.     }

  14.     memset(&server_addr,0,sizeof(server_addr));
  15.     server_addr.sin_family = AF_INET;
  16.     server_addr.sin_port = htons(atoi(argv[2]));
  17.     server_addr.sin_addr.s_addr = inet_addr(argv[1]);
  18.  
  19.     if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)) < 0){
  20.         handler_error("connect");
  21.     }
  22.     
  23.     //connect成功返回,表示三次握手完成
  24.     while(1)
  25.     {
  26.         printf(">");

  27.         fgets(buf,sizeof(buf),stdin);
  28.         buf[strlen(buf) - 1] = '\0';

  29.         write(sockfd,buf,strlen(buf));


  30.         memset(buf,0,sizeof(buf));
  31.         n = read(sockfd,buf,sizeof(buf) - 1);
  32.         buf[n] = '\0';
  33.         
  34.         printf("Recv %d bytes : %s.\n",n,buf);
  35.     }

  36.     return 0;
  37. }
    
        

阅读(2239) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~