Solved

WinSocket related, packet in buffer waiting to be read

Posted on 2004-09-12
5
335 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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