随笔 - 7  文章 - 6  trackbacks - 0
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(1)

随笔档案

文章分类

搜索

  •  

积分与排名

  • 积分 - 32746
  • 排名 - 601

最新评论

阅读排行榜

评论排行榜

To understand the backlog argument, we must realize that for a given listening socket, the kernel maintains two queues :
要明白backlog参数的含义,我们必须明白对于一个listening socket,kernel维护者两个队列:

1.An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which the server is awaiting completion of the TCP three-way handshake. These sockets are in the SYN_RCVD state .
1.一个未完成连接的队列,此队列维护着那些已收到了客户端SYN分节信息,等待完成三路握手的连接,socket的状态是SYN_RCVD

2.A completed connection queue, which contains an entry for each client with whom the TCP three-way handshake has completed. These sockets are in the ESTABLISHED state
2.一个已完成的连接的队列,此队列包含了那些已经完成三路握手的连接,socket的状态是ESTABLISHED

The backlog argument to the listen function has historically specified the maximum value for the sum of both queues.
backlog参数历史上被定义为上面两个队列的大小之和

Berkeley-derived implementations add a fudge factor to the backlog: It is multiplied by 1.5
Berkely实现中的backlog值为上面两队列之和再乘以1.5

When a SYN arrives from a client, TCP creates a new entry on the incomplete queue and then responds with the second segment of the three-way handshake: the server's SYN with an ACK of the client's SYN (Section 2.6). This entry will remain on the incomplete queue until the third segment of the three-way handshake arrives (the client's ACK of the server's SYN), or until the entry times out. (Berkeley-derived implementations have a timeout of 75 seconds for these incomplete entries.)
当客户端的第一个SYN到达的时候,TCP会在未完成队列中增加一个新的记录然后回复给客户端三路握手中的第二个分节(服务端的SYN和针对客户端的ACK),这条记录会在未完成队列中一直存在,直到三路握手中的最后一个分节到达,或者直到超时(Berkeley时间将这个超时定义为75秒)

If the queues are full when a client SYN arrives, TCP ignores the arriving SYN (pp. 930–931 of TCPv2); it does not send an RST. This is because the condition is considered temporary, and the client TCP will retransmit its SYN, hopefully finding room on the queue in the near future. If the server TCP immediately responded with an RST, the client's connect would return an error, forcing the application to handle this condition instead of letting TCP's normal retransmission take over. Also, the client could not differentiate between an RST in response to a SYN meaning "there is no server at this port" versus "there is a server at this port but its queues are full."
如果当客户端SYN到达的时候队列已满,TCP将会忽略后续到达的SYN,但是不会给客户端发送RST信息,因为此时允许客户端重传SYN分节,如果返回错误信息,那么客户端将无法分清到底是服务端对应端口上没有相应应用程序还是服务端对应端口上队列已满这两种情况

posted @ 2010-02-07 19:43 许海斌 阅读(18740) | 评论 (2)编辑 收藏

        今天看到有人在问这个问题,写了下代码,标准库分离了算法和数据结构,按照这个框架写程序确实比较方便,个人认为熟读和透彻理解标准库源码是每个想成为资深c++程序员的必修课,就框架结构而论,stl很好的分离了算法和数据结构,就算法而论,标准库里有很多常见算法的经典实现,所以有非常高的研究价值。

#include <iostream>
#include 
<stddef.h>
#include 
<stdlib.h>
#include 
<string>
#include 
<iterator>
#include 
<algorithm>
#include 
<vector>

using namespace std;

template 
<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator delete_intersection(InputIterator1 first1, InputIterator1 last1, 
         InputIterator2 first2, InputIterator2 last2, OutputIterator dest) 
{
    
while (first1 != last1 && first2 != last2) {
        
if (*first1 > *first2) {
            
*dest = *first2;
            
++first2;
            
++dest;
        }
 else if (*first1 < *first2) {
            
*dest = *first1;
            
++first1;
            
++dest;
        }
 else {
            
++first1;
            
++first2;
        }

    }


    
for (;first2 != last2; ++first2) *dest = *first2;

    
return dest;
}


int main() {
    
int a[] = {1,1,2,2,5,6,9,9};
    
int b[] = {1,2,3,4,4,6,7,8,9,9,9,10};

    vector
<int> vc;

    delete_intersection(a, a 
+ sizeof(a)/sizeof(a[0]), b, b + sizeof(b)/sizeof(b[0]), back_inserter(vc));

    std::copy(a, a 
+ sizeof(a)/sizeof(a[0]), ostream_iterator<int>(cout, ",")); 
        cout 
<< endl;

    std::copy(b, b 
+ sizeof(b)/sizeof(b[0]), ostream_iterator<int>(cout, ",")); 
        cout 
<< endl;

    std::copy(vc.begin(), vc.end(), ostream_iterator
<int>(cout, ",")); 
        cout 
<< endl;

    ::system(
"PAUSE");
    
return EXIT_SUCCESS;

}
posted @ 2009-03-05 18:56 许海斌 阅读(1044) | 评论 (4)编辑 收藏
boost的线程库不能强行终止,所以通过time_wait可以让其自然的结束

 1
 #include <iostream>
 2 #include <boost/thread/thread.hpp>
 3 #include <boost/thread/mutex.hpp>
 4 #include <boost/thread/condition.hpp>
 5 #include <boost/date_time/posix_time/posix_time.hpp>
 6 
 7 using namespace std;
 8 using namespace boost;
 9 
