consuming mixed ascii/binary messages from a socket with NSInputStream - help !!

Discussion in 'Mac Programming' started by stevehicks, Feb 28, 2010.

  1. stevehicks macrumors newbie

    Joined:
    Feb 28, 2010
    #1
    Hi Guys

    I'm writing a client that listens to messages that composed of ascii and binary strings. I want to use NSInputStream because I want to run the client on both Mac and GNUStep on Solaris. Here's a C struct that describes the first few fields of the messages :

    Code:
        char messageType;
        char ProtoOrVer;
        uint16_t MsgLen;    
        uint32_t MsgSeqNum;
        uint64_t ClOrdID;
        char OnBehalfOfCompID[11];
        char Rule80A;
        char Account[12];
        char TechnicalOrdType;
        char ClearingFirm[8];
        char ClientID[8];
        char FreeText[18];
        char OpenClose;
        char ClearingHandlingType;
        char Filler1;
        uint16_t NoQuoteEntries;
    

    I've looked at the NSInputStream reference but it only reads uint8_t at a time. I think I probably need to override read:maxLength: to do this...what do you think ?? If so, any tips ?? I've not been able to find an example of how to do it :(

    Any pointerrs greatly appreciated !!!

    thanks :)
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Why do you think you need to subclass NSInputStream at all?

    It seems to me your client code simply asks the NSInputStream to read bytes into a buffer of length sizoef(myHeader), where myHeader is the struct definition you posted. You then perform any endian conversions needed on the non-char struct members, and bam, there's your struct in the buffer.

    I don't know what you mean by "it only reads uint8_t at a time". Do you understand Objective-C? Do you realize that read:maxLength: takes a first parameter that is pointer to uint8_t, and a second parameter representing the max number of bytes to put at said pointer? The count of actual bytes put into buffer is returned. This is essentially identical to every read() or fread() function ever created.
     
  3. stevehicks thread starter macrumors newbie

    Joined:
    Feb 28, 2010
    #3
    I'm just starting out with C and Objective C actually and I clearly misunderstood. Thanks for enlightening me so succinctly.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Glad to have helped.

    However, if you're just starting out with both C and Objective-C, then a project of this apparent complexity may not be the best choice.
     

Share This Page