12_虚函数表、TCP、UDP

本文详细介绍了使用Boost.ASIO库进行UDP和TCP网络编程的方法,并通过具体实例展示了如何构建服务器与客户端应用程序。此外,还探讨了C++中的虚函数表机制以及如何实现一个简单的递归计算器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【目录】

一、 UDP-server 2
二、 UDP-client 2
三、 TCP-server 3
四、 TCP-client 4
五、 虚函数表 4
1、 范例: 4
六、 计算器递归实现 6

一、UDP-server

#include <iostream>
#include<string>
#include <boost/asio.hpp>
#include <stdlib.h>
using namespace std;
using namespace boost::asio;

void main()
{
    boost::asio::io_service  io_serviceA;
//一个服务的类,给这个UDP通信初始化
    boost::asio::ip::udp::socket  udp_socket(io_serviceA);
//给这个UDP通信初始化
    boost::asio::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();
}

二、UDP-client

#include <iostream>
#include<string>
#include <boost/asio.hpp>
#include <stdlib.h>
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");
    }

三、TCP-server

#include <boost/asio.hpp>
#include <iostream>
#include <stdlib.h>
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();
}

四、TCP-client

#include <boost/asio.hpp>
#include <iostream>
#include <stdlib.h>
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();
}

五、虚函数表

1、范例:

#include <iostream>
using namespace std;

class H
{
    virtual void M()
    {
        cout << "H::M" << endl;
    }
};
class A
{
    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; //8,有两个虚函数表
    B b;
    Fun pFun;

    for (int i = 0; i < 5; i++)
    {
        pFun = (Fun)*((int*)* (int*)(&b) + i);
通过这个示例,我们可以看到,我们可以通过强行把&b转成int *,取得虚函数表的地址,然后,再次取址就可以得到第一个虚函数的地址了,也就是Base::f(),这在上面的程序中得到了验证(把int* 强制转成了函数指针)。
        pFun();
    }
    Fun pFun1 = (Fun)*((int *)*((int*)(&b) + 1));
    pFun1();

    cin.get();
}

六、计算器递归实现

#include<iostream>
#include <cstdlib>
#include <cctype>//字符的判定,

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;
    }
    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"<<str;
    /*
    int i = 0;
    cout << "\n"<<getnum(str,i) << endl;
    */

    cout << "\n"<<fenxi(str) << endl;
    system("pause");
}

// 1+3/5%3*(1+2*(1+3))大家都要求会的 
// 3>2+3+1//关系运算符  1+2<3
//+= ,-=,=
//位运算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值