SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
WinSock Asynchronous
          Input/Output
         ECE 4564: Network Application Design

                       Scott F. Midkiff
               Bradley Department of Electrical
                  and Computer Engineering
      Virginia Polytechnic Institute and State University




                                              Topics
 ! Need for specialized WinSock calls
 ! Blocking and message-driven architecture
 ! Asynchronous calls
   " Message-based
   " Event-based
   " Asynchronous TCP ECHO server example
   " Database calls

 ! WinSock version checking




ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 2




                                    © 1998-2002, Scott F. Midkiff
WinSock ≠ BSD UNIX Sockets
 ! WinSock 1.1 and 2.2 “core” calls are based
     on BSD UNIX Sockets model
       "    Builds on existing knowledge base -- BSD UNIX
            Sockets widely used by network programmers
       "    Simplifies porting of applications from UNIX to
            Microsoft Windows
 ! Usual socket calls may not be the best to
     use in Windows, especially for windowed
     applications
       "    Blocking can cause problems for
            message-driven operating system
       "    May not take full advantage of Windows
            advanced I/O features
ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 3




        Message-Driven Architecture (1)
 ! Microsoft Windows is message-driven
   " System indicates “events” by providing a
     message to a program
   " The program responds to the message
   " The response may lead to new messages being
     sent at a later time or new messages may occur
     because of user actions
 ! Messages indicate different system events
   " User interface action
   " I/O completion
   " System call completion
   " Error conditions



ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 4




                                    © 1998-2002, Scott F. Midkiff
Message-Driven Architecture (2)
 ! Windows programs must check messages
     and will be unresponsive if messages are
     not checked
       "    Not an issue for console applications
 ! Multitasking scheme determines how
     processes share the processor -- and when
     messages are checked
       "    16-bit Windows (3.1 and 3.11) uses cooperative
            multitasking
       "    32-bit Windows (95, 98, ME, NT, 2000, and XP)
            uses preemptive multitasking


ECE 4564: Network Application Design (10/6/2002)                     WinSock Asynchronous I/O - 5




                    Cooperative Multitasking

  ! Processes must check
    for messages to keep
    system “alive”                                                  Message
  ! Blocked calls block the                                         Queue
    entire system

                                                     Dispatch



                             Process               Process          ...      Process

                    GetMessage()
                   PeekMessage()
ECE 4564: Network Application Design (10/6/2002)                     WinSock Asynchronous I/O - 6




                                    © 1998-2002, Scott F. Midkiff
Preemptive Multitasking (1)



                                                   ...                     Message
                                                                           Queues


                        Dispatch                             Dispatch


                          Thread                              Thread

               GetMessage()                               GetMessage()
              PeekMessage()                              PeekMessage()

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 7




               Preemptive Multitasking (2)
 ! Each thread has a message queue
 ! Operating system preempts threads so that
   a single blocked thread will not block other
   threads
 ! Blocking can make an application
   unresponsive and should be avoided, at
   least in user interface threads




ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 8




                                    © 1998-2002, Scott F. Midkiff
Messages and Application Code (1)
 ! A Windows application defines a procedure
   to handle messages for a window
 ! Example: dialog process or DialogProc()
       "    hwndDlg identifies where to to send messages
       "    uMsg, with wParam and lParam, define message

   BOOL CALLBACK DialogProc(
      HWND hwndDlg, // handle to dialog box
      UINT uMsg,     // message
      WPARAM wParam, // 1st message parameter
      LPARAM lParam // 2nd message parameter
   );

ECE 4564: Network Application Design (10/6/2002)                     WinSock Asynchronous I/O - 9




     Messages and Application Code (2)
 ! Dialog procedure processes messages

  switch(uMsg)
  {
    case WM_INITDIALOG:
      // process initialization message
    case WM_COMMAND:
      // process user interface message
    case SM_EVENT:
      // process asynchronous WinSock event
    …
  }


ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 10




                                    © 1998-2002, Scott F. Midkiff
Non-Blocking WinSock Calls
 ! To prevent blocking in an application
   thread, use non-blocking WinSock calls
 ! Two approaches
       "    Non-blocking sockets
             # ioctlsocket() to make socket non-blocking

             # select() to determine readiness of socket

             # Largely portable to UNIX

       "    Asynchronous calls (preferred method)
             # Asynchronous call returns immediately

             # Completion is indicated to application (three

               notification methods)
             # Windows-specific (WSA prefix for function

               names)

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 11




                    Notification Methods (1)
 ! Three notification methods
    $ Message-driven
    % Event-driven
    & Callback routine

 ! Message-driven notification
   " Window handle passed to WSAAsyncSelect() or
     other asynchronous call
   " Message is sent to indicated window procedure
     upon completion
   " Requires a windowed application




ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 12




                                    © 1998-2002, Scott F. Midkiff
