473,396 Members | 1,775 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,396 software developers and data experts.

C# Client/Server Disconnection

I have a fairly simple client/server structure using System.Net.Sockets, 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:
Expand|Select|Wrap|Line Numbers
  1.             for (int i = Clients.Count - 1; i >= 0; i--)
  2.             {
  3.                 if (((Client)Clients[i]).isAlive() && serve)
  4.                 {
  5.                     ((Client)Clients[i]).send(data.get_bytes());
  6.                 }
  7.                 else
  8.                 {
  9.                     ((Client)Clients[i]).Stop();  // releases resources associated w/ client
  10.                     Clients.RemoveAt(i);
  11.                 }
  12.  
Also in the server code, there is a client handling class, and this is the send function used above:
Expand|Select|Wrap|Line Numbers
  1.  
  2.                 try
  3.                 {
  4.                     ns.Write(sendB, 0, sendB.Length);
  5.                 }
  6.                 catch (***Exception) { } //if there's an error i will say that the client has disconnected, and isAlive = false;
  7.  
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.
Nov 6 '07 #1
3 2035
Plater
7,872 Expert 4TB
I had the same problem for a LOOOOOONG time, until I started using this sneaky little trick:
Expand|Select|Wrap|Line Numbers
  1. private bool CheckIsConnected(Socket s)
  2. {
  3.    bool part1 = s.Poll(1000, SelectMode.SelectRead);
  4.    bool part2 = (s.Available == 0);
  5.    if (part1 & part2)
  6.    {//connection is closed
  7.       return false;
  8.    }
  9.    return true;
  10. }
  11.  
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
Nov 6 '07 #2
interesting...now 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.
Nov 6 '07 #3
Plater
7,872 Expert 4TB
Actually I use it on the server side (I wrote a webserver, so the client is a web browser)
Nov 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
19
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...
15
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...
6
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...
1
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"...
6
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. ...
4
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...
9
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,...
0
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
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
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...

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.