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;
}
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;
}