客户端将如何连接到设置到Inaddr_any的服务器?

发布于 2025-01-27 04:51:30 字数 2427 浏览 4 评论 0原文

我对客户端将如何将其结构sockaddr_in设置为地址连接到服务器感到非常困惑。sin_addr.s_addr= htonl(inaddr_any);

绑定呼叫后,服务器侦听套接字将设置为inaddr_any,客户端甚至如何连接到设置Inaddr_any的套接字?

在Connect()系统调用之前,客户端将进入SockAddr_in struct的地址是什么?是服务器的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;
}

我想编写一个连接到该服务器的客户端,但是我对将其传递给其名称感到困惑。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;
}

我在评论说“/我在这里输入什么?/”的评论说什么?

I am super confused on how a client would connect to a server with its struct sockaddr_in set to ADDRESS.sin_addr.s_addr = htonl(INADDR_ANY);

after a bind call, the servers listening socket would be set to INADDR_ANY, how would a client even connect to a socket set to INADDR_ANY?

What is the address that the client would pass into the sockaddr_in struct before the connect() system call? Is it the ip address of the server, I am so confused.

Here is the code for a basic super unreliable server I am playing around with...

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

I want to write a client that connects to this server, but I am stumped as to what I pass into its name.sin_addr.s_addr parameter.

EDIT: HERE IS THE INCOMPLETE CLIENT PROGRAM.

#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 put where the comment says "/What do I input here?/"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2025-02-03 04:51:30

绑定插座时,您指定IP地址内核应接受连接。这使您仅绑定只能在特定网络上访问指定地址的连接。例如,如果将套接字绑定到127.0.0.1,则只有在同一系统上运行并使用IPv4的客户端可以连接,而从外部则无关。

如果您指定InAddr_any的绑定,则可以绑定到系统将来拥有或将来将有的所有地址。这是一个通配符,允许任何地方连接。

想想您在绑定中指定的IP地址为过滤器。仅当IP地址客户端连接到绑定中指定的匹配项时,连接将发生。 Inaddr_any匹配那里的每个地址。

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.

If you specify INADDR_ANY for bind then you bind to all the addresses the system has or will have in the future. It's a wildcard allowing connections from anywhere.

Think of the IP address you specify in bind as a filter. Only if the IP address the client connects to matches that specified in bind then the connection will happen. INADDR_ANY matches every address there.

过去的过去 2025-02-03 04:51:30

inaddr_any是指每个计算机的IP,这意味着您可以同时绑定,连接等。

如果您想从另一个设备或Localhost IP连接127.0.0.0.1以从同一设备连接,我建议您找到主要的Internet IP。

查找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是dome的数组存储主机名和参数2之后是该数组的大小

,我们创建一个类型hottent的指针,并在其中存储由gethostbyname返回的值。

然后从Pointer H-&GT; H_ADDR_LIST中获取值

INADDR_ANY refers to the every machine's ip, meaning that you can bind, connect, etc at all IPs at the same time.

I would recommend find the main internet IP if you want to connect from another device or localhost IP which is 127.0.0.1 to connect from same device.

Finding IP address

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

variable ip is your ip

We create an array of characters called hostname, then we pass it into the function called gethostname, its argument 1 is the array in which to store hostname and argument 2 is size of that array

After, we create a pointer of type hostent and store the value returned by gethostbyname in it.

Then take the value from pointer h->h_addr_list and type-cast it to in_addr

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文