#include<stdio.h>
#include<winsock.h>

void hosttoip(char*name);
void iptohost(char*ip);

void main(void)
{
	WSADATA wsadata;
	WORD wver;
	int Initwinsock;
	SOCKET thesocket;

	wver=MAKEWORD(1,1);
	//Initialize WinSock and check the version
	Initwinsock=WSAStartup(wver,&wsadata);

	if(Initwinsock !=0){
		printf("Cannot Start Winsock\n");
	}
	else{
	//	Create a TCP/IP strem socket
		thesocket = socket(AF_INET,SOCK_STREAM,0);
                printf("Socket Number = %d\n",thesocket);

		hosttoip("queen.mut.ac.th");
		iptohost("203.148.159.3");

	        closesocket(thesocket);
	}
	//Release WinSock
	WSACleanup();
}
void hosttoip(char*name)
{
	LPHOSTENT lphostent;
	SOCKADDR_IN addr_host;

	lphostent = gethostbyname(name);

	if(lphostent == NULL){
		printf("Cannot find IP from hostname\n");
		}else{
			addr_host.sin_family=AF_INET;
			addr_host.sin_addr=*((LPIN_ADDR)*lphostent->h_addr_list);
			printf("%s has IP Adress %s\n",name,inet_ntoa(addr_host.sin_addr));
			}
		}

		void iptohost(char*ip)
		{
			LPHOSTENT lphostent;

			IN_ADDR addr_ip;

			addr_ip.S_un.S_addr=inet_addr(ip);

			lphostent=gethostbyaddr((char*)&addr_ip,sizeof(IN_ADDR),AF_INET);
			if(lphostent == NULL){
				printf("Cannot fond hostname from ip adress\n");
			}else{
			printf("%s has hostname %s\n",ip,lphostent->h_name);
		}
	} 

