SlideShare une entreprise Scribd logo
1  sur  16
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Introduction to Go language
Introduction to Go languageIntroduction to Go language
Introduction to Go languageTzar Umang
 
Responsibility Accountability RACI Matrix
Responsibility Accountability RACI MatrixResponsibility Accountability RACI Matrix
Responsibility Accountability RACI MatrixSlideTeam
 
Docker presentation | Paris Docker Meetup
Docker presentation | Paris Docker MeetupDocker presentation | Paris Docker Meetup
Docker presentation | Paris Docker MeetupdotCloud
 
Primavera - Tutorial
Primavera - TutorialPrimavera - Tutorial
Primavera - TutorialRajeev Sharma
 
DCEU 18: Dockerfile Best Practices
DCEU 18: Dockerfile Best PracticesDCEU 18: Dockerfile Best Practices
DCEU 18: Dockerfile Best PracticesDocker, Inc.
 
Manage your bare-metal infrastructure with a CI/CD-driven approach
Manage your bare-metal infrastructure with a CI/CD-driven approachManage your bare-metal infrastructure with a CI/CD-driven approach
Manage your bare-metal infrastructure with a CI/CD-driven approachinovex GmbH
 
Odoo Performance Limits
Odoo Performance LimitsOdoo Performance Limits
Odoo Performance LimitsOdoo
 
Building a CICD Pipeline for deploying on Containers
Building a CICD Pipeline for deploying on ContainersBuilding a CICD Pipeline for deploying on Containers
Building a CICD Pipeline for deploying on ContainersAmazon Web Services
 
왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법GeunCheolYeom
 
Project management by MS Project 2010
Project management by MS Project 2010Project management by MS Project 2010
Project management by MS Project 2010Wadhwani Foundation
 
오픈스택 멀티노드 설치 후기
오픈스택 멀티노드 설치 후기오픈스택 멀티노드 설치 후기
오픈스택 멀티노드 설치 후기영우 김
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackKernel TLV
 
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
Faster Container Image Distribution on a Variety of Tools with Lazy PullingFaster Container Image Distribution on a Variety of Tools with Lazy Pulling
Faster Container Image Distribution on a Variety of Tools with Lazy PullingKohei Tokunaga
 

Tendances (20)

Introduction to Go language
Introduction to Go languageIntroduction to Go language
Introduction to Go language
 
Pmp Final Review - M Maged
Pmp Final Review - M MagedPmp Final Review - M Maged
Pmp Final Review - M Maged
 
DOCUMENT CONTROLLER CV
DOCUMENT CONTROLLER  CVDOCUMENT CONTROLLER  CV
DOCUMENT CONTROLLER CV
 
Responsibility Accountability RACI Matrix
Responsibility Accountability RACI MatrixResponsibility Accountability RACI Matrix
Responsibility Accountability RACI Matrix
 
Docker presentation | Paris Docker Meetup
Docker presentation | Paris Docker MeetupDocker presentation | Paris Docker Meetup
Docker presentation | Paris Docker Meetup
 
Primavera - Tutorial
Primavera - TutorialPrimavera - Tutorial
Primavera - Tutorial
 
DCEU 18: Dockerfile Best Practices
DCEU 18: Dockerfile Best PracticesDCEU 18: Dockerfile Best Practices
DCEU 18: Dockerfile Best Practices
 
Advanced Work Packaging
Advanced Work Packaging Advanced Work Packaging
Advanced Work Packaging
 
Manage your bare-metal infrastructure with a CI/CD-driven approach
Manage your bare-metal infrastructure with a CI/CD-driven approachManage your bare-metal infrastructure with a CI/CD-driven approach
Manage your bare-metal infrastructure with a CI/CD-driven approach
 
Odoo Performance Limits
Odoo Performance LimitsOdoo Performance Limits
Odoo Performance Limits
 
02 terraform core concepts
02 terraform core concepts02 terraform core concepts
02 terraform core concepts
 
Primavera p6 advanced project planning
Primavera p6 advanced project planningPrimavera p6 advanced project planning
Primavera p6 advanced project planning
 
Building a CICD Pipeline for deploying on Containers
Building a CICD Pipeline for deploying on ContainersBuilding a CICD Pipeline for deploying on Containers
Building a CICD Pipeline for deploying on Containers
 
Container Networking Deep Dive
Container Networking Deep DiveContainer Networking Deep Dive
Container Networking Deep Dive
 
왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법
 
Prometheus Storage
Prometheus StoragePrometheus Storage
Prometheus Storage
 
Project management by MS Project 2010
Project management by MS Project 2010Project management by MS Project 2010
Project management by MS Project 2010
 
오픈스택 멀티노드 설치 후기
오픈스택 멀티노드 설치 후기오픈스택 멀티노드 설치 후기
오픈스택 멀티노드 설치 후기
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network Stack
 
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
Faster Container Image Distribution on a Variety of Tools with Lazy PullingFaster Container Image Distribution on a Variety of Tools with Lazy Pulling
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
 

Similaire à WinSock Asynchronous Input/Output

T2: What the Second Generation Holds
T2: What the Second Generation HoldsT2: What the Second Generation Holds
T2: What the Second Generation HoldsVlado Handziski
 
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 IPv6 Conference
 
#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...Paris Open Source Summit
 
Introducing MQTT
Introducing MQTTIntroducing MQTT
Introducing MQTTAndy Piper
 
Symbian OS - Communication And Messaging
Symbian OS - Communication And MessagingSymbian OS - Communication And Messaging
Symbian OS - Communication And MessagingAndreas Jakl
 
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 @ 2017Jian-Hong Pan
 
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...Felipe Prado
 
Windows Filtering Platform And Winsock Kernel
Windows Filtering Platform And Winsock KernelWindows Filtering Platform And Winsock Kernel
Windows Filtering Platform And Winsock KernelHardway Hou
 
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
 
Practical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacksPractical steps to mitigate DDoS attacks
Practical steps to mitigate DDoS attacksMartin Holovský
 
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 vulnerabilityHigh-Tech Bridge SA (HTBridge)
 
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 routersphosika sithisane
 

Similaire à 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
 

Dernier

Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03DallasHaselhorst
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607dollysharma2066
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menzaictsugar
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationAnamaria Contreras
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfRbc Rbcua
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Riya Pathan
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfJos Voskuil
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCRashishs7044
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Pereraictsugar
 
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxFinancial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxsaniyaimamuddin
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCRashishs7044
 
Cyber Security Training in Office Environment
Cyber Security Training in Office EnvironmentCyber Security Training in Office Environment
Cyber Security Training in Office Environmentelijahj01012
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFChandresh Chudasama
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCRashishs7044
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Seta Wicaksana
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607dollysharma2066
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCRashishs7044
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 

Dernier (20)

Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement Presentation
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdf
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdf
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Perera
 
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxFinancial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
 
Cyber Security Training in Office Environment
Cyber Security Training in Office EnvironmentCyber Security Training in Office Environment
Cyber Security Training in Office Environment
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDF
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 

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