0

i'm quite new to socket programming and my task is to change a windows code into linux. There i got a problem and i hope you can help me. I got this code segment, where the four parameters of the IP4-address get transfered from a function to my socket code (in windows with the header winsock.h).

struct sockaddr_in server;

server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

My question is, if there is a similar type of way to transfer these parameters to the linux socket code.

2
  • the server is defined like this: struct sockaddr_in server; Commented Apr 26, 2013 at 11:37
  • 1
    Please edit your question to add this information Commented Apr 26, 2013 at 11:58

1 Answer 1

2

You can convert it manually:

inaddr_t make_inaddr(
            unsigned char a1,
            unsigned char a2,
            unsigned char a3,
            unsigned char a4)
{
    inaddr_t result;

    result = htonl(((uint32_t)a1 << 24) 
                    | ((uint32_t)a2 << 16)
                    | ((uint32_t)a3 << 8)
                    | a4);
    return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Kilian Then it is about time to accept ✓ this answer in order to encourage other people to give good answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.