Solved

Stream problem with TcpClient when connected to TcpListener on desktop app

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

930 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

16 Experts available now in Live!

Get 1:1 Help Now