레이블이 Socket Programming (Windows)인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Socket Programming (Windows)인 게시물을 표시합니다. 모든 게시물 표시

2016년 3월 27일 일요일

Web Server based on MultiThread

  • HTTP Request/Response Process


  • Web Server is Server that transfer HTML(HyperText Markup Language) based on HTTP(HyperText Transfer Protocol). Web Server make sentence using HTTP standard. 
  • Running Screen




Source Code



2016년 3월 25일 금요일

Chatting Program using IOCP

  • IOCP Server Model

  • IOCP (Input Output Completion Port)

 IOCP can process several clients request by IO Thread. IOCP can assign Threads to process after IO completed. That is, IO Information completed is enrolled to Kernel Object named Completion Port Object.

  • Accept Thread (=Main Thread)
1. generate Completion Port Object
   hComPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0)

2. connect Socket with Completion Port Object.
   CreateIoCompletionPort((HANDLE)hClntSock, hComPort, (DWORD)handleInfo, 0);

  • IO Threads
1. check IO completed
   GetQueuedCompletionStatus(hComPort, &bytesTrans, (LPDWORD)&handleInfo, (LPOVERLAPPED*)&ioInfo, INFINITE);

(LPDWORD)&handleInfo <=  CreateIoCompletionPort((HANDLE)hClntSock, hComPort, (DWORD)handleInfo, 0);
(LPOVERLAPPED*)&ioInfo <= WSARecv(handleInfo->hClntSock, &(ioInfo->wsaBuf), 1, &recvBytes, &flags, &(ioInfo->overlapped), NULL);

2. IO Process

  • IOCP Advantagement
1. IO don't need to compose repetitive statement in comparison with 'select' Server Model
2. OS manage sockets as enrolling socket to CP(Completion Port) Object
3. IOCP can adjust Thread number to process IO. Thus IOCP can block performance deterioration according to context switching.
4. IOCP can efficiently use CPU because server don't wait IO completion.

  • Running Screen






2016년 3월 12일 토요일

Echo Server using Asynchronous Notification IO

1. Enroll Event Kernel Object to OS using WSACreateEvent()
   WSACreateEvent() is manual-reset mode event object creation function
   WSAEventSelect() - OS observe chage of IO according to 3th parameter state and notify user. Thus, this funtion mean that WSAEventSelect() connect event object and socket
※ WSAEventSelect(SOCKET s, WSAEVENT hEventObject, long lNetworkEvents)
 (1)  's' delivered socket generate event according to 'lNetworkEvents' delivered event
 (2)  'hEventObject' delivered handle change state into 'signaled'

ex)
newEvent = WSACreateEvent();
if (WSAEventSelect(hServSock, newEvent, FD_ACCEPT) == SOCKET_ERROR)
ErrorHandling("WSAEventSelect() error");


2. Socket have to connect Event Object as matching index value.
ex)
hSockArr[numOfClntSock] = hServSock;
hEventArr[numOfClntSock] = newEvent;
numOfClntSock++;

3. Check whether or not event generation is true using WSAWaitForMultipleEvents()
ex)
posInfo = WSAWaitForMultipleEvents(numOfClntSock, hEventArr, FALSE, WSA_INFINITE, FALSE);
startIdx = posInfo - WSA_WAIT_EVENT_0;
※ 'posInfo - WSA_WAIT_EVENT_0' is event generation start index value

4. Check Event Object signaled one by one using WSAWaitForMultipleEvents()
ex)
for (i = startIdx; i<numOfClntSock; i++)
{
int sigEventIdx = WSAWaitForMultipleEvents(1, &hEventArr[i], TRUE, 0, FALSE);

5. Classify and event type using WSAEnumNetworkEvents()
ex)
WSAEnumNetworkEvents(hSockArr[sigEventIdx], hEventArr[sigEventIdx], &netEvents);

6. Process event generated
FD_ACCEPT : Do exist connect request?
FD_READ : Do exist data recevied?
FD_CLOSE : Do exist disconnect request?
ex)
if (netEvents.lNetworkEvents & FD_ACCEPT)
 // process content
if (netEvents.lNetworkEvents & FD_READ)
 // process content
if (netEvents.lNetworkEvents & FD_CLOSE)
 // process content

Source Code



2016년 2월 10일 수요일

Data Transmission using Broadcasting




  • Data Transmission of Broadicast Method base on UDP
  • It is possible for you to send same network all hosts text data.
  • IP Broadicast Address Range :
        - Direct : nnn.nnn.nnn.255  (n : network address)
        - Local : 255.255.255.255


Data Transmission using Multicasting


  • Data Transmission of Multicast Method base on UDP
  • It is possible for you to send other network hosts that subscribe Multicast Group text data
       - Thus, TTL(Time to Live) is essential
       - Use ip_mreq struct
       => add 'ws2tcpip.h'
  • IP Multicast Address Range : 224.0.0.0 ~ 239.255.255.255

Source Code

2016년 1월 30일 토요일

Echo Client & Server based on Multiplexing








  • Server based on MultiProcessing is inefficient because it ganerate process whenever clients request connect.
  • Thus, Server provide Two or more client with service using Multiplexsing.
       Use 'select()' function.
     
       As using Multiplexsing,
       One server can receive messages which Clients sended.
       Also, It is efficient about resource because one process processes clients's connect request.
  • Caution 
       MultiProcessing unconditionally isn't inefficient. It depends on the situation.

OS : Windows

Source Download

◈ references
   picture - http://fingerdev.tistory.com/24