/*
  自分自身に接続し、そのときに IP 番号を取得する
  Satofumi KAMIMURA
  $Id: ownIPAddress.cpp 286 2008-10-20 09:40:22Z satofumi $
*/

#include <stdio.h>
#include <stdlib.h>
#include "tcpipServer.h"


int main(int argc, char *arvg[]) {
  try {
    // サーバの起動
    enum { Port = 57577 };

    char computer[256];
    if (gethostname(computer, 255) != 0) {
      fprintf(stderr, "Could not get host information.\n");
      exit(1);
    }

    //const char ConnectHost[] = "localhost";
    //const char ConnectHost[] = "bird3";

    TcpipServer server;
    server.activate(Port);

    // 接続
    IPaddress ip;
    //if (SDLNet_ResolveHost(&ip, ConnectHost, Port) < 0) {
    if (SDLNet_ResolveHost(&ip, computer, Port) < 0) {
      printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
      exit(1);
    }

    //TCPsocket socket = SDLNet_TCP_Open(&ip);

    // Host, Port 情報の出力
    const char* host = SDLNet_ResolveIP(&ip);
    if (host) {
      printf("Host: %s\n", host);
    }
    printf("IP: %x, Port: %d\n", ip.host, ip.port);
    printf("IP: %d.%d.%d.%d\n",
	   ip.host & 0xff,
	   (ip.host >> 8) & 0xff,
	   (ip.host >> 16) & 0xff,
	   (ip.host >> 24) & 0xff);

  } catch (std::exception& e) {
    printf("exception: %s\n", e.what());
  }
  return 0;
}
