473,569 Members | 2,768 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 2799
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
9081
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...
2
7918
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...
3
3034
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...
5
72177
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
28627
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...
3
4672
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...
0
1955
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:...
1
4523
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
7494
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...
5
7283
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
7612
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...
0
7922
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. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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...
0
5218
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...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
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
1
1209
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.