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