473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket.Select() > 64 connections

I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time. I
know that I have to manipulate the FD_SetSize to increase the limit. How
do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so running
each socket in it's own thread doesn't seem very practical.

Stuart.
Dec 7 '05 #1
9 8713
I did this in C++ some time ago using the concepts of "Overlapped IO" and "IO
Completion Ports". This is very scalable (my service talked to hundreds of
clients simultaneously and never lost a connection or data) and only uses few
threads based on the number of processors in your server. There were examples
on MSDN. I'm sure there's one for the .NET environment. Search on these key
words and perhaps you can find something. Hope this helps.
--
Thom
"Stuart" wrote:
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time. I
know that I have to manipulate the FD_SetSize to increase the limit. How
do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so running
each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #2
Hey,

Have you tried out that limit in reality?

I fired up Reflector and took a peek at how Socket.Select() works in the
2.0 Framework, and it checks that not all sets are empty, and that no
one of them contains more than 0x10000 sockets (which would be a lot).

I guess this would mean that it should work for sets that are between 1
and 0x10000 in size. (anyone can confirm?)

-Lenard
Stuart wrote:
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time. I
know that I have to manipulate the FD_SetSize to increase the limit. How
do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so running
each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #3
Hi,

I checked further. The managed Select() converts the sets to IntPtr[]
arrays. The first element contains the number of items (socket handles)
in the array, while subsequent elements are the elements from the set.

I checked winsock2.h from the platform SDK, and an fd_set is just
implemented like that. A single unsigned number containing the number of
sockets in the set, and then an array of SOCKET handles. When you set
FD_SETSIZE in C++, you have to set it before you include windows.h (or
winsock2.h). This way, the fd_set type is declared with a constant size
of FD_SETSIZE. Although this is a C# newsgroup, I hope some C++ is not a
problem. So here is the part from winsock2.h:

#ifndef FD_SETSIZE
#define FD_SETSIZE 64
#endif /* FD_SETSIZE */

typedef struct fd_set {
u_int fd_count; /* how many are SET? */
SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */
} fd_set;

This means that you should not worry about this in .NET, because it is
implemented for you transparently. Go ahead and use Socket.Select() for
socket arrays that contain less than 0x10000 elements. (thats 65536 in
decimal). It will work.

-Lenard

Lenard Gunda wrote:
Hey,

Have you tried out that limit in reality?

I fired up Reflector and took a peek at how Socket.Select() works in the
2.0 Framework, and it checks that not all sets are empty, and that no
one of them contains more than 0x10000 sockets (which would be a lot).

I guess this would mean that it should work for sets that are between 1
and 0x10000 in size. (anyone can confirm?)

-Lenard
Stuart wrote:
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time.
I know that I have to manipulate the FD_SetSize to increase the limit.
How do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so
running each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #4
There is a limit in 1.1 for sure. I haven't tried it on 2.0 yet. I
tried running my .exe on Mono, and it bombed out at over 1000
connections, which is a lot better than 64!

Stuart.
Lenard Gunda wrote:
Hey,

Have you tried out that limit in reality?

I fired up Reflector and took a peek at how Socket.Select() works in the
2.0 Framework, and it checks that not all sets are empty, and that no
one of them contains more than 0x10000 sockets (which would be a lot).

I guess this would mean that it should work for sets that are between 1
and 0x10000 in size. (anyone can confirm?)

-Lenard
Stuart wrote:
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time.
I know that I have to manipulate the FD_SetSize to increase the limit.
How do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so
running each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #5
Hi,

I checked the .NET 1.1 version of Select with Reflector, and I think it
should work there too with many sockets.

What kind of IList do you use for your sets? Is it an array or ArrayList
or something of your own?

-Lenard
Stuart wrote:
There is a limit in 1.1 for sure. I haven't tried it on 2.0 yet. I
tried running my .exe on Mono, and it bombed out at over 1000
connections, which is a lot better than 64!

Stuart.
Lenard Gunda wrote:
Hey,

Have you tried out that limit in reality?

I fired up Reflector and took a peek at how Socket.Select() works in
the 2.0 Framework, and it checks that not all sets are empty, and that
no one of them contains more than 0x10000 sockets (which would be a lot).

I guess this would mean that it should work for sets that are between
1 and 0x10000 in size. (anyone can confirm?)

-Lenard
Stuart wrote:
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a
time. I know that I have to manipulate the FD_SetSize to increase the
limit. How do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so
running each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #6
Do these connections have to be "up" all the time, or is there just some data
exchanged and then you could recycle the socket for another connection?
In that case you could use a thread pool with asynchronous callbacks.

