PDA

View Full Version : sockets problem



tindala
02-23-2014, 05:33 PM
hi,
i have this code :


#include <stdio.h>#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>


int main()
{
char *hostname = "ddr";
struct addrinfo hints, *res;
struct in_addr *addr;
int err;
char gg[50];
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;


if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
{
printf("error %d\n", err);
return 1;
}


addr->s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
gg = inet_ntoa(addr);
/*printf("official host name: %s\n",h->h_name);*/
printf("ip address : %s\n", gg);


freeaddrinfo(res);


return 0;
}

and i want to get the following output:
/*
official host name: ddr
internet address: 127.0.1.1 (7F000101)
*/
where (7F000101) is by the inet_ntoa() obtained info.

with this code i can get the desired info:

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


int main(void)
{
struct hostent *h;
struct in_addr *a;


if((h=gethostbyname("ddr"))==NULL) exit(1);
printf("official host name: %s\n",h->h_name);


a=(struct in_addr*)h->h_addr_list[0];
printf("internet address: %s (%08lX)\n",inet_ntoa(*a),(long unsigned int)ntohl(a->s_addr));


exit(0);

}


but witht the 1 code i am not able for some reason, first of all i am getting the following warning when i compile the 1 code:
gcc tt.c -o tt
tt.c: In function ‘main’:
tt.c:29:5: error: incompatible types when assigning to type ‘char[50]’ from type ‘int’
make: *** [tt] Error 1

and i dont know why i getting that error, i think that i am doing everything ok, the inet_ntoa returns a char * and gg is also a char*, so what is the problem?

i thank you in advance for the help

Salem
02-23-2014, 11:10 PM
> tt.c:29:5: error: incompatible types when assigning to type ‘char[50]’ from type ‘int’
...
> gg = inet_ntoa(addr);
You can't assign arrays.

When you've made it return a char*, then you use strcpy() to copy that to an array.
Is 50 chars always enough?


Also, since it also complains that you're trying to assign from int, it means you didn't declare the function properly.
See UNIX man pages : inet_ntoa (3) (http://unixhelp.ed.ac.uk/CGI/man-cgi?inet_ntoa+3)

tindala
02-24-2014, 05:21 AM
hi, thank you for the answer, but the function inet_ntoa, which is defined as following:
char *inet_ntoa(struct in_addr in);

returns a pointer to a char, so it is valid gg = inet_ntoa(addr); given that gg is also a char* type.

beside that i have changed the program a little bit:



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


int main()
{
char *hostname = "ddr";
struct addrinfo hints, *res;
struct in_addr *addr;
int err;
char *gg;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;


if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
{
printf("error %d\n", err);
return 1;
}


addr->s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
/*gg = inet_ntoa(addr);*/
/*printf("official host name: %s\n",h->h_name);*/
printf("ip address : %s\n", inet_ntoa(addr));


freeaddrinfo(res);


return 0;
}


so it is now similar to the other program 2.In Program 2 i have the same printf for the same type and i dont get that error
printf("internet address: %s (%08lX)\n",inet_ntoa(*a),(long unsigned int)ntohl(a->s_addr));

and in the program 1 i get that error, which is
gcc tt.c -o tt
tt.c: In function ‘main’:
tt.c:32:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

Salem
02-24-2014, 05:40 AM
First, compile with more warnings.


$ gcc -Wmissing-prototypes -Wall foo.c
foo.c: In function ‘main’:
foo.c:31:2: warning: implicit declaration of function ‘inet_ntoa’ [-Wimplicit-function-declaration]
foo.c:31:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:15:8: warning: unused variable ‘gg’ [-Wunused-variable]
foo.c:28:15: warning: ‘addr’ may be used uninitialized in this function [-Wuninitialized]


Second, read the manual page. Note the feature macro necessary to expose the interface of inet_ntoa
Also note the header file arpa/inet.h which you are not including.


INET(3) Linux Programmer's Manual INET(3)

NAME
inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof, inet_netof - Internet address manipulation routines

SYNOPSIS
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);

in_addr_t inet_network(const char *cp);

char *inet_ntoa(struct in_addr in);

struct in_addr inet_makeaddr(int net, int host);

in_addr_t inet_lnaof(struct in_addr in);

in_addr_t inet_netof(struct in_addr in);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

inet_aton(), inet_ntoa(): _BSD_SOURCE || _SVID_SOURCE



When the include is fixed, the call is fixed and compiled with the correct command line, I get


$ gcc -Wmissing-prototypes -Wall -D_SVID_SOURCE foo.c
foo.c: In function ‘main’:
foo.c:15:8: warning: unused variable ‘gg’ [-Wunused-variable]
foo.c:28:15: warning: ‘addr’ may be used uninitialized in this function [-Wuninitialized]