ace(adaptive communication environment),它是乙個物件導向的、跨平台的、開放原始碼的網路程式設計基礎設施框架。
ace 是由加利福尼亞大學irvine分校的douglas c.schmidt 博士主導開發的,是一種跨平台可編譯的網路程式設計api,並隨後在工業界中發展、完善,它將底層的不同系統的細節透明化,為開發者提供統一的介面,從而可在不同系統上相互移植,提高**利用率。與傳統的網路程式設計相比,它更加規範化,極力避免工業開發中人們常會出現的錯誤情況,從而提高了程式的可靠性和易用性,並且擁有較高的效能優勢。
本文實現了ace的乙個簡單多執行緒伺服器,主要是通過多執行緒處理併發能力。源**這裡
跟傳統的socket程式設計一樣,我們也需要在while迴圈中監聽埠,並分配執行緒進行處理,程式的標頭檔案定義如下:
定義好資料結構後,就可以實現具體的邏輯實現了:#include "ace/sock_stream.h"
#include "ace/sock_acceptor.h"
class ace_server
ace_server *ls;
ace_sock_stream peer;
};virtual int handle_connections();
virtual int handle_data(ace_sock_stream* arg = 0);
int block_recv(ace_sock_stream* peer, char* recv_buf, size_t len);
void ace_server::gettime(char* result);
void ace_server::getlocalip(char* result);
void getcalc(int param, char* result, int count, char* re_buf);
static ace_thr_func_return thread_run(void *arg);
private:
ace_sock_acceptor acceptor;
ace_inet_addr server_addr;
//request catch size
static const int max_buf = 2048;
//response catch size
static const int max_result = 64;
//network port
static const int port = 20120;
};
#include "ace_server.h"
#include "ace/thread_manager.h"
#include "ace/auto_ptr.h"
int ace_server::run_svc()
printf("it is listening the port 20120...\n");
if (acceptor.open(server_addr) == -1)
for(;;)
return acceptor.close();
}ace_thr_func_return ace_server::thread_run(void *arg)
thread_args->peer.close();
return 0;
}int ace_server::handle_connections()
//the core of allocating the sub thread
if(ace_thread_manager::instance()->spawn(
ace_server::thread_run,//run the thread_run() upon every sub thread
static_cast(thread_args.get()),
thr_detached | thr_scope_system) == -1)
//it is not necessary to wait for all of sub_threads exiting.
//ace_thread_manager::instance()->wait();
//it is necessary to release the pointer binding with thread_run().
thread_args.release();
return 0;
}int ace_server::block_recv(ace_sock_stream* peer, char* recv_buf, size_t len)
return recvd_size;
}void ace_server::gettime(char* result)
}void ace_server::getlocalip(char* result)
if (size < max_result)
strncpy(result, addr.c_str(), addr.length());
else
printf("result is overflow.");
}}void ace_server::getcalc(int begin, char* result, int length, char* re_buf)
else if (re_buf[i] == '&')
if (isvalue)
}sum += atoi(value.c_str());
value = "";
}sprintf(result, "%d", sum);
}int ace_server::handle_data(ace_sock_stream * peer)
if (params == 0)
}if (strcmp(querys.c_str(), "at") == 0)
else if(strcmp(querys.c_str(), "ip") == 0)
else if(strcmp(querys.c_str(), "calc") == 0)
else if(strcmp(querys.c_str(), "json") == 0)
]";memcpy(result, (char*)json.c_str(), json.length()+1);
iov[1].iov_base = (char*)json.c_str();
iov[1].iov_len = json.length();
if (peer->sendv_n(iov, 2) == -1)
if (peer->send_n(result, max_result) == -1)
}else
printf("the wrong query:: %s\n", re_buf);
}printf(result);
printf("\n-----------------------------------------------\n");
if (peer->send_n(result, max_result) == -1)
delete re_buf;
delete result;
re_buf = nullptr;
result = nullptr;
return 0;
}int main(int argc, char *argv)
伺服器獲得如下結果:
小結:ace目前在工業界運用廣泛,本文只是對ace的多執行緒使用的乙個簡單實現,更多功能仍需探索和學習。
以後如有更多關於ace的個人理解,會積極和大家分享。
多執行緒伺服器
posix執行緒庫 a 與執行緒有關的函式都構成了乙個完整的系列,絕大多數函式的名字都是以 pthread 打頭的。b 要使用這些庫函式,要引入標頭檔案。c 鏈結這些執行緒庫函式時要使用編譯命令的 lpthread 選項。int pthread create pthread t thread,con...
多執行緒時間伺服器
include stdafx.h include include include include include define port 8080 int x 1 void error char str dword winapi new client proc lpvoid lpparam prin...
多執行緒的伺服器
許多實際應用要求伺服器具有同時為多個客戶提供服務的能力。用多個執行緒來同時為多個客戶提供服務,這是提高伺服器的併發效能的最常用的手段。主要有3種方式來實現 第一種方式的不足之處 執行緒池為執行緒生命週期開銷問題和系統資源不足問題提供了解決方案。執行緒池中預先建立了一些工作執行緒,它們不斷從工作佇列中...