Solved

Stream problem with TcpClient when connected to TcpListener on desktop app

Posted on 2006-12-01
4
427 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the most popular question in our Windows Mobile Programming zone is about the backlight - we all want to keep it on when our application is running. Few years ago the function SystemIdleTimerReset() did this job. On our side we had to detec…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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

9 Experts available now in Live!

Get 1:1 Help Now