Notification Methods (2)
 ! Event-driven notification
   " Event handle passed to WSAEventSelect() or
     other asynchronous call
   " Event is signaled, application must check
   " Can be used with any application

 ! Callback routine
   " Pointer to callback procedure is passed to call
     such as WSASend() or WSARecv()
   " WinSock “calls back” at indicated procedure
     when action is completed
   " Can be used with any application




ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 13




                       WSAAsyncSelect() (1)
 ! Makes socket non-blocking
   " No need to call ioctlsocket()

 ! Allows application to tell WinSock that
     messages should be sent for certain events
       "    When a connection is established
       "    When data is ready for receiving from socket
       "    When data can be sent to socket (following a
            situation when data cannot be sent)
       "    When socket is closed by peer
       "    When out-of-band data is received
       "    When socket or socket group quality of service
            (QoS) changes

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 14




                                    © 1998-2002, Scott F. Midkiff
WSAAsyncSelect() (2)
 ! Note that one message is created for any
     event — cannot have different messages
       "    wParam is the socket descriptor
       "    lParam contains event code and error code
             # WSAGETSELECTEVENT(lParam)

             # WSAGETSELECTERROR(lParam)



 int WSAAsyncSelect (
    SOCKET s,          //                              socket
    HWND hWnd,         //                              window for messages
    unsigned int wMsg, //                              message number
    long lEvent        //                              types of events
 );

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 15




                       WSAAsyncSelect() (3)
 ! Example call
   " Define SM_EVENT as the message to be sent to
     window with handle hWnd
   " Check for connect, close, read, and write events
     on socket sock
 #define SM_EVENT WM_USER + 1

 cc = WSAAsyncSelect (
    sock,
    hWnd,
    SM_EVENT,
    FD_CONNECT| FD_CLOSE | FD_READ | FD_WRITE
 );

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 16




                                    © 1998-2002, Scott F. Midkiff
WSAEventSelect() (1)
 ! Note that WSAAsyncSelect() requires that
   the application have a window to receive
   messages
 ! Windows are not always suitable
       "    Console applications are simpler to design
       "    Long-running servers
 ! WSAEventSelect() allows asynchronous
     notifications to be provided to applications,
     which may or may not have a window
       "    Sets an event object instead of posting a
            message to a window handle

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 17




                        WSAEventSelect() (2)
 ! Makes socket non-blocking
 ! Allows application to specify event types for
     which the event will be set
       "    Same set as for WSAAsyncSelect()
       "    Event bit mask

  int WSAEventSelect (
     SOCKET s,              // socket
     WSAEVENT hEventObject, // event handle
     long lNetworkEvents    // event bit mask
  );



ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 18




                                    © 1998-2002, Scott F. Midkiff
Asynchronous Echo: Overview
 ! TCP ECHO server with asynchronous event
   notification (aechod.cpp)
 ! Similar in concept to singly-threaded
   concurrent TCP ECHO server
   (tcpmechod.cpp)
 ! Two classes of sockets
       "    Master socket (index = 0)
             # Monitor when ready for accept (FD_ACCEPT)

       "    Sockets for connections to clients (index > 0)
             # Monitor when ready for read or write or when

               a close occurs (FD_READ | FD_WRITE |
               FD_CLOSE)

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 19




             Asynchronous Echo: Calls (1)
 ! WSACreateEvent()
   " Used to create an event object
   " One event object per socket
   " Events[NumSock] = WSACreateEvent();

 ! WSAEventSelect()
   " Used to indicate monitored event types
   " Master socket:
     WSAEventSelect(temp_sock, Events[0],
     FD_ACCEPT);
   " Per connection socket:
     WSAEventSelect(temp_sock, Events[NumSock],
     FD_READ | FD_WRITE | FD_CLOSE);

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 20




                                    © 1998-2002, Scott F. Midkiff
