[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [C/C++] TCP/IP byte-order kommunikation



On 30 Jan 2004 15:51:06 +0100
Peter Makholm <sslug@sslug> wrote:

> Jeg ville ikke bliver overrasket over at den slags spil absolut ikke
> var portable, men ignorerede fuldstændig alt om endianess og størelse
> af datatyper.

Det er sikkert rigtigt men ville denne kode ikke kunne bruges. Jeg har
ikke testet den på nogen måde. Så der er sikkert fejl. I øvrigt er det
også kun et eksempel... jeg kunne ikke finde på at bruge det "in real
life".

--------------------------------

struct ProtocolHeader {
  int type;
  int size;
  void *data;
};



void TServer::slotIncomingData()
{
  QSocket *s = (QSocket*)sender();

  // We will only read from the socket when
  // the hole protocol header are in place.
  if( s->bytesAvailable() >= sizeof(ProtocolHeader) ) {
    ProtocolHeader ph;
    Q_LONG len = 0;
    Q_ULONG left = 0;
    if( s->readBlock( (char *)&ph, sizeof(ProtocolHeader)) != -1 ) {
      // We only accept text string.
      if( ph.type == 1 && ph.size > 0 ) {
        ph.data = new char[ ph.size ];
        left = ph.size;
        while( left ) {
          if( (len = s->readBlock( (char *)ph.data, left )) != -1 )
            left -= len;
          else
            break;
        }				
        
        qDebug( QString("type: %1, size: %2, data: %3")
                         .arg(ph.type)
                         .arg(ph.size)
                         .arg( (char *) ph.data) );				
        delete [] (char *)ph.data;
      }
    }
    else {
      qFatal("readBlock(...) error!");
    }
  }
}


----------------------


(John =;-)


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *