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

C# - APP - How To Make Server Detect That Client is Disconnected

P: 1
hello,

I have Problem with Client Server Application

when I unplug the client network cable, the server still read the network stream
what function should I use to make server detect that the client is disconnect

here the code...

serverSide

Expand|Select|Wrap|Line Numbers
  1. public static void Main()
  2.     {
  3.         int recv;
  4.         byte[] data = new byte[1024];
  5.         TcpListener newsock = new TcpListener(9050);
  6.         newsock.Start();
  7.         Console.WriteLine("Waiting for a client...");
  8.         TcpClient client = newsock.AcceptTcpClient();
  9.         NetworkStream ns = client.GetStream();
  10.         string welcome = "Welcome to my test server";
  11.         data = Encoding.ASCII.GetBytes(welcome);
  12.         ns.Write(data, 0, data.Length);
  13.         while(true)
  14.         {
  15.             data = new byte[1024];
  16.             recv = ns.Read(data, 0, data.Length);
  17.             if (recv == 0)
  18.                 break;
  19.  
  20.             Console.WriteLine(
  21.                 Encoding.ASCII.GetString(data, 0, recv));
  22.             ns.Write(data, 0, recv);
  23.         }
  24.         ns.Close();
  25.         client.Close();
  26.         newsock.Stop();
  27.     }

Client Side
Expand|Select|Wrap|Line Numbers
  1. public static void Main()
  2.     {
  3.         byte[] data = new byte[1024];
  4.         string input, stringData;
  5.         TcpClient server;
  6.         try
  7.         {
  8.             server = new TcpClient("10.8.101.75", 9050);
  9.         } 
  10.         catch (SocketException)
  11.         {
  12.             Console.WriteLine("Unable to connect to server");
  13.             return;
  14.         }
  15.         NetworkStream ns = server.GetStream();
  16.         int recv = ns.Read(data, 0, data.Length);
  17.         stringData = Encoding.ASCII.GetString(data, 0, recv);
  18.         Console.WriteLine(stringData);
  19.         while(true)
  20.         {
  21.             input = Console.ReadLine();
  22.             if (input == "exit")
  23.                 break;
  24.             ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
  25.             ns.Flush();
  26.             data = new byte[1024];
  27.             recv = ns.Read(data, 0, data.Length);
  28.             stringData = Encoding.ASCII.GetString(data, 0, recv);
  29.             Console.WriteLine(stringData);
  30.         }
  31.         Console.WriteLine("Disconnecting from server...");
  32.         ns.Close();
  33.         server.Close();
Sep 5 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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