#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    573
    Rep Power
    70

    Only receiving max of 1448 bytes on socket reads


    Hi

    I'm writing a native MySQL connection class in Objective C and have it working nicely so long as the App using the class and the MySQL server are on the same machine. If the MySQL database is on a remote server, when I read data from the BSD socket file descriptor, it maxes out at 1448 bytes. Queries whose results are smaller than 1448 bytes execute fine, but above that, and I don't know how to retreive all the expected data. Here's how I'm reading the socket

    Code:
    - (NSData *) reply
    {
    	NSMutableData	*result		= [NSMutableData data];
    	int		bytesRead	= readBufferLength;
    	
    	while (bytesRead == readBufferLength)
    	{
    		bytesRead = read(socketFD, readBuffer, readBufferLength);
    		
    		NSLog(@"bytesRead: %i", bytesRead);
    		
    		// append buffer's data to result
    		if (bytesRead > 0)
    			[result appendBytes: readBuffer length: bytesRead];
    		else
    		{
    			// some sort of error occurred, log it, 
    			// release result and break out of the loop
    			NSLog(@"error: %@", [MySQLSocket socketError]);
    			
    			[result release];
    			result	= nil;
    			
    			break;
    		}
    	}
    	
    	return result;
    }
    Anyone know how I can read more than 1448 bytes from the file descriptor? Is there some system variable I need to set? BTW, "readBuffer" above is 1MB so there's no question of it holding most queries in one shot.

    Any help greatly appreciated.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Your logic appears flawed. What is readBufferLength? A constant defined somewhere?
    Code:
    while (bytesRead == readBufferLength) {
           bytesRead = read(socketFD, readBuffer, readBufferLength);
    One of the things you have to understand about the read() call is that it returns the number of bytes actually read. You may request readBufferLength bytes to be read in, but the function is allowed to return any number of bytes from 0 up to readBufferLength bytes. Consider that you request 1000 bytes, but it only has 500 bytes to return to you because that's what the query returned. Or, your query may have 1000 bytes in the result set, but the network stack chooses to return the data to you in two 500 byte packets. Hence, your while loop logic is wrong. It should be:
    Code:
    while (bytesRead > 0) {
     ...
    }
    Last edited by Scorpions4ever; February 7th, 2011 at 11:18 AM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    45
    Rep Power
    11
    Originally Posted by ktoz
    Hi

    I'm writing a native MySQL connection class in Objective C and have it working nicely so long as the App using the class and the MySQL server are on the same machine. If the MySQL database is on a remote server, when I read data from the BSD socket file descriptor, it maxes out at 1448 bytes. Queries whose results are smaller than 1448 bytes execute fine, but above that, and I don't know how to retreive all the expected data. Here's how I'm reading the socket

    Anyone know how I can read more than 1448 bytes from the file descriptor? Is there some system variable I need to set? BTW, "readBuffer" above is 1MB so there's no question of it holding most queries in one shot.

    Any help greatly appreciated.
    Hello I'm a network guy so your question about these 1448 bytes shocked me very quickly.
    1448 = 1500 - 20 (ip) - 20 (tcp) - 12 (???)
    Headers of ip and tcp are 20 bytes long, the 12 other bytes might be made of mysql header (I read that it's 4 bytes) and 8 bytes for something else

    What it means is that you might have 1448 bytes of data in each networking packet.
    When you say "on the same machine" I think you use the loopback interface (127.0.0.1) to send your data. On my system its mtu is 33200, so there are no problems for these 1500 bytes. But what if you tried to use the ip address of your network card ? I wouldn't be surprised if you had the same behavior that you saw when dealing with a remote machine.

IMN logo majestic logo threadwatch logo seochat tools logo