Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1126036
  • 博文数量: 188
  • 博客积分: 1156
  • 博客等级: 少尉
  • 技术积分: 2173
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-16 15:01
个人简介

go!go!go!

文章分类

全部博文(188)

文章存档

2024年(2)

2023年(11)

2022年(13)

2021年(15)

2020年(38)

2019年(3)

2018年(6)

2016年(1)

2015年(16)

2014年(13)

2013年(24)

2012年(46)

分类: C/C++

2015-07-05 09:15:04


  1. [test@localhost ~/src/short-url]# cat select.cpp
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>
  9. #include <stdio.h>


  10. #include <iostream>


  11. /*
  12.  * support http find/insert/delete short url
  13.  * find: client send http request like curl -I t.cn/111
  14.  * */
  15. int main(int argc, char **argv)
  16. {
  17.     //1. listen socket
  18.     int listen_fd;
  19.     listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  20.     if (listen_fd < 0)
  21.     {
  22.         std::cout<<"socket create failed:"<<strerror(errno)<<std::endl;
  23.         exit(1);
  24.     }
  25.     int option_value=1;
  26.     if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &option_value, sizeof(option_value)) < 0)
  27.     {
  28.         std::cout<<"socket setsockopt failed:"<<strerror(errno)<<std::endl;
  29.         exit(1);
  30.     }

  31.     //2. process request
  32.     
  33.     struct sockaddr_in server_addr, client_addr;
  34.     bzero(&server_addr, sizeof(server_addr));
  35.     server_addr.sin_family = AF_INET;
  36.     server_addr.sin_port = htons(9876);
  37.     server_addr.sin_addr.s_addr = INADDR_ANY;

  38.     if (bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
  39.     {
  40.         std::cout<<"socket bind failed:"<<strerror(errno)<<std::endl;
  41.         exit(1);
  42.     }

  43.     if (listen(listen_fd, 128) < 0)
  44.     {
  45.         std::cout<<"socket listen failed:"<<strerror(errno)<<std::endl;
  46.         exit(1);
  47.     }

  48.     char buffer[256];
  49.     int client_fd;
  50.     int len;
  51.     socklen_t clilen;
  52.     clilen = sizeof(client_addr);
  53.     while (1)
  54.     {
  55.         bzero(buffer, sizeof(buffer));
  56.         if ((client_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &clilen)) < 0)
  57.         {
  58.             std::cout<<"socket accept failed:"<<strerror(errno)<<std::endl;
  59.             exit(1);
  60.         }

  61.         if ((len = read(client_fd, buffer, sizeof(buffer))) < 0)
  62.         {
  63.             std::cout<<"socket read failed:"<<strerror(errno)<<std::endl;
  64.             exit(1);
  65.         }
  66.         std::cout<<"ip:"<<inet_ntoa(client_addr.sin_addr)<<" port:"<<client_addr.sin_port<<" msg:"<<buffer<<std::endl;

  67.         bzero(buffer, sizeof(buffer));
  68.         snprintf(buffer, sizeof(buffer), "At %d I got you message!", time(NULL));
  69.         if ((len = write(client_fd, buffer, strlen(buffer))) < 0)
  70.         {
  71.             std::cout<<"socket write failed:"<<strerror(errno)<<std::endl;
  72.             exit(1);
  73.         }
  74.         close(client_fd);
  75.     }

  76.     return 0;
  77. }

阅读(1036) | 评论(0) | 转发(0) |
0

上一篇:golang学习笔记

下一篇:一个推流软件

给主人留下些什么吧!~~