Valhalla Legends Forums Archive | C/C++ Programming | Question about winsock

AuthorMessageTime
warz
Is there a way to use a method like this:

[code]
#define YOUR_SOCKET_MSG  WM_USER + 100 // WM_USER is a base value for user-defined messages

...

WSAAsyncSelect(theSocket, hwnd, YOUR_SOCKET_MSG, FD_READ | FD_WRITE | FD_CONNECT | ...);

...


in window procedure:

switch (msg) {
  case YOUR_SOCKET_MSG:
      if (WSAGETSELECTERROR(lParam)) { // If an error occurred,
        closesocket(theSocket); // Close the socket
        WSACleanup(); // Shutdown Winsock
        return NETWORK_ERROR; // Return NETWORK_ERROR, WSAGetLastError(), etc.
      }
      switch (WSAGETSELECTEVENT(lParam)) { // Differentiate between the events
        case FD_READ:
    // Receive data
    break;
case FD_WRITE:
    // Write data
    break;
        case FD_CONNECT:
    // Just connected to server
    break;
        case ... // Same setup for other flags
    break;
      }
      break;

  ...
}
[/code]

in a console application?
March 28, 2006, 4:57 AM
TheMinistered
WSAEventSelect, AsyncSelect requires a window... I guess you could create a window in your console app... but that's just lame
March 28, 2006, 5:07 AM
warz
Both of those require a window?
March 28, 2006, 5:17 AM
LoRd
WSAEventSelect() doesn't, but WSAAsyncSelect() does.
March 28, 2006, 5:32 AM
warz
Well, I guess here's my main question I should seek an answer to first.

If, as of right now, my client will most likely only create one socket to bnet at a time. A simple blocking style socket would be efficient enough for this. What if, though, in the future I might need to create a bnftp connection, or a bnls connection or maybe even multiple clients online at once? Should I go ahead an use socket overlapped io instead of a blocking style socket? Even with one connection, would overlapped io still be the best option?

What do you guys think
March 28, 2006, 6:35 AM
LoRd
With blocking sockets, you can handle multiple connections using select() or sepetate threads.
March 28, 2006, 6:40 AM
warz
So, in short, what would you do? What's best? Overlapped, or blocking?
March 28, 2006, 6:56 AM
Myndfyr
I personally just like using multiple threads in few-connection scenarios.
March 28, 2006, 8:29 AM
UserLoser
[quote author=warz link=topic=14607.msg149260#msg149260 date=1143528983]
So, in short, what would you do? What's best? Overlapped, or blocking?
[/quote]

On Windows, probably overlapped i/o or i/o completion ports
March 28, 2006, 3:58 PM
Kp
[quote author=TheMinistered link=topic=14607.msg149250#msg149250 date=1143522467]
WSAEventSelect, AsyncSelect requires a window... I guess you could create a window in your console app... but that's just lame
[/quote]

However, it's probably OK to use a message-only window for this.  Message-only windows are created by passing HWND_MESSAGE as the parent when calling CreateWindowExW.
March 29, 2006, 12:03 AM
SecretShop
Id use overlapped IO and event handles, use a main loop to wait for the events with WASWaitForMultipleEvents, or considering how WSAEvents are also regular event handles, WaitForMultipleObjects would also work too if you are waiting on things other than sockets aswell.  This way you can have an idle function called and be able to run timed events from the main loop.  You can also not have to worry about race conditions which threads introduce.  The reason I wouldn't use IO completion routines is that you lose the main loop concept which makes designing applications like this, atleast IMO, alot easier.
March 29, 2006, 7:42 PM

Search