WSAEventSelect Help

B

BBryant

Here is the scenario;

Two sockets are bound on two seperate UDP ports, 46999 and 47000. Both
sockets initilize, and are bound to the ports as expected, however only
port 46999 receives data.

Any help is greatly appreciated, here is the code;



void bListen() {
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
cout <<"Error at WSAStartup()\n";

// Create a socket.
SOCKET m_socket[2];
m_socket[0] = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
m_socket[1] = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

if ( m_socket[0] == INVALID_SOCKET ) {
cout << "Error at socket(0): " << WSAGetLastError() << "\n";
WSACleanup();
return;
}
if ( m_socket[1] == INVALID_SOCKET ) {
cout << "Error at socket(1): " << WSAGetLastError() << "\n";
WSACleanup();
return;
}

// Bind the socket.
sockaddr_in service[2];

service[0].sin_family = AF_INET;
service[0].sin_addr.s_addr = inet_addr( "192.168.1.100" );
service[0].sin_port = htons( 46999 );
service[1].sin_family = AF_INET;
service[1].sin_addr.s_addr = inet_addr( "192.168.1.100" );
service[1].sin_port = htons( 47000 );

if ( bind( m_socket[0], (SOCKADDR*) &service[0], sizeof(service[0])
) == SOCKET_ERROR ) {
cout << "bind(0) failed.\n";
closesocket(m_socket[0]);
return;
}
if ( bind( m_socket[1], (SOCKADDR*) &service[1], sizeof(service[1])
) == SOCKET_ERROR ) {
cout << "bind(1) failed.\n";
closesocket(m_socket[1]);
return;
}

char wbData[256];
int iBytes, nRet;

WSAEVENT hEvent = WSACreateEvent();
WSANETWORKEVENTS events;
WSAEventSelect(*m_socket, hEvent, FD_READ);
while(1)
{
nRet = WSAWaitForMultipleEvents(2, &hEvent, FALSE,
WSA_INFINITE, FALSE);
if(WSAEnumNetworkEvents(*m_socket,hEvent,&events) ==
SOCKET_ERROR)
{
cout << "Failure at WSAEnumNetworkEvents: " <<
WSAGetLastError() << endl;
}
else
{
if(events.lNetworkEvents & FD_READ)
{
iBytes = recv(*m_socket, wbData, 256, 0);
ParseData(static_cast<string> (wbData));
}
}
}

WSACloseEvent(hEvent);
return;
}
 
R

red floyd

BBryant said:
Here is the scenario;

[redacted]

You have posted to the wrong group.

Try one of the following groups:
comp.os.ms-windows.programmer
comp.os.ms-windows.programmer.win32
comp.os.ms-windows.programmer.tools.winsock

This newsgroup is for discussing the C++ language. Socket programming,
and specifically winsock programming is off topic.

Hope those other groups help.
 
M

modemer

At first, looks like this question shouldn't be posted in this group because
that it's not a C++ related. Anyway, as my usual, try to give an answer for
saving submitter's time. ( I am so eager to answer questions, right ? ;-)

iBytes = recv(*m_socket, wbData, 256, 0);

obviously you are only receiving data from m_socket[0] socket, right? ie
46999 port
 
B

BBryant

My apologies, did not realize this sort of question should not be
posted here.

To answer your question modemer, we want to receive from both
m_socket[0] and m_socket[1], but we are only receiving from m_socket[0]
(port 46999).
 
M

modemer

I am not asking your question :) sorry about that if you feel I am asking a
question.

I mean you made a mistake on this statement:
iBytes = recv(*m_socket, wbData, 256, 0);

this statement is equal to the following:
iBytes = recv(m_socket[0], wbData, 256, 0);

these statements means you are only receiving data from m_socket[0] which is
port 46999, you should fix this to be able to receive data from m_socket[1]
as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top