Trying to use NSStream

  • Hmm.. I am quite lost here and cannot seem to find any information. I
    am trying to use NSStream to connect to and communicate with a mail
    server. The connection actually seems ok and I get the +OK hello
    message from the server then all further communications fail.

    When I try to send a request to the serve I get no data back...
    However I have no trouble at all communicating with http servers. Any
    way this is my write code:

    - (IBAction)sendText:(NSString*)stringToSend
    {
    NSLog(@"StringToSend %@",stringToSend);
    [rawData insertText:[NSString stringWithFormat:@"\n%@",stringToSend]];
    [textField setStringValue:@""];
        NSData * dataToSend = [stringToSend
    dataUsingEncoding:NSISOLatin1StringEncoding];
        if (ostream) {
            int remainingToWrite = [dataToSend length];
            void * marker = (void *)[dataToSend bytes];
            while (0 < remainingToWrite) {
                int actuallyWritten = 0;
                actuallyWritten = [ostream write:marker
    maxLength:remainingToWrite];
                remainingToWrite -= actuallyWritten;
                marker += actuallyWritten;
            }
        }
    }

    After I send a block of data and get nothing back. The input stream
    has no data available and the event sent to the controller is
    NSStreamEventHasSpaceAvailable along with the outputstream for the
    stream variable. I have made sure that I append \n to the end of each
    string I send, I have also tried \r instead but nothing seems to help.
    Any ideas?
  • Ok... Did any one else get the memo that the EOL character had been
    changed to the char(10).
    Every document I've read and all information I have seen tells me that
    I suffix every line I send to a server with either \n or \r or \n\r or
    some other such combination. Either way... I have never had to do
    sendString = [NSString stringWithFormat:@"%@\r%c",aString,10]; I do
    not know what Char 10 is however I am assuming it is an EOL or EOF
    character? Either way this seems to be unique to NSStream as other
    tools I've used require only the \r. When using NSStream it seems that
    for anything but http it requires both a \r and character 10. (\n
    didn't work and neither did the character by itself.)
    I discovered that with every server, the final character of each
    welcome message was ascii character 10 preceded by character 13 so I
    tried it and it worked.

    I suppose I'm hoping some one can explain this to me. Even though it
    is working I do not understand why and would be grateful if some one
    could clarify.

    On Jan 12, 2008, at 4:24 PM, Development wrote:

    > Hmm.. I am quite lost here and cannot seem to find any information.
    > I am trying to use NSStream to connect to and communicate with a
    > mail server. The connection actually seems ok and I get the +OK
    > hello message from the server then all further communications fail.
    >
    > When I try to send a request to the serve I get no data back...
    > However I have no trouble at all communicating with http servers.
    > Any way this is my write code:
    >
    > - (IBAction)sendText:(NSString*)stringToSend
    > {
    > NSLog(@"StringToSend %@",stringToSend);
    > [rawData insertText:[NSString stringWithFormat:@"\n
    > %@",stringToSend]];
    > [textField setStringValue:@""];
    > NSData * dataToSend = [stringToSend
    > dataUsingEncoding:NSISOLatin1StringEncoding];
    > if (ostream) {
    > int remainingToWrite = [dataToSend length];
    > void * marker = (void *)[dataToSend bytes];
    > while (0 < remainingToWrite) {
    > int actuallyWritten = 0;
    > actuallyWritten = [ostream write:marker
    > maxLength:remainingToWrite];
    > remainingToWrite -= actuallyWritten;
    > marker += actuallyWritten;
    > }
    > }
    > }
    >
    > After I send a block of data and get nothing back. The input stream
    > has no data available and the event sent to the controller is
    > NSStreamEventHasSpaceAvailable along with the outputstream for the
    > stream variable. I have made sure that I append \n to the end of
    > each string I send, I have also tried \r instead but nothing seems
    > to help. Any ideas?
  • On 12 Jan 08, at 17:16, Development wrote:
    > Ok... Did any one else get the memo that the EOL character had been
    > changed to the char(10).

    ASCII 10 is the newline character, and has been since the ASCII
    standard was created in the 1960s. If your compiler is outputting
    something else for the '\n' escape, something's wrong.
  • On Jan 12, 2008, at 5:16 PM, Development wrote:

    > Ok... Did any one else get the memo that the EOL character had been
    > changed to the char(10).
    > Every document I've read and all information I have seen tells me
    > that I suffix every line I send to a server with either \n or \r or
    > \n\r or some other such combination. Either way... I have never had
    > to do sendString = [NSString stringWithFormat:@"%@\r%c",aString,10];
    > I do not know what Char 10 is however I am assuming it is an EOL or
    > EOF character?

    10 (0x0A) is a newline. 'man ascii' when in doubt. It is the same as
    "\n". Almost all text-based network protocols require lines to end in
    a carriage-return/newline pair. So "Foo\r\n".

    --Brady
  • on 1/12/08 4:24 PM, <development...> purportedly said:

    > When I try to send a request to the serve I get no data back...
    > However I have no trouble at all communicating with http servers. Any
    > way this is my write code:

    The difference between http and pop3 is that http is generally
    transactional, i.e. in a single connection you send a request and the server
    responds. At that point, you (or the server) can close the connection
    without concern. This is why http is generally considered stateless. POP,
    however, requires a series of request/response transactions on a single
    connection.

    So the first thing you may want to check is whether you are closing the
    connection after the first transaction (request/response), such as in
    response to a NSStreamEventEndEncountered.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • Well the only think I can think then is that I did not type \r\n but
    must have instead typed \n\r which would of course be wrong.

    P.S. Thank you to every one who responded, things are clearer now.

    On Jan 12, 2008, at 6:51 PM, Brady Duga wrote:

    > On Jan 12, 2008, at 5:16 PM, Development wrote:
    >
    >> Ok... Did any one else get the memo that the EOL character had been
    >> changed to the char(10).
    >> Every document I've read and all information I have seen tells me
    >> that I suffix every line I send to a server with either \n or \r or
    >> \n\r or some other such combination. Either way... I have never had
    >> to do sendString = [NSString stringWithFormat:@"%@\r%c",aString,
    >> 10]; I do not know what Char 10 is however I am assuming it is an
    >> EOL or EOF character?
    >
    > 10 (0x0A) is a newline. 'man ascii' when in doubt. It is the same as
    > "\n". Almost all text-based network protocols require lines to end
    > in a carriage-return/newline pair. So "Foo\r\n".
    >
    > --Brady
  • Below is a snippet of the code I'm using for getting the incoming data
    from the stream. It is cutting download speed from what it should be
    at around 200Kbps to 22 - 30 Kbps.. I know it is because it reads the
    data one byte at a time but my attempts to have it read all bytes
    available have produced much failure. Can any one suggest how to speed
    this piece of code up? (The main problem I've been having is that if
    it reads the whole buffer I cannot seem to find the \r\n any where,
    and so the program cannot find the end of the message from the server.
    Also when I tre the getBuffer:length: function, the length I get seems
    always to be 0.

    - (void)stream:(NSStream *)aStream handleEvent:
    (NSStreamEvent)streamEvent {
        NSInputStream * instream;
    NSString * error;
        switch(streamEvent) {
            case NSStreamEventHasBytesAvailable:;

                uint8_t oneByte;
                int actuallyRead = 0;
                instream = (NSInputStream *)aStream;
                if (!dataBuffer) {
                    dataBuffer = [[NSMutableData alloc] initWithCapacity:
    2048];
                }
                actuallyRead = [istream read:&oneByte maxLength:1]; <- if
    I set max length to 2048 it crashes
                if (actuallyRead == 1) {
                    [dataBuffer appendBytes:&oneByte length:1];
                }

        if (oneByte == '\n' || oneByte == '\r') {
        [self newBytesAvailable:dataBuffer];
        [dataBuffer release];
        dataBuffer = nil;
        }
                break;
  • On 13 Jan 08, at 14:41, Development wrote:
    > Below is a snippet of the code I'm using for getting the incoming
    > data from the stream. It is cutting download speed from what it
    > should be at around 200Kbps to 22 - 30 Kbps.. I know it is because
    > it reads the data one byte at a time but my attempts to have it read
    > all bytes available have produced much failure. Can any one suggest
    > how to speed this piece of code up? (The main problem I've been
    > having is that if it reads the whole buffer I cannot seem to find
    > the \r\n any where, and so the program cannot find the end of the
    > message from the server. Also when I tre the getBuffer:length:
    > function, the length I get seems always to be 0.

    > uint8_t oneByte;
    > actuallyRead = [istream read:&oneByte maxLength:1]; <- if
    > I set max length to 2048 it crashes

    Well, yeah, I imagine it would - because you're trying to read 2 kB
    into a one-byte buffer (and trashing your stack in the process).
  • Do you need to send \r? If so, send it before \n, i.e. send \r\n, not
    \n\r

    On 12-Jan-08, at 8:16 PM, Development wrote:

    > Ok... Did any one else get the memo that the EOL character had been
    > changed to the char(10).
    > Every document I've read and all information I have seen tells me
    > that I suffix every line I send to a server with either \n or \r or
    > \n\r or some other such combination. Either way... I have never had
    > to do sendString = [NSString stringWithFormat:@"%@\r%c",aString,10];
    > I do not know what Char 10 is however I am assuming it is an EOL or
    > EOF character? Either way this seems to be unique to NSStream as
    > other tools I've used require only the \r. When using NSStream it
    > seems that for anything but http it requires both a \r and character
    > 10. (\n didn't work and neither did the character by itself.)
    > I discovered that with every server, the final character of each
    > welcome message was ascii character 10 preceded by character 13 so I
    > tried it and it worked.
    >
    > I suppose I'm hoping some one can explain this to me. Even though it
    > is working I do not understand why and would be grateful if some one
    > could clarify.
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today