I have a fairly simple client/server structure using System.Net.Sock ets, where the server only sends data to a collection of clients, and the clients only receive data, so, the data flow is unidirectional. My server sends the same data to each of the connected clients, at the same time through a for loop. The loop sends the data, or removes the client if it has disconnected. My problem is when the client disconnects, it can cause the server program to pause.
This is where the server distributes the data: -
for (int i = Clients.Count - 1; i >= 0; i--)
-
{
-
if (((Client)Clients[i]).isAlive() && serve)
-
{
-
((Client)Clients[i]).send(data.get_bytes());
-
}
-
else
-
{
-
((Client)Clients[i]).Stop(); // releases resources associated w/ client
-
Clients.RemoveAt(i);
-
}
-
Also in the server code, there is a client handling class, and this is the send function used above: -
-
try
-
{
-
ns.Write(sendB, 0, sendB.Length);
-
}
-
catch (***Exception) { } //if there's an error i will say that the client has disconnected, and isAlive = false;
-
I would like to keep it simple and have the client disconnect, whether or not the client disconnected on purpose, or the connection is cut. The actual client code is simple, and just sits in a loop waiting to call the NetworkStream read() call. I'm sure there's a better and more efficient way of handling the client disconnecting, b/c i don't want this to affect the remaining clients, if one disconnects for any reason. Any ideas? Thanks.
3 2178 Plater 7,872
Recognized Expert Expert
I had the same problem for a LOOOOOONG time, until I started using this sneaky little trick: -
private bool CheckIsConnected(Socket s)
-
{
-
bool part1 = s.Poll(1000, SelectMode.SelectRead);
-
bool part2 = (s.Available == 0);
-
if (part1 & part2)
-
{//connection is closed
-
return false;
-
}
-
return true;
-
}
-
If you look at what Socket.Poll() does you can see that there are situations in which you KNOW the socket is disconnected.
(I should really have a third bool in there to deal with if there is pending connection requests, but whatever)
I actually use that in my "waiting to read" loop and if it shows up as disconnected I break out of the loop and stuff
interesting...n ow are you using that on the client side? i could use that on both sides, i suppose, but i'm less concerned w/ the client and what happens to it when it's disconnected. thanks for the tip.
Plater 7,872
Recognized Expert Expert
Actually I use it on the server side (I wrote a webserver, so the client is a web browser)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Greg |
last post by:
I got the following problem:
A client connects to the MySQL server from a remote machine and
creates a lock using GET_LOCK() method. Then the client crashes or the
connection crashes (e.g.somebody pulls the cable) - whatever. The
point is that the client does not manage to send any disconnection
message to the server.
The process of the crashed user remains pretty long (I checked it by
calling SHOW PROCESSLIST) and holds the lock.
Is...
|
by: Thue Tuxen Sørensen |
last post by:
Hi everybody !
I´m maintaining a large intranet (approx 10000 concurrent users) running on
one IIS box and one DB box with sqlserver 2000.
Currently there is 2,5 GB Ram, 1 1400 mhz cpu and 2 scsi disks installed on
the db box.
Sqlserver is set to use max 1,4 GB RAM, and the sqlserver does not seem to
be using it all.
|
by: Michael Rybak |
last post by:
hi, everyone.
I'm writing a 2-players game that should support network mode. I'm now
testing it on 1 PC since I don't have 2. I directly use sockets, and
both client and server do computations, the only data transfered is
user mouse/kbd input.
It works synchronously, but somehow, when I play in client window,
both client and server have 17 fps, while when playing in server
window, server has 44 fps while client ...
|
by: Ken Allen |
last post by:
I am relatively new to .Net and C#, but I hav ebeen programing in other
languages and done some COM work for a number of years. I am attempting to
understand how to map an older program architecture into .Net -- not looking
to do it at this time, just to understand how I would achieve it.
In the old environment, we had two classes, a client and a server class,
that managed a data object. The server object knew how to interface with the...
|
by: Mainak Sarcar |
last post by:
Hi, I am writing an terminal app that would allow you to connect to POP3
server and receive mail data in raw format. Now the POP3 server expects a
graceful disconnection by sending a "quit\r\n" command to it. The server then
replies you with a "+OK ... Goodbye" message.
Now I have a code as follows for the disconnect button:---
<pre>
private void btnDisconnect_Click(object sender, System.EventArgs e)
{
if (serverSock != null)
| |
by: kwdavids |
last post by:
I'm getting occasional instances of the error:
SQL Server does not exist or
access denied
I get the error both in application code and from Enterprise Manager.
The database is LOCAL.
Microsoft SQL Server 2000 Developer Edition SP4
Windows XP Professional SP2
|
by: =?Utf-8?B?SGl3ag==?= |
last post by:
I recently upgraded my application from VB6, and was using the Winsock
control. In VB2005 I am using the TcpListener Class and have managed to get
it working, except for one thing:
Is there a way for the server to detect when a client disconnects (i.e.
closes the program abruptly)? In VB6 the Winsock control had the Close event,
which would fire when the client disconnected.
Some suggest that it can be done by sending a message to the...
|
by: timor.super |
last post by:
Hi group,
I've written a client/server application, using the dotnet sockets.
In my server, I have a thread waiting for messages with :
ret = currSocket.Receive(buffer, 1024, SocketFlags.None);
When the client exits, I close the socket with a specific message
(like "end") and the thread terminate in a proper manner, but If my
client crashes, the server is still waiting for receiving data, and
|
by: myprotein |
last post by:
if "all applications disconnection" is anathor tigger of page cleaner?
hi all
I discovered that when all application disconnect from the database, db2 will "truncate" current active log file and push forward a new log file either by reusing a exist log or by generating a new log file.
when anather application get connected to current database, execute "db2 get db cfg for xxxxx", u should see that the first active log is just the file...
|
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,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
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
| |