BING_BOTTY
BING_BOTTY

Reputation: 31

how would a client connect to a server set to INADDR_ANY?

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?/"?

Upvotes: 2

Views: 1065

Answers (2)

Supergamer
Supergamer

Reputation: 433

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

Upvotes: 0

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12342

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.

Upvotes: 1

Related Questions