Asynchronous Echo: Calls (2)
 ! WaitForMultipleEvents()
   " Used to wait for any one event to be signaled
   " SigEvent = WSAWaitForMultipleEvents(
     NumSocks, Events, FALSE, WSA_INFINITE,
     FALSE );

     DWORD WSAWaitForMultipleEvents(
      DWORD cEvents, // number of events
      const WSAEVENT FAR *lphEvents, // array
      BOOL fWaitAll, // wait for all events
      DWORD dwTimeOUT, // time out value
      BOOL fAlertable // make alertable
     );

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 21




             Asynchronous Echo: Calls (3)
 ! WSAEnumNetworkEvents()
   " Determine triggering event, e.g. FD_READ

  int WSAEnumNetworkEvents (
   SOCKET s,
   WSAEVENT hEventObject,
   LPWSANETWORKEVENTS lpNetworkEvents
  );

 typedef struct _WSANETWORKEVENTS {
      long lNetworkEvents;
      int iErrorCodes[FD_MAX_EVENTS];
 }
 WSANETWORKEVENTS, FAR * LPWSANETWORKEVENTS;
ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 22




                                    © 1998-2002, Scott F. Midkiff
Asynchronous Echo: Calls (4)
 ! WSACloseEvent()
   " Releases event object
   " WSACloseEvent(Events[EventNum]);




ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 23




   Asynchronous Echo: Data Structures
 ! Array of event handles
   " WSAEVENT Events[MAXSOCKS];

 ! Array of socket information structures
                struct info {
                   SOCKET sock;
                   int    state;
                   char buf[BUFSIZE];
                   char * bufptr;
                   int buflen;
                };

                struct info * Socks[MAXSOCKS];

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 24




                                    © 1998-2002, Scott F. Midkiff
Asynchronous Echo: Operation




                                                                                           FD_WRITE && block FD_WRITE && block
 FD_READ && !block

                                              awaiting
                         FD_ACCEPT
 recv(), send()


                         accept()
                                            FD_READ && block




                                                                                                             send()
                                            recv(), send()
                       READING                                  WRITING
                                         FD_WRITE && !block
                                         send()
                                                    FD_CLOSE
                     FD_CLOSE
                     cleanup                                        CLOSING
                                 closed




                                                                                           send()
                                                    FD_WRITE && !block
                                                    send(), cleanup

ECE 4564: Network Application Design (10/6/2002)                     WinSock Asynchronous I/O - 25




              Asynchronous Database Functions
 ! Asynchronous version of getXbyY() calls
   " WSAGetHostByAddr()
   " WSAGetHostByName()
   " WSAGetProtoByNumber()
   " WSAGetProtoByName()
   " WSAGetServByPort()
   " WSAGetServByName()

 ! Most important are WSAGetHostByAddr()
   and WSAGetHostByName() because of long
   potential delays
 ! Special call to cancel outstanding request
              "      WSACancelAsyncRequest()
ECE 4564: Network Application Design (10/6/2002)                     WinSock Asynchronous I/O - 26




                                    © 1998-2002, Scott F. Midkiff
WSAAsyncGetHostByName()
 ! Identify window and message as in
   WSAAsyncSelect()
 ! Provide host name and return buffer
       "    Buffer must be large enough for all information
            since no information is stored by WinSock

  HANDLE WSAAsyncGetHostByName                                  (
     HWND hWnd,             //                                  window for msg
     unsigned int wMsg,     //                                  message id
     const char FAR * name, //                                  host name
     char FAR * buf,        //                                  return info
     int buflen             //                                  buffer length
  );

ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 27




            WinSock Version Checking (1)
 ! WSAStartup()
   " Version requested is an input parameter
   " Returned LPWSADATA structure indicates
      # Version that WinSock expects application to

        use
      # Highest version supported by this WinSock


 ! For example, if version 1.1 is requested from
     WinSock 2.2
       "    Version requested is 1.1
       "    Version expected (wVersion) is 1.1
       "    Highest version supported (wHighVersion) is 2.2


ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 28




                                    © 1998-2002, Scott F. Midkiff
WinSock Version Checking (2)
               Results from WSAStartup()
       Version  Version
       Running Requested wVersion wHighVersion
            1.1                    1.0             WSAVERNOTSUPPORTED*

            1.1                    1.1                1.1                   1.1
            1.1                    2.0                1.1                   1.1
            2.0                    1.1                1.1                   2.0
            2.0                    2.2                2.0                   2.0
            2.2                    2.0                2.0                   2.2
            2.2                    2.2                2.2                   2.2
                            * Error return from WSAStartup()
ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 29




            WinSock Version Checking (3)
 ! A robust application should ensure that
     requested version is supported

  if(WSAStartup(wVersionReq, &wsaData)!= 0){
     // Check for error return
  }
  else if (wsaData.wVersion != wVersionReq) {
     // Version not supported
  }
  else {
     // Version is supported
  }


ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 30




                                    © 1998-2002, Scott F. Midkiff
