Feint13
Feint13

Reputation: 1

C++ socket client / server simple message sender

I'm new in the socket concept , when I tried to folow the tutorial , I can't folow this step

Tutorial :

sockaddr_in ip;
ip.sin_family = AF_INET;
ip.sin_port = htons(99999);
ip.sin_addr.s_addr = inet_addr("10.0.0.2");

The problem is I can't folow the ip.sin_addr.saddr , when I type in : ip.sin.addr. the only member this one have is s_un , what am I suppose to do ?

Upvotes: 0

Views: 236

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596307

ip.sin_addr is an in_addr struct. Depending on the platform you are coding for, in_addr has different members available. For instance, on Linux, it has a single member:

struct in_addr {
    uint32_t s_addr;
};

Whereas on Windows, it has a union of three members:

struct in_addr {
    union {
        struct{
            unsigned char s_b1,
            s_b2,
            s_b3,
            s_b4;
        } S_un_b;
        struct {
            unsigned short s_w1,
            s_w2;
        } S_un_w;
        unsigned long S_addr;
    } S_un;
};

And then a precompiler #define macro maps s_addr to S_un.S_addr.

So, you can always use ip.sin_addr.s_addr in your code on all platforms, but it might resolve to different data members behind the scenes.

Upvotes: 1

Related Questions