473,324 Members | 2,535 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,324 software developers and data experts.

Existance of networkstream

Hi
I have a simple question. Here is the code related to my question:

while (true)
{
if (tcpListenerServer.Pending() && !this.Disposing)
{
TcpClient tcpClient =
tcpListenerServer.AcceptTcpClient();

downloadThreadClass obj = new
downloadThreadClass(tcpClient.GetStream());

Thread myThread = new Thread(new
ThreadStart(obj.downloadThreadFunction));
myThread.Start();

}
}

TcpClient object returned by AccepTcpClient is destroyed right after program
leaved 'if' block.
before program do this, i'am passing NetworkStream from tcpClient to the
thread which is proceeding some task on this stream.
The question is will it work? The stream is retreved but tcpClient will not
exist during operations on this stream.
Thanks
PK
Jun 19 '06 #1
2 4053
"PiotrKolodziej" <pi*************@gmail.com> wrote:
I have a simple question. Here is the code related to my question:

while (true)
{
if (tcpListenerServer.Pending() && !this.Disposing)
{
This forms a busy polling loop, which will waste CPU resources and will
starve other processes and threads. It's better to call a method which
will block (possibly with a timeout). After every timeout, you can then
check if the listening thread should be shut down. For example, check
out Socket.Poll() with SelectMode.SelectRead. You can get the listening
socket behind the TcpListener from its Server property.

Alternatively, you can cancel the blocking call to AcceptTcpClient by
calling Stop() on the server on a different thread. For example:

---8<---
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class App
{
static void Main()
{
TcpListener server = new TcpListener(2000);
ManualResetEvent serverRunning = new ManualResetEvent(false);

Thread serverThread = new Thread(delegate()
{
server.Start();
serverRunning.Set();
try
{
for (;;)
{
TcpClient client = server.AcceptTcpClient();
}
}
catch (SocketException ex)
{
// Blocking interrupted
}
});
serverThread.Start();
// prevent race to server.Stop() before server.Start()
serverRunning.WaitOne();

Console.Write("Press Enter to stop listening...");
Console.ReadLine();
server.Stop();
serverThread.Join();
}
}
--->8---
TcpClient tcpClient =
tcpListenerServer.AcceptTcpClient();

downloadThreadClass obj = new
downloadThreadClass(tcpClient.GetStream());

Thread myThread = new Thread(new
ThreadStart(obj.downloadThreadFunction));
myThread.Start();

}
}

TcpClient object returned by AccepTcpClient is destroyed right after program
leaved 'if' block.
The variable called 'tcpClient' goes out of scope, but the object it
references (on the managed heap) is not disposed of.
before program do this, i'am passing NetworkStream from tcpClient to the
thread which is proceeding some task on this stream.
The question is will it work? The stream is retreved but tcpClient will not
exist during operations on this stream.


The NetworkStream object (from TcpClient.GetStream()) keeps a reference
to the backing socket (i.e. TcpClient.Client), so it keeps it alive from
GC and finalization.

That said, you should dispose of the underlying socket or TcpClient or
an owning NetworkStream when it's done with it (closing/disposing either
of TcpClient or a NetworkStream constructed to own the stream will also
dispose of the socket; TcpClient.GetStream() creates an owning
NetworkStream).

The documentation for TcpClient.Close() says that it does not close the
underlying connection, but it is wrong: .NET Reflector shows that it
does in fact close the connection.

-- Barry

--
http://barrkel.blogspot.com/
Jun 19 '06 #2
Hi,

Your question is very easily answered, just pass the TcpClient to the new
downloadThreadClass instance (instead of its NetworkStream ) .

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"PiotrKolodziej" <pi*************@gmail.com> wrote in message
news:99**************************@news.chello.pl.. .
Hi
I have a simple question. Here is the code related to my question:

while (true)
{
if (tcpListenerServer.Pending() && !this.Disposing)
{
TcpClient tcpClient =
tcpListenerServer.AcceptTcpClient();

downloadThreadClass obj = new
downloadThreadClass(tcpClient.GetStream());

Thread myThread = new Thread(new
ThreadStart(obj.downloadThreadFunction));
myThread.Start();

}
}

TcpClient object returned by AccepTcpClient is destroyed right after
program leaved 'if' block.
before program do this, i'am passing NetworkStream from tcpClient to the
thread which is proceeding some task on this stream.
The question is will it work? The stream is retreved but tcpClient will
not exist during operations on this stream.
Thanks
PK

Jun 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cmjman | last post by:
I have an issue where networkStream.Write doesn't perform its write downstream from my client program until the network stream is closed. I then see the data that was sent appear on the other side....
1
by: Daniel | last post by:
i would like to konw when the data sent so that i can close the streamwriter and networkstream is there some sort of call backs/events i have to implement for this to work? if so how? can i just...
4
by: 0to60 | last post by:
I have a class that wraps a TcpClient object and manages all the async reading of the socket. It works really nice, and I use it all over the place. But there's this ONE INSTANCE where I create...
1
by: kmacintyre | last post by:
I am trying to us a simple NetworkStream to transfer a file over tcp. This works most of the time, but one specific file never downloads(.mdb file). It seems to close the socket and I get an...
6
by: Ryan | last post by:
Hi, I am confused with how NetworkStream works. My application needs to handle heavy requests sent through TCP socket connection. I use NetworkStream.Read method to get the stream...
4
by: Kai Thorsrud | last post by:
Hi! How do i check: if "NetworkStream.Null = true then" it says .Null does not supports type Boolean Thanks /Kai
1
by: hamid_2020 | last post by:
I wrote a class to connect to a server using tcpclient. I need to connect to the server and the connection must be open.Then i need to send request to the server again and again.But the problem is...
0
by: Al Wilkerson | last post by:
Hey, Has anyone ever got a "Unable to read data from transport connected" message after reading data from a streamreader composed of a networkstream. For example: Server TcpListener...
7
by: littleIO | last post by:
Hi, I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated. I have a server which listens, receives calls, processes them and sends back the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.