我正在尝试将多个 IPEndPoints 绑定到列表中的多个套接字,问题是它们全部绑定,让我举个例子,我有一百个套接字,所以我生成了一百个 IPEndPoints,我试图遍历它们示例中绑定 IPEndPoints 的解释 Socket 1 Binds 192.168.1.67:1 Socket 2 Binds 192.168.1.67.2 以此类推
for (int i = 0; i < tcprange; i++)
{
int[] arraysockets;
//Regular TCP/IP
Socket TCPIPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
TCPIPSocketList.Add(TCPIPSocket);
foreach (Socket ClientSocket in TCPIPSocketList)
{
ClientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false); //This is a Property setting for the socket allows it to have multiple bindings
ClientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //This allows for the socket to have reuse the same local ip
// The socket will linger for 10 seconds after
ClientSocket.LingerState = new LingerOption(true, 10);
// Disable the Nagle Algorithm for this tcp socket.
ClientSocket.NoDelay = true;
//IPEndPoint[] ip = new IPEndPoint[tcprange];
arraysockets = new int[tcprange];
arraysockets[i] = i;
//Server Endpoint for Receiving incoming data from clients
// ip[i].Address = IPAddress.Any;
// ip[i].Port = arraysockets[i];
IPEndPoint ip = new IPEndPoint(IPAddress.Any, arraysockets[i]); <--// This works when I Use a single integer I need an array of socket numbers with the same ip that's why have the reuse settings on my socket
lstIncom.Items.Add("--Client Log--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
lstIncom.Items.Add("Socket# " + ip.ToString().Substring(7, 3));
ClientSocket.Bind(ip);
ClientSocket.Listen(10);
}