Thread: Trouble printing telnet "prompts"

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    54

    Trouble printing telnet "prompts"

    I'm having a strange problem with telnet prompt lines not printing in this program I've written. The data from the server are received and seem to be in the output buffer, but they won't print unless a newline is appended or something else is printed afterwards.

    Program output:
    Code:
    DEBUG - RAW: 57 65 6c 63 6f 6d 65 20 74 6f 20 4d 69 63 72 6f 73 6f 66 74 20 54 65 6c 6e 65 74 20 53 65 72 76 69 63 65 20
     d a
    Welcome to Microsoft Telnet Service
    
    DEBUG - RAW: a d 6c 6f 67 69 6e 3a 20
    The last data received reads "\n\rlogin: ", and it's what's not printing out. I'm wondering if it has something to do with the oddly ordered \n \r pair? I've tried parsing out the carriage returns just in case the problem had something to do with that, but to no effect. On the off chance that it's relevant, I'm compiling and running the program in Cygwin.

    Printing functions:
    Code:
    int Telnet::read_data()
    {
    	memset(szRaw, 0, sizeof(char)*bufsize);
    	int cLen = recv(hSock, szRaw, bufsize, 0);
    	if(cLen == -1)
    	{
    		char msg[512];
    		sprintf(msg, "Recv error: %s\nMsg: %s\n", strerror( errno ), szRaw);
    		throw TelnetError(msg);
    	}
    	else if(cLen == 0)
    		throw TelnetError("Connection closed.");
    	
    	#ifdef debug
    	printf("\nDEBUG - RAW: ");
    	for(int i=0;i<cLen;i++)
    		printf("%x ", szRaw[i]);
    	printf("\n");
    	#endif
    	
    	process_data(cLen);
    	printf("%s", szProcessed);
    	return cLen;
    }
    
    void Telnet::process_data(size_t cLen)
    {
    	char *pB = szProcessed;
    	memset(szProcessed, 0, sizeof(char)* bufsize);
    	unsigned char cmd = 0, opt = 0;
    	for(int i = 0; i < cLen; i++)
    	{
    		if(szRaw[i]!=IAC)
    		{
    			*pB++=szRaw[i];
    			continue;
    		}	
    		i++;
    		cmd = szRaw[i];
    
    		switch(cmd)
    		{
    			case WILL:
    			case WONT:
    			case DO:
    			case DONT:
    				i++;
    				opt = szRaw[i];
    				/* Call callback function associated with command*/
    				CallCbf(cmd, opt); 
    				break;
    			case SB:
    				i++;
    				opt = szRaw[i];
    				//CallCbf(cmd, opt);
    				while(szRaw[i]!=SE) i++;
    				break;
    			default:
    				CallCbf(cmd, 0);
    				break;
    		}
    	
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    \n\r is Windows EOF (CR-LF). It may be a bit obnoxious on your end, but shouldn't cause any harm.

    As to the rest, that's how printf is supposed to work. It doesn't print a line until the line is over, iwth a \n at the end. If that's not what you want, you either shouldn't be using printf or you need to change the buffering on stdout.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    Changing the printf to:
    std::cout<<szProcessed;
    std::cout.flush();
    worked marvelously. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble printing an array of chars
    By ppata in forum C Programming
    Replies: 8
    Last Post: 11-11-2010, 09:39 PM
  2. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 06:29 PM
  3. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  4. trouble printing
    By dPmunky in forum C Programming
    Replies: 6
    Last Post: 11-18-2002, 02:43 PM
  5. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 08:04 PM