10 boost::mutex test_mutex;
11 boost::condition_variable test_condition;
12 
13 void test() {
14 
15     for (;;) {
16         
17         boost::mutex::scoped_lock lock(test_mutex);
18         if (test_condition.timed_wait(lock, get_system_time() + posix_time::seconds(3))) {
19             cout << "成功接收到通知" << endl; //这里加个break就结束了
20         } else {
21             cout << "没有等待到通知" << endl;
22         }
23 
24     }
25 }
26 
27 int main() {
28 
29     boost::thread test_thread(test);
30 
31     for (;;) {
32         ::system("PAUSE");
33         cout << "开始发送通知" << endl;
34         test_condition.notify_one();
35     }
36 
37 }

posted @ 2009-02-19 17:18 许海斌 阅读(5070) | 评论 (0)编辑 收藏
在泛型编程中,对于一个concept的一系列modeling,尤其是这些modeling中还有c++基本类型的时候,如果要抽取其特性,可以用一个traits类来解决,traits的名称可命名为concept名称_traits,对于其中的基本类型的traits可以用类模板的偏特化实现。
posted @ 2009-02-09 17:59 许海斌 阅读(1527) | 评论 (0)编辑 收藏

实现代码:

    typedef enum { UNIQUE_LOCK, SHARED_LOCK } LockType;

template<LockType lt>
class shared_lock;

template 
<>
class shared_lock<UNIQUE_LOCK>private boost::noncopyable {
public:
    shared_lock(boost::shared_mutex
& sm): sm_(sm) sm_.lock(); }
    
~shared_lock() { sm_.unlock(); }
private:
    boost::shared_mutex
& sm_;
}
;

template 
<>
class shared_lock<SHARED_LOCK>private boost::noncopyable {
public:
    shared_lock(boost::shared_mutex
& sm): sm_(sm) sm_.lock_shared(); }
    
~shared_lock() { sm_.unlock_shared(); }
private:
    boost::shared_mutex
& sm_;
}
;



调用方式:

    boost::shared_mutex session_mutex_;

//独占锁定
shared_lock<UNIQUE_LOCK> lock(session_mutex_);

//共享锁定
shared_lock<SHARED_LOCK> lock(session_mutex_);


http://www.cppblog.com/thisisbin/archive/2009/01/21/72417.html 一文 single-write/multi-read 就可以应用这种RAII风格的共享锁实

posted @ 2009-01-22 10:22 许海斌 阅读(1360) | 评论 (0)编辑 收藏
    程序中几个线程一起往控制台写入log_info,但是可以很清楚的看到begin or write_process不会同时连续的出现,而且在begin of write_process和end of write_process之间肯定不会出现begin of read_process或者end of read_process。但是begin of read_process却会在控制台上连续出现,这说明在同一时刻只有一个write_prcess在运行,但是wrte_process没有运行的时候,会有多个read_process并发运行。

 1 #include <iostream>
 2 
 3 #include <boost/thread/thread.hpp>
 4 #include <boost/thread/shared_mutex.hpp>
 5 
 6 using namespace std;
 7 using namespace boost;
 8 
 9 boost::shared_mutex shr_mutex;
10 
11 /// 这个是辅助类,能够保证log_info被完整的输出
12 class safe_log {
13 public:
14     static void log(const std::string& log_info) {
15         boost::mutex::scoped_lock lock(log_mutex);
16         cout << log_info << endl;
17     }
18 
19 private:
20     static boost::mutex log_mutex;
21 };
22 
23 boost::mutex safe_log::log_mutex;
24 
25 void write_process() {
26     shr_mutex.lock();
27     safe_log::log("begin of write_process");
28     safe_log::log("end of write_process");
29     shr_mutex.unlock();
30 }
31 
32 void read_process() {
33     shr_mutex.lock_shared();
34     safe_log::log("begin of read_process");
35     safe_log::log("end of read_process");
36     shr_mutex.unlock_shared();
37 }
38 
39 int main() {
40 
41     thread_group threads;
42     for (int i = 0; i < 10++ i) {
43         threads.create_thread(&write_process);
44         threads.create_thread(&read_process);
45     }
46 
47     threads.join_all();
48 
49     ::system("PAUSE");
50 
51     return 0;
52 }

posted @ 2009-01-21 13:58 许海斌 阅读(2624) | 评论 (0)编辑 收藏

1、安装stlport,参考文章:http://www.cnblogs.com/DonLiang/archive/2007/10/13/923143.html
如果是DEBUG模式请在项目的属性=>配置属性=>c/c++=>命令行的附加选项增加/D_STLP_DEBUG

2、boost_1_37_0\tools\jam\stage下运行build.bat编译生成bjam.exe并拷贝到boost根目录

3、修改配置文件boost_1_37_0\tools\build\v2\user-config.jam,将两个stlport选项打开

4、vs2005命令提示下编译命令:bjam stdlib=stlport --build-type=complete install,接下来是漫长的等待,大概两个小时以上吧

posted @ 2008-12-09 13:22 许海斌 阅读(1964) | 评论 (0)编辑 收藏
仅列出标题