Backup code, for Linux

Source: Internet
Author: User
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 >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.