// server
#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;
	char hostname[64];
	struct hostent *host;
	struct sockaddr_in me;
	int s0,s,n;
	char buf[512];

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

	gethostname(hostname,64);
	host = gethostbyname(hostname);
//	bzero((char *)&me,sizeof(me));
	memset(&me, 0, sizeof(me));
	me.sin_family = AF_INET;
	me.sin_port = htons(PORT);
//	me.sin_addr = *(struct in_addr *)host->h_addr;
	me.sin_addr.s_addr = INADDR_ANY;
	s0 = socket(AF_INET, SOCK_STREAM, 0);
	
	if(bind(s0, (const struct sockaddr *)&me, sizeof(me)) < 0){
		fprintf(stderr,"cannot bind socket\n");
		exit(1);
	}
	
	listen(s0,1);
	s = accept(s0,NULL,NULL);
	
	while (fgets(buf, 512, stdin)) {
		n = strlen(buf);
		send(s, buf, n, 0);
	}

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