日志服务器的main函数

原创 2006年05月25日 01:00:00

线程的执行体为了便于观察我修改了一下

#include <ace/Thread_Manager.h>
#include <ace/INET_Addr.h>
#include <ace/SOCK_Acceptor.h>
#include <ace/SOCK_Stream.h>
#include <ace/Guard_T.h>
#include <ace/Thread_Mutex.h>
#include <iostream>
using namespace std;
#define LOG_RECORD_MAX 100
// At file scope.

// Keep track of number of logging requests.

static int request_count;


// Manage threads in this process.

static ACE_Thread_Manager thr_mgr;

// Lock to protect request_count.

static ACE_Thread_Mutex lock;

ACE_THR_FUNC_RETURN logging_handlerd(void *arg);
// Forward declaration.

static ACE_THR_FUNC logging_handler=logging_handlerd;
//static int (*logging_handler)(void *);

// Port number to listen on for requests.

static const int logging_port = 10000;

// Main driver function for the multi-threaded

// logging server. Some error handling has been

// omitted to save space in the example.

int main (int argc, char *argv[])

{

 // Internet address of server.

 ACE_INET_Addr addr (logging_port);

 // Passive-mode acceptor object.

 ACE_SOCK_Acceptor server(addr);

 ACE_SOCK_Stream new_stream;

 // Wait for a connection from a client.

 for (;;)

 {

  // Accept a connection from a client.

  server.accept (new_stream);

  // Get the underlying handle.

  ACE_HANDLE h = new_stream.get_handle ();


  // Spawn off a thread-per-connection.
  thr_mgr.spawn (logging_handler,

   reinterpret_cast <void *> (h),

   THR_DETACHED);

 }

}

// Entry point that processes logging records for

// one client connection.

ACE_THR_FUNC_RETURN logging_handlerd(void *arg)
{
 cout<<"a new connection from a client";

 ACE_HANDLE h = reinterpret_cast <ACE_HANDLE> (arg);

 // Create a <SOCK_Stream> object from SOCKET <h>.

 ACE_SOCK_Stream stream (h);

 for (;;)

 {

  int len;// Ensure a 32-bit quantity.

  char log_record[LOG_RECORD_MAX];

  // The first <recv_n> reads the length

  // (stored as a 32-bit integer) of

  // adjacent logging record. This code

  // handles "short-<recv>s".

  /*ssize_t n = stream.recv_n

   (reinterpret_cast <char *> (&len),

   sizeof len);*/
  for (int r_bytes = 0;;)

  {

   r_bytes =stream.recv (log_record, sizeof log_record);

   if (r_bytes > 0)

    write (1, log_record, r_bytes);

   else

    break;

  }

  // Bail out if we’re shutdown or

  // errors occur unexpectedly.

  //if (n <= 0) break;

  //len = ntohl (len); // Convert byte-ordering.

  //if (len > LOG_RECORD_MAX) break;

  // The second <recv_n> then reads <len>

  // bytes to obtain the actual record.

  // This code handles "short-<recv>s".

  //n = stream.recv_n (log_record, len);

  // Bail out if we’re shutdown or

  // errors occur unexpectedly.

  /*if (n <= 0) break;
        cout<<log_record;*/

  //{

  // // Constructor of Guard automatically

  // // acquires the lock.

  // ACE_Guard<ACE_Thread_Mutex> mon (lock);

  // // Execute following two statements in a

  // // critical section to avoid race conditions

  // // and scrambled output, respectively.

  // ++request_count; // Count # of requests

  // if (write (n, log_record, len) == -1)

  //  break;

  // // Destructor of Guard automatically

  // // releases the lock, regardless of

  // // how we exit this block!

  //}

 }

 // Destructor of <stream> automatically

 // closes down <h>.

 return 0;

}

java普通项目log4j普通main函数写入日志

我们常见log4j用于java的web项目中记录日志,只需利用框架的各种声明配置,就可以直接调用。与此同时log4j也可应用于一些非web项目,此时需要我们引入具体的log4j的配置文件。...

一个简单的wed服务器SHTTPD(9)————main函数文件,Makefile,头文件

主函数:#include "lcw_shttpd.h"//初始化时服务器的默认配置 extern struct conf_opts conf_para= { "/usr/local/var/w...

MDK main函数运行前的分析

  • 2014年10月24日 08:27
  • 178KB
  • 下载

main函数源码

  • 2015年08月06日 16:00
  • 693B
  • 下载

spring boot学习1之main和tomcat启动及log日志

写了个spring boot例子,的确是零配置,平时需要写个web例子测试之类的时候,用它还挺方便的。现在开发都搞微服务,api端之前都是dubbo或jesry,现在也可以用spring boot了。...

main函数的相关应用

  • 2012年12月27日 17:47
  • 401B
  • 下载

C例子:main函数的参数

  • 2015年11月04日 21:12
  • 374B
  • 下载

c2-3-main-frm_102901-13100渲染日志

--- Rendering "c2-3-main-frm_12901.png" --- Size: 1024x768 Quality: 200 Oversample: 2, Filt...

游戏main函数输入控制

  • 2013年08月08日 17:14
  • 1KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:日志服务器的main函数
举报原因:
原因补充:

(最多只允许输入30个字)