Thread: coping char into a string

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Question coping char into a string

    Good day

    I have a program that opens a telnet session then sends and receives information from the telnet pipe. Most of the program works but I can't seem to capture the information from the telnet pipe. I can print it ok but can't get the information to the rest of the program. Here's the
    code:

    Code:
    int ReceiveUntil(char *buf, int bufsize, const char *until, int timeout)
    {
            int n = 0;
            int ai;
            char c;
    
            if (until != NULL && timeout > 0)
            {
                    alarm(timeout);
                    alarmed = 0;
                    ai = 0;
                    while (!alarmed)
                    {
                            if (read(fdrd, &c, 1) < 0)
                            {
                                    if (errno == EINTR) continue;
                                    if (errno == EAGAIN) continue;
                                    syslog(LOG_INFO,
                                       "Unspecified error while waiting for incoming data.");
                                    break;
                            }
    // Uncomment the next line to see traffic
    cout << c ;
    
                            if (until[ai] == c) ai++;
    
                            else
                            {
                                    for (int i = 0; i < ai; i++)
                                    {
                                            if (n < bufsize)
                                                     buf[n++] = until[i];
                                    }
                                    ai = 0;
    
                                    if (n < bufsize) buf[n++] = c;
                                    if (n >= bufsize) break;
                            }
                            if (until[ai] == '\0') break;
                    }
                    if (alarmed)
                            syslog(LOG_INFO, "Timeout while waiting for incoming data.");
                    unalarm();
            }
            return n;
    }

    The timeout works, cout << c ; works but writing c to buf doesn't
    seem to. Can anyone see my mistake. I am using GCC 3.3.3 and have tried with gcc 2.96 with the same result. I am using RH 9 and RH 7.3.

    Thanks inadvance

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I'm not sure, but i think this is what you're looking for:

    Code:
    int ReceiveUntil(char *buf, int bufsize, const char *until, int timeout)
    {
    	int n = 0;
    	int ai;
    	char c;
    
    	if (until != NULL && timeout > 0)
    	{
    		alarm(timeout);
    		alarmed = 0;
    		ai = 0;
    		while (!alarmed)
    		{
    			if (read(fdrd, &c, 1) < 0)
    			{
    				if (errno == EINTR) continue;
    				if (errno == EAGAIN) continue;
    				syslog(LOG_INFO, "Unspecified error while waiting for incoming data.");
    				break;
    			}
    
    			n=strlen(buf);
    			if(n<bufsize)
    				sprintf(buf+n, "%c", c);
    			else
    				break;
    // Uncomment the next line to see buffer contents
    cout << buf ;
    		}
    		if (alarmed)
    			syslog(LOG_INFO, "Timeout while waiting for incoming data.");
    		unalarm();
    	}
    	return n;
    }

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    the string in until is never appended to your buffer, thus if all you read exactly equals until you will never write anything you your buffer. You could also be reading a binary zero '\0' even though aditional results may be apended, you might not see them. Finally your string matching algorithm is flawed, consider ReciveUntill(buf,sizeof(buf),"aab",3000)and I send you "aaab.........." The first two characters match but the third resets ai to zero, now the fourth character that is a 'b' proceded by two 'a's is checked against the first character, a mismatch and we are off to infinity. You also do not check for the possibility of read returning 0, this can happen, particularly with sockets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM