473,503 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2780
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9068
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
2
7901
by: Theo | last post by:
Hi, I am using a tcpclient and a tcplistener to send and receive packets. The client sends a packet and the listener replies with another one. I can send 2 packets and get an answer from the...
3
3018
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
5
72126
by: Horst Walter | last post by:
What is wrong here? IPAddress ipAddress = IPAddress.Parse("10.10.20.1"); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port); this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE...
3
28613
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
3
4659
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
0
1943
by: Torsten Brasch | last post by:
Hi All and Happy New Year ;) I have a very strange problem with System.Net.Sockets.TcpClient(). For some reason, the number of bytes I can receive is limited to 5460 bytes. I made sure that the...
1
4510
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
2
7459
by: craigkenisston | last post by:
I'm creating a TcpClient instance in an object which is created in a thread. I have a loop that goes about 6-7 cycles, creating the container object in each cycle and each object contains a...
5
7269
by: puzzlecracker | last post by:
It looks like i need to get IPEndPoint first, but I cannot figure out from msdn the eventual obtainment of machine name and port number. Please suggest a solution. Thanks
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
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
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
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
7441
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...
1
4987
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
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.