TCP/IP 소켓 프로그래밍에서..

mani85의 이미지

안녕하세요/

tcp/ip 프로그램을 짜고 있는데요

클라이언트에서 현재 자신의 아이피를 서버에 보내고 싶은데

관련 함수나, 변수가 있을듯 한데 맞나요?

감사합니다.

jeongheumjo의 이미지

클라이언트가 ip를 서버에 보낸다..
제가 보기에는 이름등록과 등록된 이름으로 ip를 쿼리하는 메카니즘을 말씀하시는 것 같아요..
이를 위해서는 클라이언트와 서버가 직접 그 일을 당사자간에 수행할 수는 없고, 중간에 DNS 서버를 사용하게 되고, DNS 서버는 계층 구조로 인터넷에 쫙 깔려있고요..
머 그런 얘기하시는 거라면,

#include <netdb.h>
 
struct hostent *gethostbyaddr(const void *addr, socklen_t len,
       int type);
struct hostent *gethostbyname(const char *name);

를 연구해보시면 될듯 하고요,

/*
 * gethostbyname.c
 * Written by SW. YOON
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
 
void error_handling(char *message);
 
int main(int argc, char **argv)
{
	int i;
	struct hostent *host;
 
	if(argc!=2){
		printf("Usage : %s <addr>\n", argv[0]);
		exit(1);
	}
 
	host=gethostbyname(argv[1]);
	if(!host)
		error_handling("gethost... error");
 
	printf("Officially name : %s \n\n", host->h_name);
 
	puts("Aliases-----------");
	for(i=0;host->h_aliases[i]; i++){
		puts(host->h_aliases[i]);
	}
 
	printf("Address Type : %s \n", host->h_addrtype==AF_INET? "AF_INET":"AF_INET6");
 
	puts("IP Address--------");
	for(i=0; host->h_addr_list[i]; i++){
		puts( inet_ntoa( *(struct in_addr*)host->h_addr_list[i] ));
	}
 
	return 0;
}
 
void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}
 
 
/*
 * gethostbyaddr.c
 * Written by SW. YOON
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
 
void error_handling(char *message);
 
int main(int argc, char **argv)
{
	struct hostent *host;
	struct sockaddr_in addr;
	int i;
 
	if(argc!=2){
		printf("Usage : %s <IP>\n", argv[0]);
		exit(1);
	}
	memset(&addr, 0, sizeof(addr));
	addr.sin_addr.s_addr=inet_addr(argv[1]);
 
	host=gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
 
	if(!host)
		error_handling("gethost... error");
 
	printf("Officially name : %s \n\n", host->h_name);
 
	puts("Aliases-----------");
	for(i=0;host->h_aliases[i]; i++){
		puts(host->h_aliases[i]);
	}
 
	printf("Address Type : %s \n", host->h_addrtype==AF_INET? "AF_INET":"AF_INET6");  
	puts("IP Address--------");
	for(i=0; host->h_addr_list[i]; i++){
		puts( inet_ntoa( *(struct in_addr*)host->h_addr_list[i] ));
	}
 
	return 0;
}
 
void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

이거는 샘플입니다. 윤성우씨의 열혈강의 TCP/IP 책에 나오는 샘플소스입니다.