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 책에 나오는 샘플소스입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.