的TcpClient [英] TcpClient
问题描述
大家好,我正在使用TcpClient,它是通过
网络发送数据的流。
现在我注意到了我的应用程序正在使用大量内存,我注意到每次我需要通过网络发送内容时我会重新创建一个新的
TcpClient实例,而不是使用一个已经创建了。事情是
,如果我尝试使用一个已经创建的数据,那么数据就无法到达其他电脑。
我还有什么吗?我失踪了?我可以这样做吗?
TIA。
问候,
$ b $bSebastiánGómez
-
http://sgomez.blogspot.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
推荐答案
Sebastian [IG& A ::]写道:
Sebastian [IG&A::] wrote:
大家好,我正在使用TcpClient而且它是'流通过
网络发送数据。
现在我注意到我的应用程序正在使用大量内存,而且我注意到每次我需要发送一些内容时网络我重新创建一个新的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?
没有看到一些代码就很难说。你可以发一个简短但是
完整的例子来证明这个问题吗?请参阅
http://www.pobox.com/ ~siget / csharp / complete.html
我的意思是什么。
Jon
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
好的,我会试着解释一下自己。
这里是班级客户
公共类客户
{
私有IPAddress ip;
[NonSerialized]私有TcpClient客户端;
私有字符串version;
private string userName;
private string realName;
private FileStream fileObj;
private string fileName;
}
在表单中我有一个客户端的ArrayList。
所以,每当我想向所有这些发送消息时客户我喜欢
这个:
foreach(客户中的客户c)
{
_client = new TcpClient(c.IP.ToString(),8080); //每次创建新的TcpClient
StreamWriter stream = new StreamWriter(_client.GetStream());
stream。写(Hello World);
stream.Flush();
}
这很好用,但是我想做的是这个
foreach(客户端的客户端c)
{
StreamWriter stream = new StreamWriter(c.tcpClient.GetStream) ());
//导致客户端allready有一个TcpClient实例
stream.Write(Hello World);
stream.Flush();
}
但这不起作用,也不会抛出任何例外,它只是
不会到达另一台电脑。
我希望它有助于理解我的问题。
问候,
$ b $bSebastián
-
http://sgomez.blogspot.com
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
您应该只需要一个TcpClien与另一个同伴进行整个会话。
如果你创建另一个,你创建一个新的会话。
-
William Stacey [MVP]
" Sebastian [IG& A ::]" < SE ****************** @ gmail.com>在消息中写道
news:11 ******************** @ o13g2000cwo.googlegrou ps.com ...
大家好,我正在使用TcpClient,它是通过
网络发送数据的流。
现在我注意到我的应用我正在使用大量的内存,而且我注意到每次我需要通过网络发送内容时我会重新创建一个新的
TcpClient实例,而不是使用一个全部内存创建。事情是
,如果我尝试使用一个已经创建的数据,那么数据就无法到达其他电脑。
我还有什么吗?我失踪了?我可以这样做吗?
TIA。
问候,
$ b $bSebastiánGómez
-
http://sgomez.blogspot.com
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
这篇关于的TcpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!