You should now be able to …
 ! Indicate the benefit of non-blocking calls in
   a message-driven operating system such as
   Microsoft Windows
 ! Describe basic mechanisms for notification
   including messages and events
 ! Analyze and design simple programs using
   asynchronous sockets with event-based
   notification
 ! Describe the asynchronous database calls
 ! Analyze and design code that verifies
   WinSock versions
ECE 4564: Network Application Design (10/6/2002)                    WinSock Asynchronous I/O - 31




                                    © 1998-2002, Scott F. Midkiff

Mais conteúdo relacionado

Mais procurados

NTP Project Presentation
NTP Project PresentationNTP Project Presentation
NTP Project Presentation
Andrew McGarry
 
Apresentação - Cloud Computing
Apresentação - Cloud ComputingApresentação - Cloud Computing
Apresentação - Cloud Computing
UniCloud
 
What is Virtualization
What is VirtualizationWhat is Virtualization
What is Virtualization
Israel Marcus
 
network monitoring system ppt
network monitoring system pptnetwork monitoring system ppt
network monitoring system ppt
ashutosh rai
 
Azure architecture
Azure architectureAzure architecture
Azure architecture
Amal Dev
 

Mais procurados (20)

NTP Project Presentation
NTP Project PresentationNTP Project Presentation
NTP Project Presentation
 
Prod-Like Integration Testing for Distributed Containerized Applications
Prod-Like Integration Testing for Distributed Containerized ApplicationsProd-Like Integration Testing for Distributed Containerized Applications
Prod-Like Integration Testing for Distributed Containerized Applications
 
Internet protocol version 6 (i pv6) migration IPv4/IPv6
Internet protocol version 6 (i pv6) migration IPv4/IPv6Internet protocol version 6 (i pv6) migration IPv4/IPv6
Internet protocol version 6 (i pv6) migration IPv4/IPv6
 
3 Router Configuration - Cisco Packet Tracer
3 Router Configuration - Cisco Packet Tracer 3 Router Configuration - Cisco Packet Tracer
3 Router Configuration - Cisco Packet Tracer
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Apresentação - Cloud Computing
Apresentação - Cloud ComputingApresentação - Cloud Computing
Apresentação - Cloud Computing
 
CCNA v6.0 ITN - Chapter 03
CCNA v6.0 ITN - Chapter 03CCNA v6.0 ITN - Chapter 03
CCNA v6.0 ITN - Chapter 03
 
What is Virtualization
What is VirtualizationWhat is Virtualization
What is Virtualization
 
pfSense - Proxy integrado ao AD Regras por usuários e grupos
pfSense - Proxy integrado ao AD Regras por usuários e grupospfSense - Proxy integrado ao AD Regras por usuários e grupos
pfSense - Proxy integrado ao AD Regras por usuários e grupos
 
network monitoring system ppt
network monitoring system pptnetwork monitoring system ppt
network monitoring system ppt
 
Migrating from MFC to Qt
Migrating from MFC to QtMigrating from MFC to Qt
Migrating from MFC to Qt
 
An Introduction to VMware NSX
An Introduction to VMware NSXAn Introduction to VMware NSX
An Introduction to VMware NSX
 
Ccnp workbook network bulls
Ccnp workbook network bullsCcnp workbook network bulls
Ccnp workbook network bulls
 
10 Steps to Improve Your Network Monitoring
10 Steps to Improve Your Network Monitoring10 Steps to Improve Your Network Monitoring
10 Steps to Improve Your Network Monitoring
 
Best Network Performance Monitoring Tool
Best Network Performance Monitoring ToolBest Network Performance Monitoring Tool
Best Network Performance Monitoring Tool
 
CCNA 2 Routing and Switching v5.0 Chapter 1
CCNA 2 Routing and Switching v5.0 Chapter 1CCNA 2 Routing and Switching v5.0 Chapter 1
CCNA 2 Routing and Switching v5.0 Chapter 1
 
