Solved

WinSocket related, packet in buffer waiting to be read

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now