By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

C#.NET - Detecting socket is closed

P: 2
I'm writing a program that in its most simplest form acts as a proxy between a local client and a remote server. But I cannot figure out how to detect when either the connection to the client is close or the connection to the server is closed. I've tried looking at socket.connected - it always returns true, even when the client program has disconnected. I've tried getting an exception from the networkstream.DataAvailable, it always just reads false, and no exceptions. Anyone know how I can detect when these sockets are remotely closed??

Expand|Select|Wrap|Line Numbers
  1. TcpListener listener;
  2.             TcpClient client;
  3.             TcpClient server;
  4.             while (true)
  5.             {
  6.                 Thread.Sleep(10);
  7.                 IPAddress addy = IPAddress.Parse("127.0.0.1");
  8.                 listener = new TcpListener(addy, 4001);
  9.                 listener.Start();
  10.                 client = listener.AcceptTcpClient();
  11.                 Console.WriteLine("[[Client connected.]]");
  12.                 server = new TcpClient();
  13.                 server.Connect("imperian.com", 23);
  14.                 Console.WriteLine("[[Server connected.]]");
  15.                 NetworkStream cstm = client.GetStream();
  16.                 NetworkStream sstm = server.GetStream();
  17.                 bool quit = false;
  18.                 while (!quit)
  19.                 {
  20.                     Thread.Sleep(10);
  21.  
  22.                     if (!client.Client.Connected)
  23.                     {
  24.                         Console.WriteLine("[[Client disconnected]]");
  25.                     }
  26.  
  27.                     try
  28.                     {
  29.                         if (cstm.DataAvailable)
  30.                         {
  31.                             byte[] readBuffer = new byte[1024];
  32.                             StringBuilder builder = new StringBuilder();
  33.                             int numBytes = 0;
  34.                             do
  35.                             {
  36.                                 numBytes = cstm.Read(readBuffer, 0, readBuffer.Length);
  37.                                 builder.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer));
  38.                             } while (cstm.DataAvailable);
  39.  
  40.                             byte[] writeBuffer = Encoding.ASCII.GetBytes(builder.ToString());
  41.                             sstm.Write(writeBuffer, 0, writeBuffer.Length);
  42.                         }
  43.  
  44.                         if (sstm.DataAvailable)
  45.                         {
  46.                             byte[] readBuffer = new byte[1024];
  47.                             StringBuilder builder = new StringBuilder();
  48.                             int numBytes = 0;
  49.                             do
  50.                             {
  51.                                 numBytes = sstm.Read(readBuffer, 0, readBuffer.Length);
  52.                                 builder.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer));
  53.                             } while (sstm.DataAvailable);
  54.  
  55.                             byte[] writeBuffer = Encoding.ASCII.GetBytes(builder.ToString());
  56.                             cstm.Write(writeBuffer, 0, writeBuffer.Length);
  57.                         }
  58.                     }
  59.                     catch (Exception)
  60.                     {
  61.                         cstm.Close();
  62.                         Console.WriteLine("[[Client disconnected.]]");
  63.                         sstm.Close();
  64.                         Console.WriteLine("[[Server disconnected.]]");
  65.                         break;
  66.                         quit = true;
  67.                     }
  68.                 }
  69.             }
Aug 8 '08 #1

✓ answered by Plater

I wrote this code and use it frequently:
Expand|Select|Wrap|Line Numbers
  1. bool SocketConnected(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.  

Share this Question
Share on Google+
5 Replies


joedeene
100+
P: 583
here is a link to a great response similar to your problem.

http://bytes.com/forum/thread234523.html

at the bottom of the linked page, it says something about the .poll() function of the socket class, i think that will help
Aug 8 '08 #2

Plater
Expert 5K+
P: 7,872
I wrote this code and use it frequently:
Expand|Select|Wrap|Line Numbers
  1. bool SocketConnected(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.  
Aug 8 '08 #3

P: 2
Thank you so much, that worked!
Aug 8 '08 #4

P: 1
I used that code and it works wonderfully...thanks a bunch!!!
Nov 9 '09 #5

P: 1
to: Plater Plater
You are very good. After searching on google in many hours, but I can not find any working solutions. Now, I used that code and it works wonderfully...thanks a bunch!!!
good job!
Jul 8 '10 #6

Post your reply

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