CCNA 1 Routing and Switching v5.0 Chapter 3
CCNA 1 Routing and Switching v5.0 Chapter 3CCNA 1 Routing and Switching v5.0 Chapter 3
CCNA 1 Routing and Switching v5.0 Chapter 3
 
Switch security
Switch securitySwitch security
Switch security
 
Azure architecture
Azure architectureAzure architecture
Azure architecture
 
Nat
NatNat
Nat
 

Semelhante a WinSock Asynchronous Input/Output

Introducing MQTT
Introducing MQTTIntroducing MQTT
Introducing MQTT
Andy Piper
 
Socketプログラム Silverlight for Windows Phoneへの移植のポイント
Socketプログラム Silverlight for Windows Phoneへの移植のポイントSocketプログラム Silverlight for Windows Phoneへの移植のポイント
Socketプログラム Silverlight for Windows Phoneへの移植のポイント
Shin Ise
 
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
INSIGHT FORENSIC
 
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
INSIGHT FORENSIC
 
Configuring cisco site to site ip sec vpn with dynamic ip endpoint cisco routers
Configuring cisco site to site ip sec vpn with dynamic ip endpoint cisco routersConfiguring cisco site to site ip sec vpn with dynamic ip endpoint cisco routers
Configuring cisco site to site ip sec vpn with dynamic ip endpoint cisco routers
phosika sithisane
 

Semelhante a WinSock Asynchronous Input/Output (20)

T2: What the Second Generation Holds
T2: What the Second Generation HoldsT2: What the Second Generation Holds
T2: What the Second Generation Holds
 
Symbian OS
Symbian  OS Symbian  OS
Symbian OS
 
Mod Security
Mod SecurityMod Security
Mod Security
 
Stuxnet dc9723
Stuxnet dc9723Stuxnet dc9723
Stuxnet dc9723
 
Video phone
Video phoneVideo phone
Video phone
 
Zaccone Carmelo - IPv6 and security from a user’s point of view
Zaccone Carmelo - IPv6 and security from a user’s point of view Zaccone Carmelo - IPv6 and security from a user’s point of view
Zaccone Carmelo - IPv6 and security from a user’s point of view
 
#OSSPARIS19: Construire des applications IoT "secure-by-design" - Thomas Gaza...
#OSSPARIS19: Construire des applications IoT "secure-by-design" - Thomas Gaza...#OSSPARIS19: Construire des applications IoT "secure-by-design" - Thomas Gaza...
#OSSPARIS19: Construire des applications IoT "secure-by-design" - Thomas Gaza...
 
Introducing MQTT
Introducing MQTTIntroducing MQTT
Introducing MQTT
 
Symbian OS - Communication And Messaging
Symbian OS - Communication And MessagingSymbian OS - Communication And Messaging
Symbian OS - Communication And Messaging
 
The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017
 
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
 
ucOS
ucOSucOS
ucOS
 
Blast off!
Blast off!Blast off!
Blast off!
 
Windows Filtering Platform And Winsock Kernel
Windows Filtering Platform And Winsock KernelWindows Filtering Platform And Winsock Kernel
Windows Filtering Platform And Winsock Kernel
 
Socketプログラム Silverlight for Windows Phoneへの移植のポイント
Socketプログラム Silverlight for Windows Phoneへの移植のポイントSocketプログラム Silverlight for Windows Phoneへの移植のポイント
Socketプログラム Silverlight for Windows Phoneへの移植のポイント
 
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
 
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)(120715) #fitalk   the era of cyber sabotage and warfare (case study - stuxnet)
(120715) #fitalk the era of cyber sabotage and warfare (case study - stuxnet)
 
Practical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacksPractical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacks
 
CVE 2012-1889 Microsoft XML core services uninitialized memory vulnerability
CVE 2012-1889 Microsoft XML core services uninitialized memory vulnerabilityCVE 2012-1889 Microsoft XML core services uninitialized memory vulnerability
CVE 2012-1889 Microsoft XML core services uninitialized memory vulnerability
 
Configuring cisco site to site ip sec vpn with dynamic ip endpoint cisco routers
Configuring cisco site to site ip sec vpn with dynamic ip endpoint cisco routersConfiguring cisco site to site ip sec vpn with dynamic ip endpoint cisco routers
Configuring cisco site to site ip sec vpn with dynamic ip endpoint cisco routers
 

Último

Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in PakistanChallenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
vineshkumarsajnani12
 
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al MizharAl Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
allensay1
 

