简体   繁体   中英

Socket programming - Converting Windows code into linux code

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM