473,382 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1293
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.