471,627 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

TcpClient

Hi all, I'm using a TcpClient and it's stream to send data over a
network.
Now I'm noticing that my app is using a lot of memory, and I've noticed
that everytime I need to send something over the net I recreate a new
TcpClient instance instead of using one allready created. The thing is
that if I try to use one allready created the data does not reach the
other computer.
Is there anything I'm missing? Can I do that?

TIA.
Regards,
Sebastián Gómez
--
http://sgomez.blogspot.com

Feb 21 '06 #1
12 2698
Sebastian [IG&A::] wrote:
Hi all, I'm using a TcpClient and it's stream to send data over a
network.
Now I'm noticing that my app is using a lot of memory, and I've noticed
that everytime I need to send something over the net I recreate a new
TcpClient instance instead of using one allready created. The thing is
that if I try to use one allready created the data does not reach the
other computer.
Is there anything I'm missing? Can I do that?


It's hard to say without seeing some code. Could you post a short but
complete example which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html
for what I mean by that.

Jon

Feb 21 '06 #2
Ok, I'll try to explain myself.

Here's the class Client

public class Client
{
private IPAddress ip;
[NonSerialized] private TcpClient client;
private string version;
private string userName;
private string realName;
private FileStream fileObj;
private string fileName;
}

In a form I have an ArrayList of Clients.
So whenever I want to send a message to all this Clients I go like
this:

foreach(Client c in clients)
{
_client = new TcpClient(c.IP.ToString(),8080); //Create new TcpClient
every time

StreamWriter stream = new StreamWriter(_client.GetStream());
stream.Write("Hello World");
stream.Flush();
}

This works just fine, but what I wish to do is this
foreach(Client c in clients)
{
StreamWriter stream = new StreamWriter(c.tcpClient.GetStream());
//Cause the client allready has an instance of a TcpClient
stream.Write("Hello World");
stream.Flush();
}
But this does not work, it won't throw any exceptions either, It just
won't reach the other computer.
I hope it helps understand my problem.

Regards,
Sebastián
--
http://sgomez.blogspot.com

Feb 21 '06 #3
You should only need one TcpClient for the whole session with another peer.
If you create another one, your creating a new session.

--
William Stacey [MVP]

"Sebastian [IG&A::]" <se******************@gmail.com> wrote in message
news:11********************@o13g2000cwo.googlegrou ps.com...
Hi all, I'm using a TcpClient and it's stream to send data over a
network.
Now I'm noticing that my app is using a lot of memory, and I've noticed
that everytime I need to send something over the net I recreate a new
TcpClient instance instead of using one allready created. The thing is
that if I try to use one allready created the data does not reach the
other computer.
Is there anything I'm missing? Can I do that?

TIA.
Regards,
Sebastián Gómez
--
http://sgomez.blogspot.com
Feb 21 '06 #4
Sebastian [IG&A::] wrote:
Ok, I'll try to explain myself.


<snip>

As I said before, a short but *complete* program would really help.

See http://www.pobox.com/~skeet/csharp/incomplete.html

Jon

Feb 21 '06 #5
Hello, Sebastian!

Why can't you do something like this?

foreach(Client c in clients)
{
if ( c.client == null )
c.client = new TcpClient(c.IP.ToString(),8080); //Create new TcpClient every time

StreamWriter stream = new StreamWriter(c.client.GetStream());
stream.Write("Hello World");
stream.Flush();
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 21 '06 #6
Thanks William, is there any "state" or something I'm missing?
Why is it that the first time works just fine and then stops sending...
or receiving?

Sebastián
--
http://sgomez.blogspot.com

Feb 21 '06 #7
Thanks Vadym, I've done that after the first run the TcpClient is
already created so it won't create it again but it does not send the
message.
Or it does but the receiver doesn't get it.

Sebastián
--
http://sgomez.blogspot.com

Feb 21 '06 #8
How are you receiving data?

Also you can enable network tracing ( .NET 2.0 ) to see what is happeningn under the hood
( http://msdn2.microsoft.com/en-us/library/a6sbz1dx.aspx )
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 21 '06 #9
I'm not
What about .Net 1.1???

Sebastián
--
http://sgomez.blogspot.com

Feb 21 '06 #10
Hello, Sebastian!

If you're on Windows Server you can utilize network monitor. If not, take a look at Ethereal packet sniffer.( http://www.ethereal.com/ )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 21 '06 #11
These are "normally" an error on the receive side. You could be blocking
for a some bytes you already read or are not blocking on a receive at all.
It is hard to say without seeing a small sample that shows the issue.

--
William Stacey [MVP]

"Sebastian [IG&A::]" <se******************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Thanks Vadym, I've done that after the first run the TcpClient is
already created so it won't create it again but it does not send the
message.
Or it does but the receiver doesn't get it.

Sebastián
--
http://sgomez.blogspot.com
Feb 21 '06 #12
On the "other" side the code is the same for both examples...
I have a TcpClient created with the AcceptTcpClient method of a
TcpListener

With the first example this works just fine, while with the second
example it does not!

<b>Sebastián</b>
--
http://sgomez.blogspot.com

Feb 22 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Daniel | last post: by
2 posts views Thread by Theo | last post: by
3 posts views Thread by מורדי | last post: by
3 posts views Thread by Ricardo Quintanilla | last post: by
3 posts views Thread by Danny Tuppeny | last post: by
reply views Thread by Torsten Brasch | last post: by
2 posts views Thread by craigkenisston | last post: by
5 posts views Thread by puzzlecracker | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.