Último (20)

Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDINGBerhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Berhampur 70918*19311 CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
 
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
 
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in PakistanChallenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
 
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al MizharAl Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
 
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTSDurg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
PARK STREET 💋 Call Girl 9827461493 Call Girls in Escort service book now
PARK STREET 💋 Call Girl 9827461493 Call Girls in  Escort service book nowPARK STREET 💋 Call Girl 9827461493 Call Girls in  Escort service book now
PARK STREET 💋 Call Girl 9827461493 Call Girls in Escort service book now
 
Cannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 UpdatedCannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 Updated
 
JAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR ESCORTS
JAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR  ESCORTSJAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR  ESCORTS
JAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR ESCORTS
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptx
 
Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdf
 
UAE Bur Dubai Call Girls ☏ 0564401582 Call Girl in Bur Dubai
UAE Bur Dubai Call Girls ☏ 0564401582 Call Girl in Bur DubaiUAE Bur Dubai Call Girls ☏ 0564401582 Call Girl in Bur Dubai
UAE Bur Dubai Call Girls ☏ 0564401582 Call Girl in Bur Dubai
 
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAIGetting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
 
Berhampur Call Girl Just Call 8084732287 Top Class Call Girl Service Available
Berhampur Call Girl Just Call 8084732287 Top Class Call Girl Service AvailableBerhampur Call Girl Just Call 8084732287 Top Class Call Girl Service Available
Berhampur Call Girl Just Call 8084732287 Top Class Call Girl Service Available
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Chennai Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Av...
Chennai Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Av...Chennai Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Av...
Chennai Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Av...
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 
WheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond InsightsWheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond Insights
 

