?
Solved

WinSocket related, packet in buffer waiting to be read

Posted on 2004-09-12
5
Medium Priority
?
349 Views
Last Modified: 2008-01-09
Hi,

   I would like to know how my program could be alert by windows subsystem if there is a packet waiting to
be read for winsock 1.1 and so on, I have difficulty to deal with that. If I have like 50 clients connecting with my server
in a tight loop that will take all the processor power which I want to avoid as much as possible.

Thank you, Stephan
0
Comment
Question by:Dogofwars
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 12041343
Are you really using Winsock 1.1?  If so, your best bet is probably WSAAsyncSelect.  You use WSAAsyncSelect to tell the system what types of events you are interested in.  For example, to be notified on incoming connections, you would do:

    #define WM_NETEVENT (WM_USER+1)
    ...
    rc = WSAAsyncSelect(s, hWnd, (WM_NETEVENT, FD_ACCEPT);

after putting socket s into a listening state.

You need a window to receive the messages.  You add a handler for WM_NETEVENT and lParam holds the message value (FD_ACCEPT in this case since we're only registered for that event).  Once you call 'accept' (presumably in the message handler), the event will be reenabled.
0
 

Author Comment

by:Dogofwars
ID: 12041369
No I'm no really using winsock 1.1 but I am building a little class in C++ to use winsock in a easier manner and that will include fonction for winsock 1.1 only to the most promising IO complete ;) I see how winsock is build and I don't like
to deal with that all the time especially that when you want to track bug with that it's just a pain in the butox ;)



0
 
LVL 19

Expert Comment

by:drichards
ID: 12041390
The specific mechanics of adding a message handler will depend on the type of app (MFC, raw Win32, etc.).  Also, the message ID if of course only an example (WM_USER+1) and you should use something appropriate for your app.

Read and wwrite can be handled similarly on the accepted socket if you choose.  See the docs for WSAAsyncSelect at:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsaasyncselect_2.asp

If you use Winsock 2 there are other alternatives that do not require a message pump.
0
 

Author Comment

by:Dogofwars
ID: 12041405
thank you ;)
0
 
LVL 19

Expert Comment

by:drichards
ID: 12041418
And if you want to go with Winsock 2, you can do WSAEventSelect which registers an event object rather than a Window handle with the network events.  Then you use WSAEnumNetworkEvents to determine which event happened (unless you only register for one type of event) and WSAWaitForMultipleObjects to wait for teh event to be set.  Typically you use this to wait for incoming connections and then for each connection post asynchronous read operations with IO competion routines.  The completion routines are executed by the OS.  This is very efficient.  You need to create sockets using WSASocket with the overlapped flag set to use IO completion routines.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question