Microsoft selects some sockets
-
From MSDN:
The following code example uses Select to determine which listening sockets have a connection request.
IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostEntry.AddressList[0]; Socket socket0 = null; Socket socket1 = null; Socket socket2 = null; Socket socket3 = null; Socket socket4 = null; Socket socket5 = null; ArrayList listenList = new ArrayList(); listenList.Add(socket0); listenList.Add(socket1); listenList.Add(socket2); ArrayList acceptList = new ArrayList(); acceptList.Add(socket3); acceptList.Add(socket4); acceptList.Add(socket5); for( int i = 0; i < 3; i++ ) { listenList[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ((Socket)listenList[i]).Bind(new IPEndPoint(ipAddress, 11000 + i)); ((Socket)listenList[i]).Listen(10); } // Only the sockets that contain a connection request // will remain in listenList after Select returns. Socket.Select(listenList, null, null, 1000); for( int i = 0; i < listenList.Count; i++ ) { acceptList[i] = ((Socket)listenList[i]).Accept(); }
live MSDN link
archive just in case Microsoft MSDNs MSDN again
oh look they did MSDN MSDN
here's another archive link (scroll to the right)This example code does the following:
- Find the current machine's IP address via DNS.
- Set 6 variables to null, add 3 of them to each of 2 lists, and then never touch the variables again.
- Listen on ports 11000, 11001, and 11002, allowing up to 10 connections on each port to be queued.
- Wait UP TO 1 MILLISECOND for any of the listeners to receive a connection.
- For each listener that either had a connection waiting or received a connection during that millisecond, accept one connection and replace one of the three nulls in the second list.
- As the program has ended, the operating system reclaims the sockets.
-
The C++ snippet seems to have had the beginning cut off at some point, and the main function just exits immediately so who knows what it was ever supposed to do:
IPHostEntry^ lipa = Dns::Resolve( Dns::GetHostName() ); //Gets three separate local endpoints. IPEndPoint^ lep1 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 ); IPEndPoint^ lep2 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11001 ); IPEndPoint^ lep3 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11002 ); //creates an array of endpoints. array<IPEndPoint^>^ipendpoints = gcnew array<IPEndPoint^>(3); ipendpoints[ 0 ] = lep1; ipendpoints[ 1 ] = lep2; ipendpoints[ 2 ] = lep3; //Creates three separate sockets. Socket^ s1 = gcnew Socket( lep1->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp ); Socket^ s2 = gcnew Socket( lep2->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp ); Socket^ s3 = gcnew Socket( lep3->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp ); array<Socket^>^socketList = gcnew array<Socket^>(3); socketList[ 0 ] = s1; socketList[ 1 ] = s2; socketList[ 2 ] = s3; //Binds and Listens on all sockets in the array of sockets. for ( int i = 0; i < 3; i++ ) { socketList[ i ]->Bind( ipendpoints[ i ] ); socketList[ i ]->Listen( 1000 ); } //Calls Select to determine which sockets are ready for reading. Socket::Select( safe_cast<IList^>(socketList), nullptr, nullptr, 1000 ); //Reads on the sockets returned by Select. array<Byte>^buffer = gcnew array<Byte>(1024); String^ outString; for ( Int32 j = 0; j < (socketList->Length - 1); j++ ) { socketList[ j ]->Receive( buffer ); outString = "Socket "; outString->Concat( j.ToString(), " has the message", Encoding::ASCII->GetString( buffer ) ); Console::WriteLine( outString ); } } }; int main() { return 0; }
Not to mention it is actually C++/CLI instead of C++.
-
-
@lb_ said in Microsoft selects some sockets:
Not to mention it is actually C++/CLI instead of C++.
Well it is the .NET documentation, so. That seems normal and expected.
-
@blakeyrat Normally the language tabs differentiate, though. I've seen pages with both a C++/CLI tab and a C++ tab.
-
MSDN said:
AddressFamily.InterNetwork
InterNetwork
:butwhy.pgm:
-
@ben_lubar said in Microsoft selects some sockets:
Microsoft MSDNs MSDN again
Upvoted just for that.