Solved

WinSocket related, packet in buffer waiting to be read

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
White board coding practice 3 98
Microsoft C++ code failing in executable that worked 9 137
Coding for the first time 9 99
visual C++ 1 39
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

737 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