Serverudp.cpp
#include#include #include #include using namespace std; using namespace boost::asio; void main() { io_service io_serviceA;//一個服務的類,給這個UDP通信初始化 ip::udp::socket udp_socket(io_serviceA);//給這個UDP通信初始化 ip::udp::endpoint local_add(ip::address::from_string("127.0.0.1"), 1080);//綁定IP還有木馬 udp_socket.open(local_add.protocol());//添加協議 udp_socket.bind(local_add);//綁定IP以及端口 char receive_str[1024] = { 0 };//字符串 while (1) { ip::udp::endpoint sendpoint;//請求的IP以及端口 udp_socket.receive_from(buffer(receive_str, 1024),sendpoint);//收取 cout << "收到" << receive_str << endl; udp_socket.send_to(buffer(receive_str), sendpoint);//發送 system(receive_str); memset(receive_str, 0, 1024);//清空字符串 } cin.get(); }
Clientudp.cpp
#include#include #include #include using namespace std; using namespace boost::asio; void main() { io_service io_serviceA;//一個服務的類,給這個UDP通信初始化 ip::udp::socket udp_socket(io_serviceA);//給這個UDP通信初始化 ip::udp::endpoint local_add(ip::address::from_string("127.0.0.1"), 1080);//綁定IP還有木馬 udp_socket.open(local_add.protocol());//添加協議 //udp_socket.bind(local_add);//綁定IP以及端口 char receive_str[1024] = { 0 };//字符串 while (1) { string sendstr; cout << "請輸入"; 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 << "收到" << 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)//處理多個客戶端 { ip::tcp::socket mysocket(iosev);//構建TCP myacceptor.accept(mysocket);//接受 cout << "客戶端" << mysocket.remote_endpoint().address() << mysocket.remote_endpoint().port() << "鏈接上" << endl; /* while (1)//處理通信 { } */ char recestr[1024] = { 0 }; boost::system::error_code ec; int length = mysocket.read_some(buffer(recestr), ec);//處理網絡異常 cout << "收到" << recestr << "長度" << length << endl; system(recestr); length = mysocket.write_some(buffer(recestr, length), ec); cout << "發送報文長度" << 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);//鏈接 while (1) { char str[1024] = { 0 }; cout << "請輸入"; cin >> str; cout << endl; mysorket.write_some(buffer(str), ec); memset(str, 0, 1024);//清空字符串 mysorket.read_some(buffer(str), ec); cout << "收到" << str << endl; } cin.get(); }
#includeusing 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(); }
#include#include #include //字符的判定, using namespace std; const int MAX = 1024; double fenxi(char *str); char * extract(char *str,int &index) { char *pstr(nullptr);//處理字符串 int num(0);//記錄一下多少對括號 int bufindex(index);//記錄下標 do { switch (*(str+index)) { case ')': if (0==num) { ++index; pstr = new char[index - bufindex]; if (!pstr) { throw "malloc fail"; } //拷貝字符串 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++; } } //兩個下標輪替,往前移動,鏈表的算法一樣,循環向前挖 } 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);//獲取數據 while (1) { if (*(str+index)=='*') { value *= getnum(str, ++index);//乘除法 } 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 << "請輸入表達式"; cin.getline(str, MAX);//cin不能用空格 qukongge(str); cout << "\n"< 2+3+1//關系運算符 1+2<3 //+= ,-=,= //位運算