[C/C ++ school] 0905-boostSocketTCPUDP/virtual function table call/complex expression
BoostSocketTCPUDP
Serverudp. cpp
# Include
# Include
# Include
# Include
Using namespace std; using namespace boost: asio; void main () {io_service io_serviceA; // a service class that initializes the ip: UDP :: socket udp_socket (io_serviceA); // initialize the ip: UDP: endpoint local_add (ip: address: from_string ("127.0.0.1"), 1080) for this udp communication ); // bind the IP address and Trojan udp_socket.open (local_add.protocol (); // Add the Protocol udp_socket.bind (local_add); // bind the IP address and port char receive_str [1024] = {0 }; // string while (1) {ip: udp: endpoint sendpoint; // request IP address and port udp_socket.receive_from (buffer (receive_str, 1024), sendpoint ); // collect cout <"received" <receive_str <endl; udp_socket.send_to (buffer (receive_str), sendpoint); // send system (receive_str); memset (receive_str, 0, 1024); // clear string} cin. get ();}
Clientudp. cpp
# Include
# Include
# Include
# Include
Using namespace std; using namespace boost: asio; void main () {io_service io_serviceA; // a service class that initializes the ip: UDP :: socket udp_socket (io_serviceA); // initialize the ip: UDP: endpoint local_add (ip: address: from_string ("127.0.0.1"), 1080) for this udp communication ); // bind the IP address and the trojan udp_socket.open (local_add.protocol (); // Add the Protocol // udp_socket.bind (local_add); // bind the IP address and port char receive_str [1024] = {0 }; // string while (1) {string sendstr; cout <"enter"; cin> sendstr; cout <endl; udp_socket.send_to (buffer (sendstr. c_str (), sendstr. size (), local_add); udp_socket.receive_from (buffer (receive_str, 1024), local_add); cout <"received" <receive_str <endl ;} system ("pause ");}
Tcps. cpp
# Include
# Include
# Include
Using namespace std; using namespace boost: asio; void main () {io_service iosev; ip: tcp: acceptor myacceptor (iosev, ip: tcp: endpoint (ip:: tcp: v4 (), 1100); while (1) // process multiple clients {ip: tcp: socket mysocket (iosev); // construct TCPmyacceptor. accept (mysocket); // accept cout <"client" <mysocket. remote_endpoint (). address () <mysocket. remote_endpoint (). port () <"Link" <endl;/* while (1) // Process Communication {}*/char recestr [1024] = {0}; boost :: system: error_code ec; int length = mysocket. read_some (buffer (recestr), ec); // handle network exceptions. cout <"received" <recestr <"length" <length <endl; system (recestr); length = mysocket. write_some (buffer (recestr, length), ec); cout <"length of sent packets" <length <endl;} cin. get ();}
Tcpc. cpp
# Include
# Include
# Include
Using namespace std; using namespace boost: asio; void main () {io_service iosev; ip: tcp: socket mysorket (iosev); ip: tcp :: endpoint ep (ip: address_v4: from_string ("127.0.0.1"), 1100); boost: system: error_code ec; mysorket. connect (ep, ec); // link while (1) {char str [1024] = {0}; cout <"enter"; cin> str; cout <endl; mysorket. write_some (buffer (str), ec); memset (str, 0, 1024); // clear the string mysorket. read_some (buffer (str), ec); cout <"received" <str <endl;} cin. get ();}
Call a virtual function table
#include
using namespace std;class H{virtual void M(){cout << "H::M" << endl;}};class A{//int num;virtual void g(){cout << "A::g" << endl;}private:virtual void f(){cout << "A::f" << endl;}virtual void j(){cout << "A::j" << endl;}};class B : public A,public H{void g(){cout << "B::g" << endl;}virtual void o(){cout << "B::o" << endl;}virtual void h(){cout << "B::h" << endl;}};typedef void(*Fun)(void);void main(){cout << sizeof(A) << endl;cout << sizeof(H) << endl;cout << sizeof(B) << endl;B b;Fun pFun;for (int i = 0; i < 5; i++){pFun = (Fun)*((int*)* (int*)(&b) + i);pFun();}Fun pFun1 = (Fun)*((int *)*((int*)(&b) + 1));pFun1();cin.get();}
Complex expressions
# Include
# Include
# Include
// Determination of characters, using namespace std; const int MAX = 1024; double fenxi (char * str); char * extract (char * str, int & index) {char * pstr (nullptr); // process the string int num (0); // record the number of pairs of parentheses int bufindex (index ); // record subscript do {switch (* (str + index) {case ')': if (0 = num) {++ index; pstr = new char [index-bufindex]; if (! Pstr) {throw "malloc fail";} // copy the string strncpy_s (pstr, index-bufindex, str + bufindex, index-bufindex-1); return pstr ;} else {num --;} break; case '(': num ++; break ;}} while (* (str + index ++ )! = '\ 0'); throw "error fail";} void qukongge (char * str) {int I (0); int j (0 ); while (* (str + I) = * (str + j ++ ))! = '\ 0') {if (* (str + I )! = '') {I ++ ;}// two subscripts are rotated to move forward. The linked list algorithm is the same and the loop is dug forward.} double getnum (char * str, int & index) {double value (0.0); if (* (str + index) = '(') {char * substr (nullptr); substr = extract (str, ++ index ); value = fenxi (substr); delete [] substr; return value;} if (! Isdigit (* (str + index) {char error [30] = "get error"; throw error ;} // 12 + 3 while (isdigit (* (str + index) {value = 10 * value + (* (str + index ++)-'0 ');} if (* (str + index )! = '. ') {Return value;} else {double xiaoshu (1.0); while (isdigit (* (str + (++ index) {xiaoshu/= 10; value = value + (* (str + index)-'0') * xiaoshu;} return value;} double term (char * str, int & index) {double value (0.0); value = getnum (str, index); // get data while (1) {if (* (str + index) = '*') {value * = getnum (str, ++ index); // multiplication and division} else if (* (str + index) = '/') {value/= getnum (str, ++ index) ;}else {break ;}} return value ;}double fenxi (char * str) {double value (0.0); int index (0 ); value + = term (str, index); for (;) {switch (* (str + (index ++) {case '\ 0': return value; case '+': value + = term (str, index); break; case '-': value-= term (str, index); break; default: break ;}}void main () {char str [MAX] = {0}; cout <"Enter the expression"; cin. getline (str, MAX); // The cin cannot contain spaces qukongge (str); cout <"\ n" <
2 + 3 + 1 // relational operator 1 + 2 <3 // + =,-=, = // bitwise operation