서버의 경우 1 user 1 thread 임.
~cpp
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <process.h>
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<dwLow;) {
SetFilePointer (hFileIn, i, NULL, FILE_BEGIN);
ReadFile (hFileIn, szBuffer, sizeof (szBuffer), &nRead, NULL);
nSended = send (socket, szBuffer, nRead, NULL);
i+= max (0, nSended);
if (nSended == SOCKET_ERROR) {
printf ("Error : %d n", WSAGetLastError ());
}
else {
printf ("Current : %d / %d (%d)n", dwLow, i, nSended);
}
Sleep (100);
}
CloseHandle (hFileIn);
return 0;
}
int main (void)
{
WSADATA wsaData;
sockaddr_in local, from;
SOCKET socketListen;
SOCKET socketClient;
WSACleanup ();
DWORD dwTemp;
if (WSAStartup (0x202, &wsaData) == SOCKET_ERROR) {
printf ("error.. %dn", WSAGetLastError ());
WSACleanup ();
return 0;
}
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons (5000);
socketListen = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);
if (socketListen == INVALID_SOCKET) {
printf ("create socket error.. %dn", WSAGetLastError ());
WSACleanup ();
return 0;
}
if (bind (socketListen, (sockaddr *)&local, sizeof (local)) == SOCKET_ERROR) {
printf ("bind error.. %dn", WSAGetLastError ());
WSACleanup ();
return 0;
}
if (listen (socketListen, 5) == SOCKET_ERROR) {
printf ("listen error.. %dn", WSAGetLastError ());
WSACleanup ();
return 0;
}
BOOL bFlag = TRUE;
WSAEVENT hEvent = WSACreateEvent ();
WSAEVENT hEvent2 = WSACreateEvent ();
WSAEVENT Eventarry[2];
Eventarry[0] = hEvent;
Eventarry[1] = hEvent2;
WSANETWORKEVENTS NetworkEvents;
DWORD WaitStatus;
int addlen = sizeof (from);
WSAEventSelect (socketListen, hEvent, FD_ALL_EVENTS);
printf ("Waiting.. n");
while (bFlag) {
WaitStatus = WSAWaitForMultipleEvents (2, Eventarry, FALSE, 1000, FALSE);
if (WaitStatus == WSA_WAIT_EVENT_0) {
if (WSAEnumNetworkEvents (socketListen, hEvent, &NetworkEvents) != 0) {
printf ("Error.... %dn", GetLastError ());
bFlag = 0;
}
if (NetworkEvents.lNetworkEvents & FD_ACCEPT) {
if (NetworkEvents.iErrorCode [FD_ACCEPT_BIT] == 0) {
socketClient = accept (socketListen, (sockaddr *)&from, &addlen);
printf ("Accepted... !n");
//send (socketClient, "hugugu~rn", 9, NULL);
CreateThread (NULL, NULL, Threading, &socketClient, NULL, &dwTemp);
WSAEventSelect (socketClient, hEvent2, FD_ALL_EVENTS);
}
}
}
if (WaitStatus == (WSA_WAIT_EVENT_0 + 1) ) {
if (WSAEnumNetworkEvents (socketClient, hEvent2, &NetworkEvents) != 0) {
printf ("Error.... %dn", GetLastError ());
// bFlag = 0;
}
/*
if (NetworkEvents.lNetworkEvents & FD_READ) {
DWORD dwDataReaded;
char* szBuffer;
printf ("Data Received... n");
ioctlsocket (socketClient, FIONREAD, &dwDataReaded);
szBuffer = (char *)LocalAlloc (LPTR, dwDataReaded);
recv (socketClient, szBuffer, dwDataReaded, NULL);
printf ("Data : %s (%d)n", szBuffer, dwDataReaded);
LocalFree (szBuffer);
}
*/
if (NetworkEvents.lNetworkEvents & FD_CLOSE) {
closesocket (socketClient);
printf ("closed... !n");
// bFlag = 0;
}
}
}
printf ("Program end.. n");
WSACloseEvent (hEvent);
WSACloseEvent (hEvent2);
WSACleanup ();
return 0;
}