Solved

Stream problem with TcpClient when connected to TcpListener on desktop app

Posted on 2006-12-01
4
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Account Lockout 12 36
Samsung Galaxy S4 Port Forwarding? 1 215
Droid 4 15 159
Identity Management & Mobile Apps 5 112
With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

751 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