ソケット通信(ホストからIPアドレス取得)

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
あま

ソケット通信(ホストからIPアドレス取得)

#1

投稿記事 by あま » 14年前

struct hostent *IP;
IP = gethostbyname(host);
bcopy(IP->h_addr,&client_addr.sin_addr.s_addr,IP->h_length);

これだと指定したホストのIPを得られないみたいなのですが間違ってるのでしょうか?

あま

Re:ソケット通信(ホストからIPアドレス取得)

#2

投稿記事 by あま » 14年前

URLを取得してから分解してhostを作っています。
そのあとソケット接続しています。
コンパイルは通って動くのですが
存在するはずのドメインが存在しませんとHTTPサーバーから怒られてしまします。

下記ソース

/*省略*/
if(strstr(url,"http://") && sscanf(url,"http://%s",host_path) && strcmp(url,"http://"))
{
p = strchr(host_path,'/');
if(p != NULL)
{
strcpy(path,p);
*p = '\0';
strcpy(host,host_path);
}
else
{
strcpy(host,host_path);
}

p = strchr(host,':');
}

IP = gethostbyname(host);

if(IP == NULL)
{
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>ERROR!!</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<font color=\"red\">ERROR!![/color]\n");
printf("<hr>\n");
printf("不明なホストを取得しました。
\n");
printf("<a href=\"./dosaccess.cgi\" UTN>戻る[/url]\n");
printf("</body>\n");
printf("</html>\n");

exit(1);
}

bzero((char *)&client_addr,sizeof(client_addr));

client_addr.sin_family = AF_INET;
bcopy(IP->h_addr,&client_addr.sin_addr.s_addr,IP->h_length);
client_addr.sin_port = htons(80);

sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>ERROR!!</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<font color=\"red\">ERROR!![/color]\n");
printf("<hr>\n");
printf("接続できませんでした。
\n");
printf("<a href=\"./dosaccess.cgi\" UTN>戻る[/url]\n");
printf("</body>\n");
printf("</html>\n");

close(sockfd);

exit(1);
}

if(connect(sockfd,(struct sockaddr *)&client_addr,sizeof(client_addr)) > 0)
{
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>ERROR!!</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<font color=\"red\">ERROR!![/color]\n");
printf("<hr>\n");
printf("接続できませんでした。
\n");
printf("<a href=\"./dosaccess.cgi\" UTN>戻る[/url]\n");
printf("</body>\n");
printf("</html>\n");
exit(1);

close(sockfd);

exit(1);
}

sprintf(buf,"GET %s HTTP/1.1\r\n"
"User-Agent: %s\r\n"
"X-UP_SUBNO: %s\r\n"
"HTTP_X_JPHONE_UID: %s\r\n"
"X_DCMGUID: %s\r\n"
"\r\n",path,ua,host,ez,sb,dc);

i = 0;
x = 0;

write(sockfd,buf,strlen(buf));

i = read(sockfd,buf,1023);
x = 0;

printf("Content-type: text/html\n\n");
printf("<html><head></head><body>\n");
printf("URL : %s
\n",url);
printf("Host : %s
\n",host);
printf("Path : %s
\n",path);

for(x=0; x<i; x++)
{
printf("%c",buf[x]);
}
/*省略*/

間違っていますか?

あま

Re:ソケット通信(ホストからIPアドレス取得)

#3

投稿記事 by あま » 14年前

開発環境はLinuxのgccです。
HTMLタグがあるので気付くかと思いますがC言語のCGIです。
先日質問させていただいたときと同じ環境になります。

閉鎖

“C言語何でも質問掲示板” へ戻る