News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Usage of WSAWaitForMultipleEvents API call

Started by Uzarius Futurus, October 05, 2008, 07:16:30 PM

Previous topic - Next topic

Uzarius Futurus

Hello comrades,
I decided for a while ago to learn Winsock 2 programming in Windows NT systems.
Said is done and I started of with basic send- and recv-functions with windows.
Now I've encountered some problems when implementing a socket while using the WSAWaitForMultipleEvents function. I would like to use Events rather than the original Windows Message method since I have no window.
So basically what I have is a small codesnippet that creates a connection to a server, and then waits for command matching the one specified earlier in the code.

The API-functions I use in this order:

  • WSAStartup - To initialize the winsock reference
  • socket - To create the socket
  • WSACreateEvent - To create the event object
  • WSAEventSelect - To specify on which command my event should signal
  • hton - To convert the port
  • sin_addr - To convert the IP-Address
  • connect - To establish a connection
  • WSAWaitForMultipleEvents - To wait for commands

Anyhow, I really can't get it to work and the MSDN is not quite a good resource for beginners in MASM.
Here is a stripped version of my code:


invoke WSAStartup, 0202h, addr wsaData
invoke socket, AF_INET, SOCK_STREAM, IPPROTO_TCP
mov hSocket, eax
invoke WSACreateEvent
mov wsaEvent, eax
invoke WSAEventSelect, hSocket, wsaEvent, FD_READ

[...]

_connect:
invoke Sleep, 1000
invoke connect, hSocket, addr sockAddr, sizeof sockAddr
cmp eax, 0
jne _connect

[...]

_check:
invoke WSAWaitForMultipleEvents, 1, wsaEvent, FALSE, INFINITE, FALSE
cmp wsaNetEvents.lNetworkEvents, FD_READ
jne _check

[...]


Any help is strongly appreciated!

Regards,
Uzarius

akane

Uzarius, see the description for second parameter:
QuoteDWORD WSAWaitForMultipleEvents(
  DWORD cEvents,
  const WSAEVENT* lphEvents,
[...]
lphEvents
[in] Pointer to an array of event object handles.
You need to pass an address of array, not the first value. With standard assemblers like nasm it would work, but masm is designed for C programmers, where label equals to memory value pointed to by this label. You need to add offset macro:
invoke WSAWaitForMultipleEvents, 1, offset wsaEvent, FALSE, INFINITE, FALSE

Uzarius Futurus

akane,

Thanks for your reply.
Could you give a example code of how to pass a pointer to an array. How do you create that array?

Regards.

akane

Sure, but notice that I do not code in Masm, I prefer Intel standards.
SomeFunction proc
LOCAL events[6]:WSAEVENT
LOCAL netev:WSANETWORKEVENTS

invoke WSACreateEvent
mov    events[0],eax
...
invoke WSACreateEvent
mov    events[5],eax

invoke WSAEventSelect, hSocket[0], events[0], FD_**
...
invoke WSAEventSelect, hSocket[5], events[5], FD_**

; pass the array to WSAWaitForMultipleEvents
invoke WSAWaitForMultipleEvents, 6, offset events, FALSE, INFINITE, FALSE
; eax = event index, or >5 if failed/timeout
invoke WSAEnumNetworkEvents, hSocket[eax], events[eax], offset netev ; indexing may be wrong

Uzarius Futurus

#4
akane,

Thank you alot! Solved the problem with your help.

Regards,

Lars

Wow, I have such a similar problem.

I see your sollution but I've seen several source codes online that does not use different arrays, but have all the FD_XX in the same array. (FD_ACCEPT+FD_READ+FD_WRITE+_FD_CLOSE)

I just can't seem to make that work for me.

And akane,

what is the main difference between Masm and Intel Standards?

akane

Mainly differs by memory access model - mov reg, label - by default should load label pointer. But in Masm it reads the value from memory.

I'm not sure what you mean with "FD_XX in the same array" ? Its a bitmask. And hey, I see you are using PLUS instead OR operator for flags combining - it may be unsafe if at least one mask includes another mask.