Ami Bar has an excellent piece of work he calls "Smart Threadpool" that you
can find on codeproject.com

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Stuart" wrote:
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time. I
know that I have to manipulate the FD_SetSize to increase the limit. How
do you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so running
each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #7
Lenard Gunda wrote:
Hi,

I checked the .NET 1.1 version of Select with Reflector, and I think it
should work there too with many sockets.

What kind of IList do you use for your sets? Is it an array or ArrayList
or something of your own?

-Lenard


Here is a simplified version of the code:

ArrayList sockList = new ArrayList();
ArrayList copyList = new ArrayList();

Socket SocketRX = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);

IPEndPoint iep = new IPEndPoint(IPAd dress.Any, clsMain.PortTCP );
byte[] data;
int recv;

SocketRX.SetSoc ketOption(Socke tOptionLevel.So cket,SocketOpti onName.ReuseAdd ress,
1);
SocketRX.Bind(i ep);
SocketRX.Listen (1000);

while (!quit)
{
if (SocketRX.Poll( 0,SelectMode.Se lectRead)) //Test for new connections
{ //New Connection
Socket NewSocket = SocketRX.Accept (); //Accept the connection
SockInfo.IPEndP =(IPEndPoint)Ne wSocket.RemoteE ndPoint;
sockList.Add(Ne wSocket); //Add Socket to the SocketList

Console.WriteLi ne("Soc: Connected to {0}", SockInfo.IPEndP .ToString());
}
else
{ //Check for any pending data
copyList = new ArrayList(sockL ist);
Socket.Select(c opyList, null, null, 5000000); //block for .5 second
foreach(Socket client in copyList) //Read data from sockets that have
data
{
data = new byte[RXBufferSize];
try
{
recv = client.Receive( data); //Read the incomming data
}
catch (Exception ex)
{
recv=0;
}
if (recv == 0)
{
CloseSocket(cli ent);
}
else
{
//Proccess my data here....
}
}
}
}
Dec 7 '05 #8
Peter Bromberg [C# MVP] wrote:
Do these connections have to be "up" all the time, or is there just some data
exchanged and then you could recycle the socket for another connection?
In that case you could use a thread pool with asynchronous callbacks.

Ami Bar has an excellent piece of work he calls "Smart Threadpool" that you
can find on codeproject.com

The sockets have to be up all the time. That data thoughput is small,
and random.

Stuart.
Dec 7 '05 #9
You can utilize sockets in async mode.
I mean Socket.BeginRec eive/EndReceive and
Socket.BeginSen d/EndSend

This approach is more efficient because it uses system thread pool with
overlapped I/O ( as in good old win32 times was made with the help of
CompletionPort)

Have a look at for more details

http://msdn.microsoft.com/library/de...ogthrepool.asp

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Stuart" <st****@stu.org .uk> wrote in message
news:dn******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...
I am trying to execute a Socket.Select() statement on an arraylist of
sockets. The problem is that I can only go up to 64 sockets at a time. I
know that I have to manipulate the FD_SetSize to increase the limit. How do
you do that in C# in the .NET Visual Studio environment?

I could potentially have 1000's of simultaneous connections, so running
each socket in it's own thread doesn't seem very practical.

Stuart.

Dec 7 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2463
by: Dario | last post by:
I have an unmanaged library that handle many TCP/IP connections. In my .NET application i want to test if there is input available on these connections. Using an existing function of the unmanaged library I'm able to retrieve the list of Int32 socket-descriptors handled by the unmanaged library. How can I use this list of Int32 socket-descriptors inside my C# program to test if there is input available on these socket-descriptors? I...
9
2482
by: Phil Jenson | last post by:
I am try to evaluate the most efficient method of handling thousands of simultaneous TCP connects each of which remain connected to the server for hours and pass a small amount of data usually once a minute. The data received is logged in a SQL Server database. The only method I have found of reading each socket requires a thread for each connection which blocks waiting for data. This appears to be very inefficient as it will result in...
4
6586
by: billiejoex | last post by:
Hi, I'm writing a small asyncore-based server application serving a lot of clients. When I have to handle more than 1021 client simoultaneously the 'binded' socket object raises an error: connections: 1018 connections: 1019 connections: 1020 connections: 1021
0
1832
by: mhetfield | last post by:
Hi, I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client. - the client should send this message (after the connection established): my password "anypassword" and the server will check, if it's not the same password that's hardcoded in the server program, the server close connection. so the client will...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.