473,796 Members | 2,702 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 2826
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(_c lient.GetStream ());
stream.Write("H ello 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.GetSt ream());
//Cause the client allready has an instance of a TcpClient
stream.Write("H ello 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.co m> wrote in message
news:11******** ************@o1 3g2000cwo.googl egroups.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.GetStrea m());
stream.Write("H ello 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

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

Similar topics

3
9114
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 the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
2
7962
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 listener but it fails to send a third packet. I don't get any error messages. The client sends the packet successfully but it never arrives to the listener. This only happens when I send the packets one straight after the other. If I place a delay of 2...
3
3050
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 and arrives at the server but from after that i only a couple of KB from the start of the file which cases the picture to display only the very top (of the screen). All the pictures are saved at the client side before sent to the server and they...
5
72313
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 => Exception: "The requested address is not valid in its context" This works:
3
28656
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 slowly, about 4 seconds between send and receive. In our production environment with hundreds of transactions it was truly costly. a while ago i changed de TcpClient object. Now i am using a Socket (System.Net.Sockets.Socket) object and it...
3
4689
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 My code is as below, but I never get a response. I'm assuming my transmission is ending at the null character (the debug statement certainly only outputs up until the null character). Any ideas?
0
1975
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 server really sends everything I expect it to send. I am actually sending XML messages and this limit is a pain in the ... Here is the code: -------------------------------------------------------------------------- Dim tcpclient As New...
1
4540
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 closes the connection so I can exit my listener code. Here is what I have tried. First I asked MSDN and they said..
2
7543
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 TcpClient instance. If I run this loop without threading everything goes find. But if I do it within the thread, invariable on of the threads will fail to connect.
5
7324
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
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4114
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 we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.