Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A Question about Threading with Socket-Class

A Question about Threading with Socket-Class

Scheduled Pinned Locked Moved C#
data-structuresquestionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    I work on a class that handles the a tcp-connection but now i discovered a problem with my data-handling and cant solve it =/ First some Informations: The Socket-class provides the Async-Methodes BeginReceive and EndReceive to receive the data over the socket. When i call BeginReceive then another Methode will automatically raise.

    connSocket.BeginReceive( mainBuffer_, 0, mainBuffer_.Length, SocketFlags.None, out socketError_, new AsyncCallback( receivedata ), connSocket );
    

    In this case the methode "receivedata" will raise when data are available. In this methode "receivedata" do i fetch the data from the socket and save it a Queue, after that i start a second thread wich process the data from the queue.

    private SocketError socketError; 
    private volatile byte[] mainBuffer;
    private volatile int receivedDataSize;
    private volatile Thread processingThread;
    private volatile AutoResetEvent autoResetEvent;
    private volatile Queue> receivedDataQueue;
    
    ...
    
    private void receiveData( IAsyncResult aSyncResult ) {
        try {
            // Extract the delivered Socket
            Socket connSocket = ( Socket ) aSyncResult.AsyncState;
    
            // End Asyncronen Operation
            receivedDataSize = connSocket.EndReceive( aSyncResult, out socketFehler_ );
            
    
            // are data available ?
            if( receivedDataSize > 0 ) {
    
                  // Is the receivedDataSize bigger than the ReceiveBufferSize?
                  if( receivedDataSize != connSocket_.ReceiveBufferSize ) {
    
                      // extract the data from the mainBuffer
                      byte[] tmpBuffer = new byte[ receivedDataSize ];
                      Array.Copy( mainBuffer, tmpBuffer, receivedDataSize );
    
                      // add the mainBuffer-data to the receivedDataQueue
                      receivedDataQueue.Enqueue( new List( tmpEingangsPuffer ) );
                  }
                  else {
                      // add the mainBuffer-data to the receivedDataQueue
                      receivedDataQueue.Enqueue( new List( mainBuffer ) );
                  }
    
                  // start second Thread when the connections is still alive and the second Thread is null or is not alive
                  if( ( socketClosed == false ) &&
                      ( ( processingThread == null ) || ( processingThread.IsAlive == false ) ) ) {
    
                      
                      // Starte processing-Thread.
                      processingThread = new Thread( new ThreadStart( empfangsDatenVerarbeitung_ThreadMeth
    
    L 1 Reply Last reply
    0
    • M MarkPhB

      I work on a class that handles the a tcp-connection but now i discovered a problem with my data-handling and cant solve it =/ First some Informations: The Socket-class provides the Async-Methodes BeginReceive and EndReceive to receive the data over the socket. When i call BeginReceive then another Methode will automatically raise.

      connSocket.BeginReceive( mainBuffer_, 0, mainBuffer_.Length, SocketFlags.None, out socketError_, new AsyncCallback( receivedata ), connSocket );
      

      In this case the methode "receivedata" will raise when data are available. In this methode "receivedata" do i fetch the data from the socket and save it a Queue, after that i start a second thread wich process the data from the queue.

      private SocketError socketError; 
      private volatile byte[] mainBuffer;
      private volatile int receivedDataSize;
      private volatile Thread processingThread;
      private volatile AutoResetEvent autoResetEvent;
      private volatile Queue> receivedDataQueue;
      
      ...
      
      private void receiveData( IAsyncResult aSyncResult ) {
          try {
              // Extract the delivered Socket
              Socket connSocket = ( Socket ) aSyncResult.AsyncState;
      
              // End Asyncronen Operation
              receivedDataSize = connSocket.EndReceive( aSyncResult, out socketFehler_ );
              
      
              // are data available ?
              if( receivedDataSize > 0 ) {
      
                    // Is the receivedDataSize bigger than the ReceiveBufferSize?
                    if( receivedDataSize != connSocket_.ReceiveBufferSize ) {
      
                        // extract the data from the mainBuffer
                        byte[] tmpBuffer = new byte[ receivedDataSize ];
                        Array.Copy( mainBuffer, tmpBuffer, receivedDataSize );
      
                        // add the mainBuffer-data to the receivedDataQueue
                        receivedDataQueue.Enqueue( new List( tmpEingangsPuffer ) );
                    }
                    else {
                        // add the mainBuffer-data to the receivedDataQueue
                        receivedDataQueue.Enqueue( new List( mainBuffer ) );
                    }
      
                    // start second Thread when the connections is still alive and the second Thread is null or is not alive
                    if( ( socketClosed == false ) &&
                        ( ( processingThread == null ) || ( processingThread.IsAlive == false ) ) ) {
      
                        
                        // Starte processing-Thread.
                        processingThread = new Thread( new ThreadStart( empfangsDatenVerarbeitung_ThreadMeth
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      i'm not sure, but i think that Thread.IsAlive is a bit dangerous... msdn says about Thread.IsAlive : "true if this thread has been started and has not terminated normally or aborted otherwise false" so if an exception comes up in your dataprocessing you have a autoResetEvent that signals something and nobody listening. -- modified at 5:01 Thursday 29th November, 2007 never mind.. Thread.IsAlive does exactliy what it should :) you could put the stuff into a console application and do some Console.WriteLines to find out where it actually stops working..

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups