#include<stdio.h>
#include<winsock.h>

void finger(char*user,SOCKET thesocket,char*ipaddr);

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("Cannoot Start Winsock\n");
	}
	else{
	//	Create a TCP/IP stream socket
		thesocket=socket(AF_INET,SOCK_STREAM,0);
		printf("Socket Number=%d\n",thesocket);
		finger("all",thesocket,"queen.mut.ac.th");

		closesocket(thesocket);
	}
	//Release WinSock
	WSACleanup();
}
void finger(char*user,SOCKET thesocket,char*ipaddr)
{
	int connect_server;
	LPHOSTENT lphostent;
	SOCKADDR_IN addr_host;
	char send_buf[256];
	char recv_buf[1024];

	lphostent=gethostbyname(ipaddr);

	addr_host.sin_addr=*((LPIN_ADDR)*lphostent->h_addr_list);
	addr_host.sin_family=AF_INET;
	addr_host.sin_port=htons(79);//prot finger

	connect_server = connect(thesocket,(struct sockaddr*)&addr_host,sizeof(addr_host));

	if(connect_server==SOCKET_ERROR){
		printf("Cannot connect server\n");
	}else{
		printf("Connect Server...\n");
		if(!(strcmp(user,"all"))){
			strcpy(send_buf,"\r\n");

		}else{
			strcpy(send_buf,user);
			strcat(send_buf,"\r\n");
		}
		send(thesocket,send_buf,sizeof(send_buf),0);
		recv(thesocket,recv_buf,1024,0);
		printf("%s",recv_buf);
	}
}
