473,503 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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
0 1992

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

Similar topics

5
23340
by: Parahat Melayev | last post by:
I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a...
5
44944
by: Morten | last post by:
How do I detect if a client socket is no longer connected to the listen tcp socket ? I have tried with (just an example): --------------------- Socket tcpSocket; ...
0
1029
by: Paul Tomlinson | last post by:
How can I tell if a DCOM server I am connected to has been destroyed? All, I have a C# client app which connects to a C++/MFC DCOM server. The DCOM server implements a sink object aswell as...
1
1491
by: Mike Dole | last post by:
I'm sorry to bother you with this question but it was either this or giving up and trying to go for a simpler solution (which I will if this is not gonna work out..) I'm afraid this is way out...
1
15593
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
0
1715
by: khu84 | last post by:
Here is client server very simple code, seems to work with telnet but with with web client code gives blank output. Following is the server code:- <?php function...
1
1871
by: Miroslav Endys | last post by:
I have two apps. Client and Server (both are windows console apps). Im using Client Socket of this definition AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp and Im using async...
3
2043
by: jlgeris | last post by:
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...
1
3710
by: JamesB | last post by:
OK, after lots of faffing I have finally gotten remoting working - basically I have a windows service (the "server") and a forms app (the "client"). The forms app can call methods in the server,...
0
7086
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
7330
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...
1
6991
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...
0
7460
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...
0
5578
agi2029
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,...
1
5014
isladogs
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.