WinSock Asynchronous Input/Output

  • 1. WinSock Asynchronous Input/Output ECE 4564: Network Application Design Scott F. Midkiff Bradley Department of Electrical and Computer Engineering Virginia Polytechnic Institute and State University Topics ! Need for specialized WinSock calls ! Blocking and message-driven architecture ! Asynchronous calls " Message-based " Event-based " Asynchronous TCP ECHO server example " Database calls ! WinSock version checking ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 2 © 1998-2002, Scott F. Midkiff
  • 2. WinSock ≠ BSD UNIX Sockets ! WinSock 1.1 and 2.2 “core” calls are based on BSD UNIX Sockets model " Builds on existing knowledge base -- BSD UNIX Sockets widely used by network programmers " Simplifies porting of applications from UNIX to Microsoft Windows ! Usual socket calls may not be the best to use in Windows, especially for windowed applications " Blocking can cause problems for message-driven operating system " May not take full advantage of Windows advanced I/O features ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 3 Message-Driven Architecture (1) ! Microsoft Windows is message-driven " System indicates “events” by providing a message to a program " The program responds to the message " The response may lead to new messages being sent at a later time or new messages may occur because of user actions ! Messages indicate different system events " User interface action " I/O completion " System call completion " Error conditions ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 4 © 1998-2002, Scott F. Midkiff
  • 3. Message-Driven Architecture (2) ! Windows programs must check messages and will be unresponsive if messages are not checked " Not an issue for console applications ! Multitasking scheme determines how processes share the processor -- and when messages are checked " 16-bit Windows (3.1 and 3.11) uses cooperative multitasking " 32-bit Windows (95, 98, ME, NT, 2000, and XP) uses preemptive multitasking ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 5 Cooperative Multitasking ! Processes must check for messages to keep system “alive” Message ! Blocked calls block the Queue entire system Dispatch Process Process ... Process GetMessage() PeekMessage() ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 6 © 1998-2002, Scott F. Midkiff
  • 4. Preemptive Multitasking (1) ... Message Queues Dispatch Dispatch Thread Thread GetMessage() GetMessage() PeekMessage() PeekMessage() ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 7 Preemptive Multitasking (2) ! Each thread has a message queue ! Operating system preempts threads so that a single blocked thread will not block other threads ! Blocking can make an application unresponsive and should be avoided, at least in user interface threads ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 8 © 1998-2002, Scott F. Midkiff
  • 5. Messages and Application Code (1) ! A Windows application defines a procedure to handle messages for a window ! Example: dialog process or DialogProc() " hwndDlg identifies where to to send messages " uMsg, with wParam and lParam, define message BOOL CALLBACK DialogProc( HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, // 1st message parameter LPARAM lParam // 2nd message parameter ); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 9 Messages and Application Code (2) ! Dialog procedure processes messages switch(uMsg) { case WM_INITDIALOG: // process initialization message case WM_COMMAND: // process user interface message case SM_EVENT: // process asynchronous WinSock event … } ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 10 © 1998-2002, Scott F. Midkiff
  • 6. Non-Blocking WinSock Calls ! To prevent blocking in an application thread, use non-blocking WinSock calls ! Two approaches " Non-blocking sockets # ioctlsocket() to make socket non-blocking # select() to determine readiness of socket # Largely portable to UNIX " Asynchronous calls (preferred method) # Asynchronous call returns immediately # Completion is indicated to application (three notification methods) # Windows-specific (WSA prefix for function names) ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 11 Notification Methods (1) ! Three notification methods $ Message-driven % Event-driven & Callback routine ! Message-driven notification " Window handle passed to WSAAsyncSelect() or other asynchronous call " Message is sent to indicated window procedure upon completion " Requires a windowed application ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 12 © 1998-2002, Scott F. Midkiff
  • 7. Notification Methods (2) ! Event-driven notification " Event handle passed to WSAEventSelect() or other asynchronous call " Event is signaled, application must check " Can be used with any application ! Callback routine " Pointer to callback procedure is passed to call such as WSASend() or WSARecv() " WinSock “calls back” at indicated procedure when action is completed " Can be used with any application ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 13 WSAAsyncSelect() (1) ! Makes socket non-blocking " No need to call ioctlsocket() ! Allows application to tell WinSock that messages should be sent for certain events " When a connection is established " When data is ready for receiving from socket " When data can be sent to socket (following a situation when data cannot be sent) " When socket is closed by peer " When out-of-band data is received " When socket or socket group quality of service (QoS) changes ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 14 © 1998-2002, Scott F. Midkiff
  • 8. WSAAsyncSelect() (2) ! Note that one message is created for any event — cannot have different messages " wParam is the socket descriptor " lParam contains event code and error code # WSAGETSELECTEVENT(lParam) # WSAGETSELECTERROR(lParam) int WSAAsyncSelect ( SOCKET s, // socket HWND hWnd, // window for messages unsigned int wMsg, // message number long lEvent // types of events ); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 15 WSAAsyncSelect() (3) ! Example call " Define SM_EVENT as the message to be sent to window with handle hWnd " Check for connect, close, read, and write events on socket sock #define SM_EVENT WM_USER + 1 cc = WSAAsyncSelect ( sock, hWnd, SM_EVENT, FD_CONNECT| FD_CLOSE | FD_READ | FD_WRITE ); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 16 © 1998-2002, Scott F. Midkiff
  • 9. WSAEventSelect() (1) ! Note that WSAAsyncSelect() requires that the application have a window to receive messages ! Windows are not always suitable " Console applications are simpler to design " Long-running servers ! WSAEventSelect() allows asynchronous notifications to be provided to applications, which may or may not have a window " Sets an event object instead of posting a message to a window handle ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 17 WSAEventSelect() (2) ! Makes socket non-blocking ! Allows application to specify event types for which the event will be set " Same set as for WSAAsyncSelect() " Event bit mask int WSAEventSelect ( SOCKET s, // socket WSAEVENT hEventObject, // event handle long lNetworkEvents // event bit mask ); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 18 © 1998-2002, Scott F. Midkiff
  • 10. Asynchronous Echo: Overview ! TCP ECHO server with asynchronous event notification (aechod.cpp) ! Similar in concept to singly-threaded concurrent TCP ECHO server (tcpmechod.cpp) ! Two classes of sockets " Master socket (index = 0) # Monitor when ready for accept (FD_ACCEPT) " Sockets for connections to clients (index > 0) # Monitor when ready for read or write or when a close occurs (FD_READ | FD_WRITE | FD_CLOSE) ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 19 Asynchronous Echo: Calls (1) ! WSACreateEvent() " Used to create an event object " One event object per socket " Events[NumSock] = WSACreateEvent(); ! WSAEventSelect() " Used to indicate monitored event types " Master socket: WSAEventSelect(temp_sock, Events[0], FD_ACCEPT); " Per connection socket: WSAEventSelect(temp_sock, Events[NumSock], FD_READ | FD_WRITE | FD_CLOSE); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 20 © 1998-2002, Scott F. Midkiff
  • 11. Asynchronous Echo: Calls (2) ! WaitForMultipleEvents() " Used to wait for any one event to be signaled " SigEvent = WSAWaitForMultipleEvents( NumSocks, Events, FALSE, WSA_INFINITE, FALSE ); DWORD WSAWaitForMultipleEvents( DWORD cEvents, // number of events const WSAEVENT FAR *lphEvents, // array BOOL fWaitAll, // wait for all events DWORD dwTimeOUT, // time out value BOOL fAlertable // make alertable ); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 21 Asynchronous Echo: Calls (3) ! WSAEnumNetworkEvents() " Determine triggering event, e.g. FD_READ int WSAEnumNetworkEvents ( SOCKET s, WSAEVENT hEventObject, LPWSANETWORKEVENTS lpNetworkEvents ); typedef struct _WSANETWORKEVENTS { long lNetworkEvents; int iErrorCodes[FD_MAX_EVENTS]; } WSANETWORKEVENTS, FAR * LPWSANETWORKEVENTS; ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 22 © 1998-2002, Scott F. Midkiff
  • 12. Asynchronous Echo: Calls (4) ! WSACloseEvent() " Releases event object " WSACloseEvent(Events[EventNum]); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 23 Asynchronous Echo: Data Structures ! Array of event handles " WSAEVENT Events[MAXSOCKS]; ! Array of socket information structures struct info { SOCKET sock; int state; char buf[BUFSIZE]; char * bufptr; int buflen; }; struct info * Socks[MAXSOCKS]; ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 24 © 1998-2002, Scott F. Midkiff
  • 13. Asynchronous Echo: Operation FD_WRITE && block FD_WRITE && block FD_READ && !block awaiting FD_ACCEPT recv(), send() accept() FD_READ && block send() recv(), send() READING WRITING FD_WRITE && !block send() FD_CLOSE FD_CLOSE cleanup CLOSING closed send() FD_WRITE && !block send(), cleanup ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 25 Asynchronous Database Functions ! Asynchronous version of getXbyY() calls " WSAGetHostByAddr() " WSAGetHostByName() " WSAGetProtoByNumber() " WSAGetProtoByName() " WSAGetServByPort() " WSAGetServByName() ! Most important are WSAGetHostByAddr() and WSAGetHostByName() because of long potential delays ! Special call to cancel outstanding request " WSACancelAsyncRequest() ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 26 © 1998-2002, Scott F. Midkiff
  • 14. WSAAsyncGetHostByName() ! Identify window and message as in WSAAsyncSelect() ! Provide host name and return buffer " Buffer must be large enough for all information since no information is stored by WinSock HANDLE WSAAsyncGetHostByName ( HWND hWnd, // window for msg unsigned int wMsg, // message id const char FAR * name, // host name char FAR * buf, // return info int buflen // buffer length ); ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 27 WinSock Version Checking (1) ! WSAStartup() " Version requested is an input parameter " Returned LPWSADATA structure indicates # Version that WinSock expects application to use # Highest version supported by this WinSock ! For example, if version 1.1 is requested from WinSock 2.2 " Version requested is 1.1 " Version expected (wVersion) is 1.1 " Highest version supported (wHighVersion) is 2.2 ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 28 © 1998-2002, Scott F. Midkiff
  • 15. WinSock Version Checking (2) Results from WSAStartup() Version Version Running Requested wVersion wHighVersion 1.1 1.0 WSAVERNOTSUPPORTED* 1.1 1.1 1.1 1.1 1.1 2.0 1.1 1.1 2.0 1.1 1.1 2.0 2.0 2.2 2.0 2.0 2.2 2.0 2.0 2.2 2.2 2.2 2.2 2.2 * Error return from WSAStartup() ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 29 WinSock Version Checking (3) ! A robust application should ensure that requested version is supported if(WSAStartup(wVersionReq, &wsaData)!= 0){ // Check for error return } else if (wsaData.wVersion != wVersionReq) { // Version not supported } else { // Version is supported } ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 30 © 1998-2002, Scott F. Midkiff
  • 16. You should now be able to … ! Indicate the benefit of non-blocking calls in a message-driven operating system such as Microsoft Windows ! Describe basic mechanisms for notification including messages and events ! Analyze and design simple programs using asynchronous sockets with event-based notification ! Describe the asynchronous database calls ! Analyze and design code that verifies WinSock versions ECE 4564: Network Application Design (10/6/2002) WinSock Asynchronous I/O - 31 © 1998-2002, Scott F. Midkiff