I searched with my problem but with no success
I do not know how to implement proper function iphost().
Well I have file library.h file in which I store variables and function iphost().
There is another file called main.c where I run function iphost(...).
I really do not know how to fix my problem.
I got error adr_srvr doesn’t have element sin_addr.
/home/user/Desktop/pap_lab2/2/biblioteka.h|23|error: request for member ‘sin_addr’ in something not a structure or union|
Probably I declared wrong arguments with that stars pointers and ampersands. Can anybody help me?
library.h
void iphost(char* arg,struct sockaddr_in *adr_srvr)
{
if(*arg>=(char)'0' && *arg<=(char)'9')
{
printf("Prawdopodobnie wprowadzono IP: %s\n", arg);
inet_aton(arg, &adr_srvr.sin_addr);//here ERROR
srvr_addr = inet_ntoa(adr_srvr.sin_addr);
}
}
main.c
iphost(argv[2], &adr_srvr); // OK ?
Little help to undestand the code:
I cannot get to adr_srvr.sin_addr.
char* arg is arguments inputing by me, 4example: “127.0.0.1”.
adr_srvr.sin_addr store this 127.0.0.1
srvr_addr is char*
The normal code which works okay is (without functon) in main.c :
int main (int argc, char *argv[] )
{
int z;
int x;
char *srvr_addr;
struct sockaddr_in adr_srvr;
struct sockaddr_in adr;
struct hostent *he;
int len_inet;
int s;
char dgram[512];
struct in_addr **addr_list;
char buf[512];
//USTAWIENIE POLACZENIA
//CZY WPISANO IP
if(argv[1][0]>=(char)'0' && argv[1][0]<=(char)'9')
{
printf("Prawdopodobnie wprowadzono IP: %s\n", argv[1]);
inet_aton(argv[1], &adr_srvr.sin_addr);
srvr_addr = inet_ntoa(adr_srvr.sin_addr);
} else {}
...
}
———
I have similar question so I edit here. How can I implement this in function?:
else
{
if ((he = gethostbyname(arg)) == NULL) { // get the host info
herror("gethostbyname");
return 2;
}
// print information about this host:
printf("Wykryty HOST: '%s' ", arg);
printf("zamieniam na IP: ");
addr_list = (struct in_addr **)he->h_addr_list;
printf("%s", srvr_addr = inet_ntoa(*addr_list[0]));
printf("\n");
}
adr_srvris a pointer. To access its fields, you need to useadr_srvr->sin_addrinstead ofadr_srvr.sin_addr.In general, use
a.fieldwhenais a struct, anda->fieldwhenais a pointer to a struct.