简体   繁体   English

套接字编程 - 将Windows代码转换为linux代码

[英]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. 我是套接字编程的新手,我的任务是将windows代码更改为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). 我得到了这段代码,其中IP4地址的四个参数从一个函数转移到我的套接字代码(在带有头文件winsock.h的windows中)。

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. 我的问题是,如果有类似的方式将这些参数传递给linux套接字代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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