473,706 Members | 2,755 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 8706
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
2456
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
2475
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
6581
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
1828
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
8772
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
8692
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9276
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
9038
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
8982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6607
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
5934
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
4441
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...
2
2493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.