Here I write some Code It is a summary in recent work. It is not a part and cannot be compiled separately, but it may be used in future programming, so it is recorded here.
1. Use the boost library to create a thread.
The following two threads are created. The boost library allows passing parameters to the specified thread function. You can use the boost: BIND () function to specify the thread function parameters.
Void Fun1 ();
Void Fun2 ( String Str_val );
String Str_1;
Boost: thread thrd1 ( & Fun1 );
Boost: thread thrd2 (boost: BIND ( & Fun2, str_1 ));
Thrd1.join ();
Thrd2.join ();
2. Capture packets from a network interface and use Libpcap. Buf_u_int32 mask, net;
Struct Pcap_pkthdr header;
Const Char * Packet;
Pcap_t * Handle = NULL;
Struct Bpf_program FP;
Void Got_packet (u_char * ARGs, Const Pcap_pkthdr * Header,
Const U_char * Packet)
{
//..
}
If (Pcap_lookupnet ( " Eth0 " , & Net, & Mask, errbuf) ! =- 1 )
{
Handle = Pcap_open_live ( " Eth0 " , Bufsiz, 1 , 1000 , Errbuf );
If (Handle)
{
If (Pcap_compile (handle, & FP, " IP " , 0 , Net) ! =- 1 )
If (Pcap_setfilter (handle, & FP) ! =- 1 )
{
Pcap_loop (handle,0, Got_packet, null );
}
}
}
3. Get the current date and time value Size_t getlocaltime ( Char * Time_format)
{
Time_t time_ts;
Struct TM * Local_time_tm;
Sizt_t time_size;
If (Time_ts = Time ( & Time_ts )) ! =- 1 )
{
Local_time_tm = Localtime ( & Time_ts );
If (Time_size = Strftime (time_format, max_time_len, " % F % x % Z " , Local_time_tm )) ! = 0 )
Return Time_size;
}
Return 0 ;< BR >}