Solved

Stream problem with TcpClient when connected to TcpListener on desktop app

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IPhone IOS Development Project Problem 4 58
phone mike not working samsung 4 146
Reset Kindle Fire HD 7 2 126
Undo Sprint PCS iPhone 7 Plus iOS upgrade 4 28
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…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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