Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request. #19

Open
afraidofdark opened this issue May 20, 2020 · 2 comments
Open

Feature request. #19

afraidofdark opened this issue May 20, 2020 · 2 comments
Assignees

Comments

@afraidofdark
Copy link

afraidofdark commented May 20, 2020

example.c file's cpp version would be very apriciated. Thank you.

@afraidofdark
Copy link
Author

This is what i have so far. On vs 2019 its working without any problem.

// Udp Server
#define ZNPP_STATIC_API
#include "znet.hpp"
#include <iostream>

using namespace zsummer::network;

ZSummerEnvironment g_appEnvironment;

int main(void)
{
    EventLoopPtr lp = std::make_shared<EventLoop>();
    if (lp->initialize())
    {
        UdpSocket rec;
        if (rec.initialize(lp, "127.0.0.1", 12345))
        {
            char rmsg[256];
            rec.doRecvFrom(rmsg, 256, [&rmsg](NetErrorCode err, char const* ip, unsigned short port, unsigned count)
            {
                if (err == NEC_SUCCESS)
                {
                    rmsg[count] = '\0';
                    std::cout << ip << ":" << port << " " << rmsg << std::endl;
                }
            }
            );

            lp->runOnce();
        }
        else
        {
            return -2;
        }
    }
    else
    {
        -1;
    }

    return 0;
}
// Udp client
#define ZNPP_STATIC_API
#include "znet.hpp"
#include <iostream>

using namespace zsummer::network;

ZSummerEnvironment g_appEnvironment;

int main(void) 
{
    EventLoopPtr lp = std::make_shared<EventLoop>();
    if (lp->initialize())
    {
        // Client.
        UdpSocket uscok;
        
        if (uscok.initialize(lp, "127.0.0.1", 0))
        {
            std::string msg = "Hello world !";
            uscok.doSendTo(msg.data(), msg.size(), "127.0.0.1", 12345);
        }
        else
        {
            return -1;
        }
    }

    return 0;
}

@xiaoliuzi
Copy link

I didn't like the code style you had used, I thought there were better options at least.

@starwing starwing self-assigned this Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants