I have learned a lot about the development of proxy servers during this time.UDPThe implementation of the proxy service is also convenient for me to refer to later.
I. Communication Model
1Non-proxy Communication Model
This is typicalC-SCommunication Model. The client interacts with the server directly.
2, Proxy Communication Model
In this case, the server does not directly interact with the client. Instead, the proxy server forwards the requests sent from the client to the server and returns the service response to the client.
II, UDP Servers and clients Demo
The communication model is analyzed above. Here we provideEchoServer and clientCodeFor later use.
1, ServerDemo
Here isPythonImplementedEchoThe Code is as follows:
# ! /Usr/bin/Python # A simple UDP Server Import Socket, tracebacks = Socket. socket (socket. af_inet, socket. sock_dgram) S. setsockopt (socket. sol_socket, socket. so_reuseaddr, 1 ) S. BIND (( " 192.168.1.100 " , 12345 )) While True: Try : Message, address = S. recvfrom (1024 ) Print " Got data from " , Address Print Message S. sendto (message, address) Except (Keyboardinterrupt, systemexit ): Raise Except : Traceback. print_exc ()
The server outputs the received data and sends it to the client.
2ClientDemo
# ! /Usr/bin/Python # A simple UDP client Import Socket, timeclient = Socket. socket (socket. af_inet, socket. sock_dgram) client. Connect (( ' 192.168.1.100 ' , 12345)) While True: client. sendall ( ' 3 ' ) Print Time. Time (), ' : Send success ' Print Time. Time (), " : " , Client. Recv (1024)) Time. Sleep ( 3 ) # Client. Close ()
The client sends data to the server and receives the data returned by the server.
III, UDP Proxy Server Demo
Here we useBoostLibrary implementation,ASIO.
1Member variables and parameter descriptions
Class Name:M_udpproxyserver
Private member:
IP: UDP: Socket downstream_socket _;//Proxy server and client connection
IP: UDP: Socket upstream_socket _;//Proxy Server and remote server (Echo server).
String _ remotehost ;//Remote Server (Echo server)IPAddress
Int _ remoteport ;//Remote Server (Echo server).
IP: UDP: endpoint downstream_remoteudpendpoint ;//Client Information
IP: UDP: endpoint upstream_remoteudpendpoint ;//Server Information
Unsigned char downstream_data _ [max_data_length]; //Upload LinkBuffer
Unsigned char upstream_data _ [max_data_length]; //Download linkBuffer
2Start local services
In this model, the proxy server acts as both the client server and the remote server (Echo serverSo the proxy server must have a local listening port for the client to connect, but also to the remote server (Echo server) Initiate a link to forward data from the client.
View code
M_udpproxyserver (io_service & Io, Const String & Localhost, Const Int & Localport, Const String & Remotehost, Const Int & Remoteport): downstream_socket _ (Io, IP: UDP: endpoint (IP: UDP: V4 (), localport )), // Start local services Upstream_socket _ (IO ), // Initialize the socket connecting to the remote server _ Remotehost (remotehost), _ remoteport (remoteport), upstream_remoteudpendpoint (IP: address_v4: from_string (_ remotehost), _ remoteport) {start_downstream_receive ();}
3Connect to the remote server
After receiving data from the client, the proxy server is connected to the remote server.
View code
// Receive data from the client Void Start_downstream_receive (){ // If the client data is received, the link to the server is triggered. Downstream_socket _. async_receive_from (boost: ASIO: buffer (downstream_data _, max_data_length), downstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: upstream_connect, This , Boost: ASIO: placeholders: bytes_transferred, boost: ASIO: placeholders: Error ));} // Connect to a remote server Void Upstream_connect ( Const Size_t & Bytes_transferred, Const Boost: System: error_code & Error ){ If (! Error) {upstream_socket _. async_connect (upstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: handle_upstream_connect, This , Bytes_transferred, boost: ASIO: placeholders: Error ));} Else {STD: cerr < " Error: " <Error. Message () < STD: Endl ;}}
4, Data forwarding
Forward the received data from the client to the remote server, receive the returned data from the remote server, and forward it to the client.
View code
Void Handle_upstream_connect ( Const Size_t & Bytes_transferred, Const Boost: System: error_code & Error ){ // Forward the data received from the client to the remote server Upstream_socket _. async_send_to (boost: ASIO: buffer (downstream_data _, bytes_transferred), upstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: handle_upstream_send, This , Boost: ASIO: placeholders: Error ));} Void Handle_upstream_send ( Const Boost: System: error_code & Error ){ If (! Error ){ // Receive returned data from the server Upstream_socket _. async_receive_from (boost: ASIO: buffer (upstream_data _, max_data_length), upstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: handle_upstream_receive, This , Boost: ASIO: placeholders: bytes_transferred, boost: ASIO: placeholders: Error ));} Else {STD: cerr < " Error: " <Error. Message () < STD: Endl ;}} Void Handle_upstream_receive ( Const Size_t & Bytes_transferred, Const Boost: System: error_code & Error ){ If (! Error ){ // Forward the returned data received from the server to the client Downstream_socket _. async_send_to (boost: ASIO: buffer (upstream_data _, bytes_transferred), downstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: handle_downstream_send, This , Boost: ASIO: placeholders: Error ));} Else {STD: cerr < " Error: " <Error. Message () < STD: Endl ;}} Void Handle_downstream_send ( Const Boost: System: error_code & Error ){ If (! Error ){ // Receive data from the client Downstream_socket _. async_receive_from (boost: ASIO: buffer (downstream_data _, max_data_length), downstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: handle_downstream_receive, This , Boost: ASIO: placeholders: bytes_transferred, boost: ASIO: placeholders: Error ));} Else {STD: cerr < " Error: " <Error. Message () < STD: Endl ;}} Void Handle_downstream_receive ( Const Size_t & Bytes_transferred, Const Boost: System: error_code & Error ){ // Forward the data received from the client to the remote server Upstream_socket _. async_send_to (boost: ASIO: buffer (downstream_data _, bytes_transferred), upstream_remoteudpendpoint, boost: BIND ( & M_udpproxyserver: handle_upstream_send, This , Boost: ASIO: placeholders: Error ));}
Complete code:Https://gist.github.com/3888929
Okay, that's all. I hope it will help you.