// client
#pragma comment(lib, "ws2_32.lib")
#include <WinSock2.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <netdb.h>
#define PORT 12345/*サーバと同じ数 */

int main(int argc, char *argv[])
{
	WSADATA	wsaData;
	int	i;
	struct hostent *host;
	struct sockaddr_in serv;
	int s, n;
	char buf[512];

	WSAStartup(MAKEWORD(2, 2), &wsaData);

	if (argc < 2) {
		fprintf(stderr,"need hostname\n");
		exit(1);
	}
	
	host = gethostbyname(argv[1]);
//	bzero((char *)&serv, sizeof(serv));
	memset(&serv, 0, sizeof(serv));
	serv.sin_family = AF_INET;
	serv.sin_port = htons(PORT);
	serv.sin_addr = *(struct in_addr *)host->h_addr;
	s = socket(AF_INET, SOCK_STREAM, 0);

	i = connect(s, (const struct sockaddr *)&serv, sizeof(serv));
	if (i < 0) {
//		i = WSAGetLastError();
		fprintf(stderr, "cannot connect\n");
		exit(1);
	}
	
	while (n = recv(s, buf, 512, 0)) {
		fwrite(buf, 1, n, stdout);
	}

	shutdown(s, SD_BOTH);
	closesocket(s);
	WSACleanup();
	return 0;
}
