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 - public static void Main()
-
{
-
int recv;
-
byte[] data = new byte[1024];
-
TcpListener newsock = new TcpListener(9050);
-
newsock.Start();
-
Console.WriteLine("Waiting for a client...");
-
TcpClient client = newsock.AcceptTcpClient();
-
NetworkStream ns = client.GetStream();
-
string welcome = "Welcome to my test server";
-
data = Encoding.ASCII.GetBytes(welcome);
-
ns.Write(data, 0, data.Length);
-
while(true)
-
{
-
data = new byte[1024];
-
recv = ns.Read(data, 0, data.Length);
-
if (recv == 0)
-
break;
-
-
Console.WriteLine(
-
Encoding.ASCII.GetString(data, 0, recv));
-
ns.Write(data, 0, recv);
-
}
-
ns.Close();
-
client.Close();
-
newsock.Stop();
-
}
Client Side -
public static void Main()
-
{
-
byte[] data = new byte[1024];
-
string input, stringData;
-
TcpClient server;
-
try
-
{
-
server = new TcpClient("10.8.101.75", 9050);
-
}
-
catch (SocketException)
-
{
-
Console.WriteLine("Unable to connect to server");
-
return;
-
}
-
NetworkStream ns = server.GetStream();
-
int recv = ns.Read(data, 0, data.Length);
-
stringData = Encoding.ASCII.GetString(data, 0, recv);
-
Console.WriteLine(stringData);
-
while(true)
-
{
-
input = Console.ReadLine();
-
if (input == "exit")
-
break;
-
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
-
ns.Flush();
-
data = new byte[1024];
-
recv = ns.Read(data, 0, data.Length);
-
stringData = Encoding.ASCII.GetString(data, 0, recv);
-
Console.WriteLine(stringData);
-
}
-
Console.WriteLine("Disconnecting from server...");
-
ns.Close();
-
server.Close();
0 1992 Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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;
...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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...
|
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: 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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |