ierdna
ierdna

Reputation: 6293

sockaddr_in not found in XCode 6 (on OS X 10.10 Yosemite)

I'm trying to send UDP packets from an objective c app. It's been working fine since OS X 10.6 until Yosemite came along. Now i'm getting "unknown type name 'sockaddr_in'" error.

my code:

#import <sys/socket.h>
-(void)sendUdpPacket:(unsigned int) udp_address port:(unsigned short)port data:(const char*)data dataSize:(long)dataSize
{
    int handle = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

    if ( handle <= -1 )
    {
        return;
    }

    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl( udp_address );

    address.sin_port = htons( port );
    if ( bind( handle, (const sockaddr*) &address, sizeof(sockaddr_in) ) < -1 )
    {
        printf( "-----------------------failed to bind socket\n " );
        return;
    }
    int nonBlocking = 1;
    if ( fcntl( handle, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 )
    {
        printf( "----------------------failed to set non-blocking socket for %d\n", port );
        return;
    }

    int sent_bytes = sendto( handle, data, dataSize, 0, (sockaddr*)&address, sizeof(sockaddr_in) );
    close(handle);
}

now i'm getting errors that it can't find IPROTO_UDP or sockaddr_in

has anyone come across this problem?

EDIT: didn't realize that stuff in <> brackets wasn't displaying

Upvotes: 1

Views: 4107

Answers (2)

Henrique Leme
Henrique Leme

Reputation: 11

Xcode 6 needs the complete data type definition. Not "sockaddr_in" but "struct sockaddr_in". The same for "const sockaddr*" must be "const struct sockaddr*".

Upvotes: 1

Ken Thomases
Ken Thomases

Reputation: 90671

See the inet(4) man page. You have to:

#include <sys/types.h>
#include <netinet/in.h>

In general, the socket interface is generic. It can be used with different address families. The struct sockaddr is a general form, while sockaddr_in is specific to IPv4. You would use sockaddr_in6 for IPv6 addresses. For Unix domain sockets, you would use sockaddr_un. Etc. You need to consult the man page for the type of socket you want to work with (e.g. inet6(4), unix(4), etc.).

Upvotes: 5

Related Questions