c main () ;获取函数值的问题?
请教:代码如下:
static char *
get_socket_address(int socket, int remote, int flags)
{
struct sockaddr_storage addr;
socklen_t addrlen;
char ntop[NI_MAXHOST];
/* Get IP address of client. */
addrlen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
if (remote) {
if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
< 0)
return NULL;
} else {
if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
< 0)
return NULL;
}
/* Get the address in ascii. */
if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop),
NULL, 0, flags) != 0) {
error("get_socket_address: getnameinfo %d failed", flags);
return NULL;
}
return xstrdup(ntop);
}
///如何在 main 中读取上段代码的值
int main(int argc, char **argv)
{
int ch, longindex;
char *config = NULL;
uid_t uid = 0;
gid_t gid = 0;
int err;
.....................
sprintf(buf,"%s",ntop);
printf("IP address=%s\n", buf);
......................
如何把前面那一段函数代码中的 ntop 值在 main 函数中读取到
谢谢
const char * ntop = get_socket_address(socket, remote, flags); //socket,remote,flags是什么我也不知道,这个问你~
sprintf(buf,"%s",ntop);
...
//此时ntop已经不需要了
xfree(ntop);