Solved

WinSocket related, packet in buffer waiting to be read

Posted on 2004-09-12
5
341 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

728 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