none
Compact Framework - TCP Client - Network Stream Reading the data twice throws an exception

    General discussion

  • Hi ,

    I have created a Streaming based application where my requirement is to send the Live recorded data to the server. The data is sent via Network Stream object under TCP Client.
    The server analyzes the data in real time and sends the chunked response to the client. My application sends data continously to the server until it checks for NetworkStream.DataAvailable method. The response data from server if received in chunks I have to make two read calls to the Network Stream object. In the second read the network stream object throws an exception ?????. I am not sure why because the network stream object still shows the data available property to true.

    Below is the snippet of code used which is not a rocket science.

     

     

    //Write the data to network stream

     

    public ERRORCODE WriiteToStream(byte[] data)

    {

     

    try

    {

     

    bool isAvailable = _networkStream.DataAvailable;

     

    if (isAvailable == true)

    {

     

    do

    {

     

    //32 KB Buffer for reading the data

     

    byte[] dataFrmServer = new byte[BUFFER];

     

    int dataRead = _networkStream.Read(dataFrmServer, 0, dataFrmServer.Length);

     

    //Best fit buffer for data in 32 KB buffer

     

    byte[] DataRead = new byte[dataRead];

     

    Array.Copy(dataFrmServer, 0, DataRead, 0, dataRead);

    resultBuffer = resultBuffer + System.Text.

    Encoding.ASCII.GetString(DataRead, 0, DataRead.Length);

    }

    while (_networkStream.DataAvailable);

    }

     

    else

     

    //Writing Data on Network Stream
    if (!HaveResponse())

    {

    _networkStream.Write(data, 0, data.Length);

    }

    }

    }

     

    catch (Exception ex)

    {

     

    return ERRORCODE.FAILURE;

    }

     

    return ERRORCODE.SUCCESS;

    }


    In the above code HaveResponse just checks if the end of data flag has been received from the server and returns true or false.
    Can anyone tell me what is wrong with the above code ???

    Wednesday, June 24, 2009 7:20 AM

All replies

  • Hi,

    Which exception is actually raised?

    Thanks

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Wednesday, June 24, 2009 8:06 AM
  • Hi ,

    Thanks for the reply.

    Well, I receive the socket exception with code 10054.

    I tried to run a sample app in full framework  2.O which allows multiple reading and seems to work fine..

    I captured the network traffic using ethereal it shows the complete server response which doesn't reach the application.


    Thanks
    Swapnil
    Wednesday, June 24, 2009 8:28 AM
  • Exact Exception received is as below : at System.Net.Sockets.Socket.ReceiveNoCheck() at System.Net.Sockets.Socket.Receive() at System.Net.Sockets.NetworkStream.Read() at SocketTestRecordingStreaming.SocketCommunication.GetResponse() I hope the above exception could could answer your query.
    Wednesday, June 24, 2009 9:28 AM
  • Did you look up the error code? "An existing connection was forcibly closed
    by the remote host".

    Paul T.

    "Swapnil Jain" <=?utf-8?B?U3dhcG5pbCBKYWlu?=> wrote in message
    news:50171453-3f0a-4c4c-8133-c5c5cc13797c...
    > Hi ,
    >
    > Thanks for the reply.
    >
    > Well, I receive the socket exception with code 10054.
    >
    > I tried to run a sample app in full framework? 2.O which allows multiple
    > reading and seems to work fine..
    >
    > I? captured the network traffic using ethereal? it shows the complete?
    > server response? which doesn't reach the application.
    >
    >
    > Thanks
    > Swapnil
    >


    Wednesday, June 24, 2009 4:17 PM
  • We are changing the issue type to "General Discussion" because you have not followed up with the necessary information. If you have more time to provide more information, please feel free to change the issue type back to "Question". If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
    Please mark the post that helps you, and unmark that does not. This benefits our community members.
    Tuesday, June 30, 2009 8:28 AM
  • Correct me if I'm wrong (it's been a while since I looked at this), but I remember DataAvailable and Connected properties on these classes not functioning as advertised by the documentation.
    You should probably just rely on using the read/write methods to determine connectivity and refactor your code to handle no data and exceptions appropriately.

    Just googling quickly I think some details are here:
    http://dam.mellis.org/2004/08/net_socket_bugs_gotchas/
    Wednesday, July 01, 2009 10:32 PM