发布于 2017-06-06 23:51:21 | 150 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

tcpdaemon TCP连接管理

tcpdaemon是类似于xinetd的TCP守护服务,它封装提供了各种常见TCP连接管理模型(即时派生、预派生进程池等),使用者只需编写TCP通讯数据交换协议和应用处理代码,就能快速完整构造自己的TCP应用服务器。


1.2.2        2017-06-06        calvin
* 封装更多的IO多路复用代码进tcpdaemon,使用者用tcpmain返回值设置等待事件

* IO多路复用模型新增超时机制

----------------------------------------------------

1.概述

tcpdaemon是一个TCP通讯服务端平台/库,它封装了众多常见服务端进程/线程管理和TCP连接管理模型(Forking、Leader-Follow、IO-Multiplex、WindowsThreads Leader-Follow),使用者只需加入TCP通讯数据收发和应用逻辑代码就能快速构建出完整的TCP应用服务器。

...

2.使用示例

2.1.服务模型Forking,链接模式1,接收HTTP请求报文然后发送HTTP响应报文

使用者只需编写一个函数tcpmain,实现同步的接收HTTP请求报文然后发送HTTP响应报文回去

$ vi test_callback_http_echo.c  #include "tcpdaemon.h"    _WINDLL_FUNC int tcpmain( struct TcpdaemonServerEnvirment *p_env , int sock , void *p_addr )  {      char    http_buffer[ 4096 + 1 ] ;      long    http_len ;      long    len ;        /* 接收HTTP请求 */      memset( http_buffer , 0x00 , sizeof(http_buffer) );      http_len = 0 ;      while( sizeof(http_buffer)-1 - http_len > 0 )      {          len = RECV( sock , http_buffer + http_len , sizeof(http_buffer)-1 - http_len , 0 ) ;          if( len == 0 )              return TCPMAIN_RETURN_CLOSE;          if( len == -1 )              return TCPMAIN_RETURN_ERROR;          if( strstr( http_buffer , "rnrn" ) )              break;          http_len += len ;      }      if( sizeof(http_buffer)-1 - http_len <= 0 )      {          return TCPMAIN_RETURN_ERROR;      }        /* 发送HTTP响应 */      memset( http_buffer , 0x00 , sizeof(http_buffer) );      http_len = 0 ;      http_len = sprintf( http_buffer , "HTTP/1.0 200 OKrnContent-length: 17rnrnHello Tcpdaemonrn" ) ;      SEND( sock , http_buffer , http_len , 0 );        return TCPMAIN_RETURN_CLOSE;  }

编译链接成test_callback_http_echo.so

$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -I/home/calvin/include/tcpdaemon  -c test_callback_http_echo.c  $ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -shared -o test_callback_http_echo.so test_callback_http_echo.o -L. -L/home/calvin/lib -lpthread -ldl

用tcpdaemon直接挂接即可

$ tcpdaemon -m IF -l 0:9527 -s test_callback_http_echo.so -c 10 --tcp-nodelay --logfile $HOME/log/test_callback_http_echo.log --loglevel-debug

可执行程序tcpdaemon所有命令行参数可以不带参数的执行而得到

$ tcpdaemon  USAGE : tcpdaemon -m IF -l ip:port -s so_pathfilename [ -c max_process_count ]                    -m LF -l ip:port -s so_pathfilename -c process_count [ -n max_requests_per_process ]          other options :                    -v                    [ --daemon-level ]                    [ --work-path work_path ]                    [ --work-user work_user ]

执行后可在$HOME/log下可以看到tcpdaemon的日志。

通过curl发测试请求

$ curl "http://localhost:9527/"  Hello Tcpdaemon

测试成功!

所有代码在源码安装包的test目录里下可以找到

...

源码托管地址 : http://git.oschina.net/calvinwilliams/tcpdaemon



历史版本 :
TCP 通讯服务端平台/库 tcpdaemon 更新至 v1.2.2
tcpdaemon 更新至 v1.2.1 , TCP 通讯服务端平台/库
tcpdaemon 更新至 v1.2.1 , TCP 通讯服务端平台/库
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务