473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sockets - what's the best way to do this?

I have an application that listens on a socket using TcpListener. Afiter I
create the object I use the Start method and then I wait for an incoming
request. No problem with this. When waiting for a socket connection the
application is in blocking mode. I can use the Pending method to see if there
are incoming requests before calling AcceptSocket. But that's not what I want
to do as it means my app can spin around in a frantic loop. I could use a
timer but I don't want to if I can avoid it. Ideally I'd like to wait on the
AcceptSocket method like I've used, but just before the connection request
comes in, is there a sort of pre-connection event that gets fired that I can
use? This way, I can wait in blocking mode which is fine but if there is an
event that occurs just before I accept the socket I could use that
information to decide if I really want to accept it or not. An example might
be to allow only 20 connections at any one time for example, or deny
connection requests if a database is out of service, that sort of thing. If
there's no direct event I can trap, then how could I code for this situation
before accepting the socket. By the time I accept the socket, the client
knows it's available but a moment later I may want to prevent the connection
and that means having to Close it, sort of pulling the rug from under the
client applications feet. If I can do this before the client application gets
notifed that the connection was accepted that would be ideal. I don't want to
thrash the system spinning on the Pending method as I've mentioned and it's
not ideal that I use a timer either. In short, how could I handle a
pre-connection notification and if it does not satisfy my criteria reject the
incoming request. Thanks
Jul 21 '05 #1
1 1296
You want to deny the request based on some business rules, but wouldn't it
make sense to tell the client why you are denying the request? That way,
they can respond accordingly. For example, if you are too busy, and you
accept the request, respond with a quick "I'm busy" message and close, then
the client can incorporate logic to try again later. On the other hand, if
the client connects and provides an invalid header or improper credentials,
then you don't want them to keep trying, because the condition is unlikely
to clear up without intervention.

So, I would suggest, first of all, looking at your protocol to see if you
can provide the information to the client about why you want to drop the
connection.

Secondly, use the thread pool to wait for incoming requests. Go ahead and
block on the incoming request and accept it in your threads. No need to
spin on status or use a timer, which you rightfully point out will simply
consume CPU cycles needlessly.

I hope these suggestions are helpful,

--- Nick

"Mr Grunge of Gunk Hall." <Mr Grunge of Gunk
Hall.@discussions.microsoft.com> wrote in message
news:B9**********************************@microsof t.com...
I have an application that listens on a socket using TcpListener. Afiter I
create the object I use the Start method and then I wait for an incoming
request. No problem with this. When waiting for a socket connection the
application is in blocking mode. I can use the Pending method to see if there are incoming requests before calling AcceptSocket. But that's not what I want to do as it means my app can spin around in a frantic loop. I could use a
timer but I don't want to if I can avoid it. Ideally I'd like to wait on the AcceptSocket method like I've used, but just before the connection request
comes in, is there a sort of pre-connection event that gets fired that I can use? This way, I can wait in blocking mode which is fine but if there is an event that occurs just before I accept the socket I could use that
information to decide if I really want to accept it or not. An example might be to allow only 20 connections at any one time for example, or deny
connection requests if a database is out of service, that sort of thing. If there's no direct event I can trap, then how could I code for this situation before accepting the socket. By the time I accept the socket, the client
knows it's available but a moment later I may want to prevent the connection and that means having to Close it, sort of pulling the rug from under the
client applications feet. If I can do this before the client application gets notifed that the connection was accepted that would be ideal. I don't want to thrash the system spinning on the Pending method as I've mentioned and it's not ideal that I use a timer either. In short, how could I handle a
pre-connection notification and if it does not satisfy my criteria reject the incoming request. Thanks

Jul 21 '05 #2

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

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
4
by: WAkthar | last post by:
I am in the process of converting an MFC client and server application to C#. The communication between the client and server was done using simple WM_COPYDATA. I want to use sockets for the C#...
2
by: Nadav | last post by:
Hi, I am about to write a performance critical application and I wonder what is the best way to implement it... Should I use the standart Win32 winsock DLL or should I rather use the new managed...
1
by: taras | last post by:
IE 6.0 sockets number (TCP/IP channels number) for the same Site ??? I've discovered the restriction: IE 6.0 has 2 sockets only to the same Server (ASP.NET Site). Where can I find any kind of...
2
by: Jm | last post by:
Hi All Im trying to get a bit of background info on .net.sockets for an app that im upgrading to be .net. Under vb6 i used the winsock control and would start it listening and it would call an...
1
by: Macca | last post by:
Hi, I am writing a C# server app that listens for clients over a TCP/IP link. The clients are embedded devices with their software written in C. The clients establish connections and then...
4
by: nyhetsgrupper | last post by:
I'm writing a server application connection to multiple clients using sockets. I want one socket for each client, and the comunication needs to be async. I could use the async sockets methods...
2
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication)...
8
by: JDavis | last post by:
I am using System.Net.Sockets to connect a client socket to a server that requires three inputs when I connect: host, port and an identification number that identifies the person connecting. ...
2
yashg
by: yashg | last post by:
I am building a data backup application in C# using Sockets. It has a server component and a client component. The client is going to upload files to the server through TCP sockets. I've got all...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
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.