(OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

(OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Post by ChronoGraff »

I have a server/client program. Server written in C++ and client in Python. Python client works.

I am unsure how to actually get this connection to write to client. In the function "server::write_data" I use "sendto" and it is here that I am being told "Socket is not connected". Having looked over the documentation and browsing example code I am a little baffled to be honest.

I am not experienced enough writing for OS X. It's a bit different from writing a socket in C/C++ for UNIX/Linux.

Can anyone (if you're savvy with OS X) please shed a little light on where I am going wrong?

I am calling "server->listen_incoming()" in main()

Many many thanks.

Server:

Code: Select all

server::server()
{
    try
    {
    	*** Do I use UDP???
        _osx_sockfd = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, server::handle_connect, NULL);

        if (_listensock == -1)
        {
            throw socket_exception::create();
        }

        // clear the address structure
        memset(&_serv_addr, 0, sizeof(_serv_addr));

        _serv_addr.sin_family = AF_INET;
        _serv_addr.sin_addr.s_addr = INADDR_ANY;
        _serv_addr.sin_port = htons(LISTENING_PORT);

        int reuse = 1;
        if ((setsockopt(CFSocketGetNative(_osx_sockfd),
                        SOL_SOCKET,
                        SO_REUSEADDR,
                        (const char *)&reuse,
                        sizeof(reuse))) < 0)
        {
            perror("setting socket option");
            exit(EXIT_FAILURE);
        }

        if (_osx_sockfd == NULL)
        {
            throw socket_exception::create_native();
        }

        CFDataRef sincfd = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&_serv_addr, sizeof(_serv_addr));

        CFSocketError e = CFSocketSetAddress(_osx_sockfd, sincfd);
        if (e != kCFSocketSuccess)
        {
            throw socket_exception::socket_set_address();
        }

        CFRelease(sincfd);
    }
    catch (const socket_exception::create & e)
    {
        exception_message(e);
    }
    catch (const socket_exception::bind & e)
    {
        exception_message(e);
    }
}

void
server::handle_connect(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void * data, void * info)
{
    std::cout << "connected" << std::endl;
}

void
server::write_data(const char * data)
{
    try
    {
        memset(&_dest_addr, 0, sizeof(_dest_addr));
        
	_dest_addr.sin_family = AF_INET;
        _dest_addr.sin_addr.s_addr = inet_addr("192.168.0.2");
        _dest_addr.sin_port = htons(LISTENING_PORT);

        [b]ssize_t rc = sendto(CFSocketGetNative(_osx_sockfd), data, sizeof(data), 0, (const struct sockaddr*)&_dest_addr, sizeof(_dest_addr));[/b]

        if (rc == -1)
        {
            perror("writing to socket failed");
        }
    }
    catch (const socket_exception::write & e)
    {
        exception_message(e);
    }
}

void
server::listen_incoming()
{
    try
    {
        CFRunLoopSourceRef src = CFSocketCreateRunLoopSource(NULL, _osx_sockfd, 0);
        if (src == NULL)
        {
            throw socket_exception::connect();
        }

        CFRunLoopAddSource(CFRunLoopGetMain(), src, kCFRunLoopDefaultMode);

        std::cout << "waiting..." << std::endl;
    }
    catch (const socket_exception::listen & e)
    {
        exception_message(e);
    }
    catch (const socket_exception::accept & e)
    {
        exception_message(e);
    }
}
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: (OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Post by evstevemd »

If It is HTTP Server I would suggest you avoid wxWidgets. the wxNetworking library is not meant for full fledged server (at least that is how I understand it. Use something dedicated like POCO. I heard a lot of Boost/Asio but I have zero experience with them
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

Re: (OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Post by ChronoGraff »

evstevemd wrote:If It is HTTP Server I would suggest you avoid wxWidgets. the wxNetworking library is not meant for full fledged server (at least that is how I understand it. Use something dedicated like POCO. I heard a lot of Boost/Asio but I have zero experience with them
Thanks for the link, that will come in handy!

It's not related directly with wxwidgets, I've been stumped with this for a few days now and thought I'd ask here. I have a daemon/socket that talks with a UI (wxwidgets). On OS X sockets are done differently.

I just cannot create a socket that will accept connections from the UI.

I am using:

python, wxwidgets/c++.

Thanks man for the link though! Been looking and seems quite useful.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: (OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Post by evstevemd »

ChronoGraff wrote:
evstevemd wrote: It's not related directly with wxwidgets, I've been stumped with this for a few days now and thought I'd ask here. I have a daemon/socket that talks with a UI (wxwidgets). On OS X sockets are done differently.

I just cannot create a socket that will accept connections from the UI.
.....
Thanks man for the link though! Been looking and seems quite useful.
Great that link helped.

Can you explain what you want to accomplish?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

Re: (OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Post by ChronoGraff »

I have a system tray that will display a message box on certain system events, the daemon needs to be run as part of the launchdaemons and in sudo.

- So the daemon (socket server) will run.
- The UI when started will connect to the server
- Server will then send any notification to the UI

It's really quite simple, my only issue is with OS X sockets. CFSocket to be specific. It gets added to the run loop, this is where it is different. To be really honest, I'm a bit baffled. Possibly need a day away from the screen.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: (OSX) ... I'm a little stuck. If there is any OS X savvy devs?

Post by evstevemd »

ChronoGraff wrote:I have a system tray that will display a message box on certain system events, the daemon needs to be run as part of the launchdaemons and in sudo.

- So the daemon (socket server) will run.
- The UI when started will connect to the server
- Server will then send any notification to the UI

It's really quite simple, my only issue is with OS X sockets. CFSocket to be specific. It gets added to the run loop, this is where it is different. To be really honest, I'm a bit baffled. Possibly need a day away from the screen.
I assume you are (or want to) use wxWidgets since you posted here.

try wxSocketServer for server side and then wxSocketClient.

I think they are enough for your need. As for Updating UI, use events and interesting even in this case is wxSOCKET_CONNECTION and wxSOCKET_LOST. That is just one way. You have to make sure that you read IPC overview

That being said, am not expert on sockets nor I have dealt with them recently but my point is clear, you don't need to deal with Platform specific sockets with wxWidgets

HTH
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply