TcpClient

  • Thread starter Thread starter Sebastian [IG&A::]
  • Start date Start date
S

Sebastian [IG&A::]

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
 
Sebastian said:
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
 
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
 
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]

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
 
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
 
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
 
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
 
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]

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
 
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top