C ++-encapsulated linked list UDP Transmitter

Source: Internet
Author: User
Tags usleep

 


In many cases, the protocol used to send strings on the network is UDP. Here, we use the official UDP library of boost to encapsulate it, and an object is used for sending. Very convenient.

The Code is as follows:

 


StringQueue encapsulation. The mutex Lock is added and implemented with simple variable Lock, which is easy to understand.

The queue object of the String contains operations such as queuing, self-locking, and so on.


Header file:

 

 

[Plain] # include <iostream>
# Include <list>
Using namespace std;
 
 
Class StringQueue
{
Public:
Bool Lock;
List <string> Data;
 
StringQueue (): Lock (0 ){}
~ StringQueue (){}
 
Void Lockdata ();
Void UnLockdata ();
Void WaitUnLock ();
Private:
 
Protected:
 
};

# Include <iostream>
# Include <list>
Using namespace std;


Class StringQueue
{
Public:
Bool Lock;
List <string> Data;

StringQueue (): Lock (0 ){}
~ StringQueue (){}

Void Lockdata ();
Void UnLockdata ();
Void WaitUnLock ();
Private:

Protected:

};

 

CPP file of String Queue:

 


[Plain] void StringQueue: Lockdata ()
{
Printf ("Queue String Locked \ n ");
This-> Lock = 1;
}
Void StringQueue: UnLockdata ()
{
Printf ("Queue String UnLocked \ n ");
This-> Lock = 0;
}
Void StringQueue: WaitUnLock ()
{
While (this-> Lock = 1)
{
Printf ("Queue String Waiting \ n ");
Usleep (200000 );
}
Printf ("Queue String UnLocked \ n ");
}

Void StringQueue: Lockdata ()
{
Printf ("Queue String Locked \ n ");
This-> Lock = 1;
}
Void StringQueue: UnLockdata ()
{
Printf ("Queue String UnLocked \ n ");
This-> Lock = 0;
}
Void StringQueue: WaitUnLock ()
{
While (this-> Lock = 1)
{
Printf ("Queue String Waiting \ n ");
Usleep (200000 );
}
Printf ("Queue String UnLocked \ n ");
}

Next, we will explain the UDP sending object. It is very convenient and easy to use according to the official boost sending example. boost is highly portable and can be used in linux, windows, and other systems.

 


The Code is as follows:

 


Header file:

 

 

[Plain] # include <stdio. h>
# Include <stdlib. h>
# Include <iostream>
# Include <list>
Using namespace std;
 
Typedef unsigned int uint;
 
 
Class UDPSender
{
Public:
UDPSender (StringQueue Data, string DspthDst, string DspPort );
 
~ UDPSender (){}
Int main_UDP_Send (char * Data, uint Len, char * UDPdst, char * port );
Private:
StringQueue & Data _;
Protected:
 
};

# Include <stdio. h>
# Include <stdlib. h>
# Include <iostream>
# Include <list>
Using namespace std;

Typedef unsigned int uint;


Class UDPSender
{
Public:
UDPSender (StringQueue Data, string DspthDst, string DspPort );

~ UDPSender (){}
Int main_UDP_Send (char * Data, uint Len, char * UDPdst, char * port );
Private:
StringQueue & Data _;
Protected:

};

 

CPP file:

 

 

[Plain] UDPSender: UDPSender (StringQueue Data, string DspthDst, string DspPort): Data _ (Data)
{
 
While (Data _. Data. size ()> 0)
{
Printf ("!!!!!!! UDP sending % d ", Data _. Data. size ());
String Sendbuffer = Data. Data. front ();
Main_UDP_Send (char *) Sendbuffer. c_str (), Sendbuffer. length (), (char *) DspthDst. c_str (), (char *) DspPort. c_str ());
Data. Data. pop_front ();
}
 
}
Int UDPSender: main_UDP_Send (char * Data, uint Len, char * UDPdst, char * port)
{
Try
{
Boost: asio: io_service;
 
Udp: socket s (io_service, udp: endpoint (udp: v4 (), 0 ));
 
Udp: resolver (io_service );
Udp: resolver: query (udp: v4 (), UDPdst, port );
Udp: resolver: iterator = resolver. resolve (query );
 
S. send_to (boost: asio: buffer (Data, Len), * iterator );
 
}
Catch (std: exception & e)
{
Std: cerr <"Exception:" <e. what () <"\ n ";
}
 
Return 0;
}

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.