PC Review


Reply
Thread Tools Rate Thread

reusing TcpClient object

 
 
Junaid
Guest
Posts: n/a
 
      25th Nov 2004
I've got a situation where I need to send small strings across the
network to a listening application. Events are fired randomly on the
client and I need to send one message per event. I've got a separate
thread running to handle network communication so I don't block the
rest of the application.

Currently, what I do is create a new TcpClient object and connect to
the listening application whenever I need to send something. Here's
the code which does that (managed C++):

// where i need to send
TcpClient *theSocket = new TcpClient(IPAddress, Port);
NetworkStream *theStream = theSocket->GetStream();
theStream->Write(str, 0, len);
theSocket->Close();

The TcpClient constructor I'm using makes a new object and connects in
a single step. However, the connection process blocks the thread for a
few seconds while it connects to the listener. While this does not
significantly impact the spec, it would be nice to have the messages
go over instantly rather than waiting a few seconds for the client to
connect for every message.

Here's what I tried doing to get around the problem:

// created pseudo global variables
__value struct dummy
{
static TcpClient *theSocket;
static NetworkStream *theStream;
};

// in my class' constructor:
dummy::theSocket = new TcpClient(IPAddress, Port);
dummy::theStream = dummy::theSocket->GetStream();

// where i need to send
dummy::theStream->Write(str, 0, len);

The problem is that this only works the first time around, and on
subsequent Write()s, I get the following exception: "Unable to write
data to the transport connection."

Incidentally, the listening application is currently just a test app I
downloaded off some site (no source code unfortunately) which will sit
there and listen on a specified port. It simply displays anything it
receives. I'm not sure if this app is forcibly disconnecting my client
once it has received the data since I'm not actually disconnecting.

I'm wondering if my approach is any good or if there's some other way
I can achieve what I want. Also, I'm new to socket/network programming
so it's entirely possible that what I'm attempting is not only not
viable, but also a bit silly to boot.

Cheers
 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2FqYWwgU2luaGE=?=
Guest
Posts: n/a
 
      25th Nov 2004
try to create a separate thread for connection which is using a
System.Collections.Queue object to identify whether the queue is emtpy or not
if not then Dequeue the topmost item and throw it to the relavant
destination using that thread. you can also start a new thread for each
message call if required.

"Junaid" wrote:

> I've got a situation where I need to send small strings across the
> network to a listening application. Events are fired randomly on the
> client and I need to send one message per event. I've got a separate
> thread running to handle network communication so I don't block the
> rest of the application.
>
> Currently, what I do is create a new TcpClient object and connect to
> the listening application whenever I need to send something. Here's
> the code which does that (managed C++):
>
> // where i need to send
> TcpClient *theSocket = new TcpClient(IPAddress, Port);
> NetworkStream *theStream = theSocket->GetStream();
> theStream->Write(str, 0, len);
> theSocket->Close();
>
> The TcpClient constructor I'm using makes a new object and connects in
> a single step. However, the connection process blocks the thread for a
> few seconds while it connects to the listener. While this does not
> significantly impact the spec, it would be nice to have the messages
> go over instantly rather than waiting a few seconds for the client to
> connect for every message.
>
> Here's what I tried doing to get around the problem:
>
> // created pseudo global variables
> __value struct dummy
> {
> static TcpClient *theSocket;
> static NetworkStream *theStream;
> };
>
> // in my class' constructor:
> dummy::theSocket = new TcpClient(IPAddress, Port);
> dummy::theStream = dummy::theSocket->GetStream();
>
> // where i need to send
> dummy::theStream->Write(str, 0, len);
>
> The problem is that this only works the first time around, and on
> subsequent Write()s, I get the following exception: "Unable to write
> data to the transport connection."
>
> Incidentally, the listening application is currently just a test app I
> downloaded off some site (no source code unfortunately) which will sit
> there and listen on a specified port. It simply displays anything it
> receives. I'm not sure if this app is forcibly disconnecting my client
> once it has received the data since I'm not actually disconnecting.
>
> I'm wondering if my approach is any good or if there's some other way
> I can achieve what I want. Also, I'm new to socket/network programming
> so it's entirely possible that what I'm attempting is not only not
> viable, but also a bit silly to boot.
>
> Cheers
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reusing same reader object throughout lifetime of app vbDavidC Microsoft VB .NET 1 27th Mar 2007 05:20 PM
newbie: reusing command object in same procedure vbDavidC Microsoft VB .NET 3 10th Feb 2007 07:43 AM
reusing command object Andy G Microsoft Access ADP SQL Server 7 9th Feb 2006 11:20 PM
Getting remote ip from TcpClient object Ethan Microsoft C# .NET 1 7th Oct 2004 11:42 PM
ReUsing a defined object - by redefining it =?Utf-8?B?Qm9i?= Microsoft C# .NET 2 20th Feb 2004 03:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:58 PM.