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;
}