OS X Socket issue - can someone help please?

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".

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

Can anyone please shed a little light on where I am going wrong?

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

Many many thanks.

Server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
server::server()
{
    try
    {
        _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
    {
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));

        ssize_t rc = sendto(CFSocketGetNative(_osx_sockfd), data, sizeof(data), 0, (const struct sockaddr*)&addr, sizeof(addr));

        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);
    }
}
Last edited on
Topic archived. No new replies allowed.