C语言 客户端如何连接到设置为INADDR_ANY服务器?

bsxbgnwa  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(170)

我对客户端如何连接到服务器感到非常困惑,因为它的结构sockaddr_in设置为ADDRESS。sin_addr。s_addr = htonl(INADDR_ANY);
在绑定调用之后,服务器侦听套接字将被设置为INADDR_ANY,那么客户端如何连接到设置为INADDR_ANY的套接字呢?
在connect()系统调用之前,客户端传递给sockaddr_in结构体的地址是什么?是服务器的ip地址吗?我很困惑。
这里是一个基本的超级不可靠的服务器的代码,我正在玩...

#include <arpa/inet.h> 
#include <sys/socket.h> /*socket()*/
#include <netinet/in.h> /*struct sockaddr_in*/
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    char string[32];
    int ssockfd, csocadsz, nwsockfd;
    unsigned short int ptnum;
    struct sockaddr_in ssockaddr, csockaddr;
    unsigned long int addr;
    ssockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    ssockaddr.sin_family = AF_INET;
    
    printf("Enter port number: ");
    scanf("%hu", &ptnum);
    
    ssockaddr.sin_port = htons(ptnum);
    ssockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    bind(ssockfd, (struct sockaddr *) &ssockaddr, sizeof(ssockaddr));
    
    listen(ssockfd, 5);
    
    csocadsz = sizeof(csockaddr);
    
    nwsockfd = accept(ssockfd, (struct sockaddr *) &csockaddr, &csocadsz);
    
    read(nwsockfd, string, 31);

    printf("Here is the message: %s\n", string);

    write(nwsockfd, "I got your message lol\n", 24);
    
    return 0;
}

我想写一个连接到这个服务器的客户机,但是我对于传入它的name.sin_addr.s_addr参数的内容感到困惑。
编辑:这是不完整的客户端计划。

#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    int clisockfd;
    unsigned short int port_number;
    char sipad[12], string[32];
    struct sockaddr_in saddr;
    
    printf("Enter port number: ");
    scanf("%hu", &port_number);
        
    printf("Enter servers &ip: ");
    scanf("%s", sipad);
    
    clisockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(port_number);
    saddr.sin_addr.s_addr = /*What do I input here?*/
    
    connect(clisockfd, (struct sockaddr *)&saddr, sizeof(saddr));
    
    printf("Please enter a message without whitespace: ");

    scanf("%s", string);
    
    write(clisockfd, string, strlen(string));
    bzero(string, 256);
    
    read(clisockfd, string, 31);
        
    printf("%s\n", string);
    
    return 0;
}

我应该在注解“/What do I input here?/"的位置输入什么?

hwazgwia

hwazgwia1#

When you bind a socket you specify the IP address the kernel should accept connects on. That allows you to bind only so connections coming in on specific networks that have access to the specified address. For example if you bind the socket to 127.0.0.1 then only clients running on the same system and using IPv4 can connect and nothing from the outside.
如果你指定INADDR_ANY作为bind,那么你就绑定到系统已经拥有或将来将要拥有的所有地址。
将您在bind中指定的IP地址视为一个过滤器。只有当客户端连接到的IP地址与bind中指定的IP地址匹配时,才会进行连接。INADDR_ANY匹配那里的每个地址。

iqjalb3h

iqjalb3h2#

INADDR_ANY指的是每台机器的ip,这意味着您可以同时在所有IP上绑定、连接等。
我会建议找到主要的互联网IP,如果你想从另一个设备连接或localhost IP,这是127.0.0.1连接从同一设备。

正在查找IP地址

char hostname[128];
gethostname(hostname,128);
hostent *host=gethostbyname(hostname);
in_addr ip=*((in_addr *)host->h_addr_list[0]);

变量ip是您的ip
我们创建一个名为hostname的字符数组,然后将其传递给名为gethostname的函数,其参数1是存储主机名的数组,参数2是该数组的大小
之后,我们创建一个hostent类型的指针,并将gethostbyname返回的值存储在其中。
然后从指针h->h_addr_list获取值,并将其类型转换为in_addr

相关问题