Solved

Stream problem with TcpClient when connected to TcpListener on desktop app

Posted on 2006-12-01
4
422 Views
Last Modified: 2013-11-18
Greetings -

   I have a c# client app running on an HP iPAQ handheld ( or the emulator ), and a c# server app running on my desktop. The client app uses the TcpClient class while the server app uses the TcpListener class.
   When I run these apps, it looks like the connect to each other ( I've set breakpoints to verify, and used netstat -a to verify that the apps see each other ).
   The problem is that when the client app does a read, it always throws a system.objectdisposedexception. The behavior is exactly the same on the emulator and the actual PDA. I can't for the life of me figure out what is wrong. Code snippets below.

Mucho thanks if you can figure this out.

PS. Using VS 2003, PocketPC 2003

server:
private void InitializeSocket()
{
      _socketInitialized = false;

      try
      {
            _ipAddress = Dns.Resolve( LOCAL_ADDRESS ).AddressList[0]; // validation
                        
            try
            {
                  _tcpListener = new TcpListener( _ipAddress, LOCAL_PORT );
                  _socketInitialized = true;
            }
}

private void WaitForSocketConnect()
{
      try
      {
            _tcpListener.Start();

            // blocking call
            try
            {
                  _tcpClient = _tcpListener.AcceptTcpClient();

                  _readThreadAlive = true;
                  _readThread.Start();

                  _writeThreadAlive = true;
                  _writeThread.Start();

            }
}


client:

private void InitializeSocket()
{
      try
      {
            // doing this just to make sure the address is valid
            IPAddress ipAddress = Dns.Resolve( DAQ_IP_ADDRESS ).AddressList[0];
            _socketInitialized = true;
      }
}

private void ConnectToSocket()
{
      while( _connectThreadAlive )
      {
            try
            {
                  // this blocks for quite awhile if no connection
                  _tcpClient = new TcpClient( DAQ_IP_ADDRESS, DAQ_PORT );

                  _connectThreadAlive = false; // shut down this thread

                  try
                  {
                        _readThreadAlive = true;
                        _readThread.Start();
                        }
               }
        
            if( _connectThreadAlive ) // means we're not connected yet
            {
                  _tcpClient = null; // being defensive
                  Thread.Sleep( ATTEMPT_CONNECT_INTERVAL );
            }
      }
}

private void ReadFromSocket()
{
      int numBytes;
      while( _readThreadAlive )
      {
            try
            {
                  using( NetworkStream stream = _tcpClient.GetStream() )
                  {
                        if( stream.DataAvailable ) // so we don't lock() forever
                        {
throws exception -->            numBytes = stream.Read( _readBuffer, 0, BUFFER_SIZE );
                              _app.ParseMessageFromDAQ( _readBuffer, numBytes );
                        }
                  }
            }

            Thread.Sleep( READ_INTERVAL );
}
0
Comment
Question by:Joe7Pak
4 Comments
 
LVL 9

Expert Comment

by:checoo
ID: 18084568
objectdisposed exception is caused while trying to invoke a method from a disposed object or if there is a failure reading from the network.

Use the InnerException property to get more details about the cause of the exception.
0
 

Author Comment

by:Joe7Pak
ID: 18093275
( Answering my own question )

I believe I know the answer - it appears that using

     using( NetworkStream stream = _tcpClient.GetStream() )

is problematic. I'm not sure why, maybe that once the stream is closed it cannot be used again. { "using" in this context will dispose of the stream once the block is exited } I found that by making "stream" global ( and therefore calling GetStream() only once ) that my code worked.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18155300
PAQd, 250 points refunded.

DarthMod
CS Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now