Windows Socket API. Win32 API 에 기본적으로 지원한다. 일반적인 버클리 소켓 스타일과 Windows 자체 스타일의 비동기소켓 (윈도우 메세지를 이용하는 방법, IOCP) API 를 제공해준다. 다음은 화일보내고 받기 관련 Winsock API 간단 예제. (옛날 예제삼아 만든 소스여서 직관적이지가 않긴 하군 -_-; 그냥 이해의 차원정도) === 보내기 소스 (서버) === 서버의 경우 1 user 1 thread 임. {{{~cpp #include #include #include #include DWORD WINAPI Threading (LPVOID args) { SOCKET* pSocket = (SOCKET*)args; SOCKET socket = *pSocket; HANDLE hFileIn; char szBuffer[8192]=""; DWORD nRead, nWrite, i; DWORD dwLow, dwHigh; int nSended; hFileIn = CreateFile ("d:\test.mp3", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); // send (*pSocket, "Data", 5, NULL); dwLow = GetFileSize (hFileIn, &dwHigh); for (i=0;i #include #include int main (void) { WSADATA wsaData; sockaddr_in local, from; SOCKET socketClient; SOCKADDR_IN ServerAddress; //소켓의 주소 int i=0; int nSize; WSACleanup (); DWORD dwTemp; HANDLE hFileOut; DWORD nWrite; if (WSAStartup (0x202, &wsaData) == SOCKET_ERROR) { printf ("error.. %dn", WSAGetLastError ()); WSACleanup (); return 0; } char szCommand[8]=""; char szHostName[40]; gethostname(szHostName, sizeof(szHostName)); HOSTENT FAR* lphostent = ::gethostbyname(szHostName); printf ("IP : %s n",inet_ntoa(*((struct in_addr*)lphostent->h_addr))); socketClient = socket (AF_INET, SOCK_STREAM, IPPROTO_IP); if (socketClient == INVALID_SOCKET) { printf ("create socket error.. %dn", WSAGetLastError ()); WSACleanup (); return 0; } ServerAddress.sin_family = AF_INET; //IP주소값. INADDR_ANY는 아무거나입니다. ^^; ServerAddress.sin_addr.s_addr = inet_addr( "127.0.0.1" ); ServerAddress.sin_port = htons( 1002 ); //포트번호 connect (socketClient, (struct sockaddr*)&ServerAddress, sizeof (ServerAddress)); WSAEVENT hEvent = WSACreateEvent (); WSANETWORKEVENTS NetworkEvents; DWORD WaitStatus; WSAEventSelect (socketClient, hEvent, FD_ALL_EVENTS); hFileOut = CreateFile ("testdata.mp3", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL); while (1) { WaitStatus = WSAWaitForMultipleEvents (1, &hEvent, FALSE, 10, FALSE); if (WaitStatus == WSA_WAIT_EVENT_0) { if (WSAEnumNetworkEvents (socketClient, hEvent, &NetworkEvents) != 0) { printf ("Error.... %dn", GetLastError ()); } if (NetworkEvents.lNetworkEvents & FD_CONNECT) { if (NetworkEvents.iErrorCode [FD_CONNECT_BIT] == 0) { printf ("Connect... !n"); // TEST_FILE_REQUARE 0xF0 szCommand[0] = 0xF0; send (socketClient, szCommand, 8, NULL); } } if (NetworkEvents.lNetworkEvents & FD_READ) { DWORD dwDataReaded; char* szBuffer; ioctlsocket (socketClient, FIONREAD, &dwDataReaded); szBuffer = (char *)LocalAlloc (LPTR, dwDataReaded); recv (socketClient, szBuffer, dwDataReaded, NULL); WriteFile (hFileOut, szBuffer, dwDataReaded, &nWrite, NULL); i+= dwDataReaded; printf ("Data Received... %d (%d)n", i, dwDataReaded); LocalFree (szBuffer); } } } closesocket (socketClient); WSACleanup (); return 0; } }}} ---- ["도구분류"]