
/*

TCP/IP α׷ ΰ  ޾ֽ  ֳ?
https://kldp.org/node/160126


//winapi utf-8 ansi
Windows API ANSI functions and UTF-8
https://stackoverflow.com/questions/8831143/windows-api-ansi-functions-and-utf-8

[WinAPi]WinAPI ̿Ͽ cp949(ANSI) - utf8 ڵ ȯ   C/C++  
http://blog.naver.com/PostView.nhn?blogId=nimi315&logNo=50093464158

How to read text file of type utf-8 format,ANSI and Unicode in VC++  
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/00be67c0-59ca-4bbe-95f8-e89581a56309/how-to-read-text-file-of-type-utf8-formatansi-and-unicode-in-vc?forum=vclanguage

Reading characters from UTF-8 Encoded File
https://www.codeproject.com/Questions/311387/Reading-characters-from-UTF-8-Encoded-File.aspx

recv winsock


locale   
https://kldp.org/node/159457#comment-626326
*/



// :ʱȭ-ϻ-(:bind,listen,accept)-- 
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>


#include <iostream>
#include <string>


#include <conio.h>
//Error: Getch Was Not Declared In This Scope
//https://www.dreamincode.net/forums/topic/341445-error-getch-was-not-declared-in-this-scope/

//htons function
//https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-htons

//WinSock32  
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/82ee2e1b-9957-4147-ac31-4e9ac82cbd59/winsock32?forum=vcgeneral

#include <locale.h>




#pragma comment(lib,"ws2_32.lib") 

#define BUFSIZE 2018

void ErrorHandling(char* message);

using namespace std;




//[WinAPi]WinAPI ̿Ͽ cp949(ANSI) - utf8 ڵ ȯ   C/C++  
//http://blog.naver.com/PostView.nhn?blogId=nimi315&logNo=50093464158

//UTF-8  ANSI  ȯ (ѱ ¿ )
string utf8_to_ansi(string &utf8) 
{ 
    WCHAR unicode[1500]; 
    char ansi[1500]; 
 
    memset(unicode, 0, sizeof(unicode)); 
    memset(ansi, 0, sizeof(ansi)); 
 
    ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, unicode, sizeof(unicode)); 
    ::WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, sizeof(ansi), NULL, NULL); 
 
    return string(ansi); 
}




int main(int argc, char *argv[])
{
	//ѱ  
	setlocale(LC_ALL, "korean");
	
	//utf-8 ѱ ansi 
	string str = "";
	string r;
	r = utf8_to_ansi(str);
	cout << str << endl;
	cout << r << endl;
	printf("%s\n", r.c_str());

	//簪
	char hex[] = "A003A0030101000E";
	char *ptr;
	long value;
	value = strtoul(hex, &ptr, 16);
	cout << "value = " << value << endl;

	// 
	SOCKET sock, clientsock;
	WSADATA wsa; 
	struct sockaddr_in sockinfo, clientinfo; 
	int clientsize;

	string msg;
	char message[BUFSIZE];
	char buffer[BUFSIZE] = { 0 };
	
	// ʱȭ
	memset(message, 0x00, BUFSIZE);
	memset(buffer, 0x00, BUFSIZE);

	msg = "TCP/IP  α׷";
	strcpy(message, (char*)utf8_to_ansi(msg).c_str());

	//  ʱȭ
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) 
	{
		str = "Winsock DLL ";
		ErrorHandling((char*)utf8_to_ansi(str).c_str());
	}

	//TCP  
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sock == INVALID_SOCKET)
	{
		str = "Soecket  ";
		ErrorHandling((char*)utf8_to_ansi(str).c_str());
	}

	// ּ ʱȭ
	memset(&sockinfo, 0, sizeof(SOCKADDR_IN));
	sockinfo.sin_family = AF_INET; //ּü AF_INET,AF_INET6,AF_LOCAL

	//Ʈ ȣ / ּ 
	int n_port = atoi(argv[1]);						//ڿ ڷ ȯ
	sockinfo.sin_port = htons(n_port);				//ڸ Ʈũ· ȯ
	sockinfo.sin_addr.s_addr = htonl(INADDR_ANY);	//ip 

	//ϰ ּҸ bind
	if (bind(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR) 
	{
		str = "bind ";
		ErrorHandling((char*)utf8_to_ansi(str).c_str());
	}

	// listen
	if (listen(sock, 5) == SOCKET_ERROR) //[ 0,н SOCKET_ERROR]
	{
		str = "listen ";
		ErrorHandling((char*)utf8_to_ansi(str).c_str());
	}
	
	//
	str = "Ŭ̾Ʈκ  ٸ ֽϴ...";
	printf("%s\n", (char*)utf8_to_ansi(str).c_str());

	//struct sockaddr_in sockinfo Ŭ̾Ʈ   ũ⸦ ´.
	clientsize = sizeof(clientinfo);
	//ϰ Ŭ̾Ʈ  ũ.  Ŭ̾Ʈ  
	clientsock = accept(sock, (SOCKADDR*)&clientinfo, &clientsize); 
	if (clientsock == INVALID_SOCKET) //ڵ鰪̶ INVALID_SOCKET Ȯ
	{
		str = "accept ";
		ErrorHandling((char*)utf8_to_ansi(str).c_str());
	}

	// Ŭ̾Ʈ . ޽ 迭 ũ ŭ 
	printf("send : message");
	send(clientsock, message, sizeof(message), 0);

	// Ŭ̾Ʈ . hex 迭 ũ ŭ 
	send(clientsock, hex, sizeof(hex), 0);

	//  
	closesocket(sock);
	closesocket(clientsock);
	str = "";
	printf("%s\n", (char*)utf8_to_ansi(str).c_str());

	//  Cleanup
	WSACleanup();
	return 0;
}


//
void ErrorHandling(char *message) 
{
	WSACleanup();
	fputs(message, stderr);
	fputc('\n', stderr);
	_getch();
	exit(1);
}
