Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
How can I check to make sure the packet is actually getting to the server?

Interarchy has a Traffic-watching feature, that lets you see all (or some) packets going in or out of the host.
http://nolobe.com/interarchy/

You could run it on the client or on the server (or both).

There are doubtless other tools capable of watching network traffic, which a little googling will probably turn up. The canonical command-line tool is tcpdump (see: man tcpdump).
 
I've gotten it to receive messages, though it's a bit strange...
On the client side I have this:
Code:
case NSStreamEventHasSpaceAvailable:
			
			event = @"NSStreamEventHasSpaceAvailable";
			if (theStream == oStream)
			{
				/**
				//send data
				uint8_t buffer[11] = "I send this";				
				int len;
				
				len = [oStream write:buffer maxLength:sizeof(buffer)];
				if (len > 0)
				{
					NSLog(@"Command send");
					[oStream close];
				}
				 **/
				/**
				NSMutableData *data1;
				NSString *myString = @"string for data1";
				const char *utfMyString = [myString UTF8String];
				
				// initialize data1, data2, and secondBuffer... 
				NSMutableData* _data = [NSMutableData dataWithBytes:utfMyString length:strlen(utfMyString)+1];
				
				
				uint8_t byteIndex;
				uint8_t *readBytes = (uint8_t *)[_data mutableBytes];
				readBytes += byteIndex; // instance variable to move pointer
				int data_len = [_data length];
				unsigned int len = ((data_len - byteIndex >= 1024) ?
									1024 : (data_len-byteIndex));
				uint8_t buf[len];
				(void)memcpy(buf, readBytes, len);
				len = [oStream write:(const uint8_t *)buf maxLength:len];
				byteIndex += len;
				**/
				
				/**
				NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
				NSInteger x = [oStream write:[data bytes] maxLength:[data length]];
				 **/
				NSString* test = @"Hello";
				[test retain];
				NSData *data = [test dataUsingEncoding: NSASCIIStringEncoding];
				// Convert from host to network endianness
				uint32_t length = (uint32_t)htonl([data length]);
				// Don't forget to check the return value of 'write'
				[oStream write:(uint8_t *)&length maxLength:4];
				[oStream write:[data bytes] maxLength:length];
				[oStream close];
			}
			 
			
            break;

On server side:
Code:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
	NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	[self logMessage:aStr];
	/**
	aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	
	[self logMessage:aStr];
	
	NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
NSString *msg = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
	if(msg)
	{
		[self logMessage:msg];
	}
	else
	{
		[self logError:@"Error converting received data into UTF-8 String"];
	
	}
	 **/
	
	// Even if we were unable to write the incoming data to the log,
	// we're still going to echo it back to the client.
	//[sock writeData:data withTimeout:-1 tag:ECHO_MSG];
}

Now it does post my message up when received but it also adds a bit extra. (Picture of it below)
Any ideas of why it'd do such a thing?


Thanks,
-Gan
 

Attachments

  • Screen shot 2010-02-25 at 4.00.18 PM.png
    Screen shot 2010-02-25 at 4.00.18 PM.png
    106.4 KB · Views: 69
I would suspect that a buffer is assigned for reading that is bigger than the incoming data. Terminate your message with the correct string terminator (\0 : this is basic C!) and it should work.

To pinpoint the issue. This:

Code:
uint8_t buffer[11] = "I send this";

is not a valid string (in C which is the level we are at now). This is:

Code:
uint8_t buffer[12] = "I send this\0";

I would suggest you brush up on your C if you are going to use this level of API.
 
Code:
NSString* bufferText = @"Hello\r\n";
				const uint8_t *buffer = 
				(uint8_t *) [bufferText cStringUsingEncoding:NSUTF8StringEncoding];
				int len;
				
				len = [oStream write:buffer maxLength:sizeof(buffer)];
				if (len > 0)
				{
					NSLog(@"Command send");
					[oStream close];
				}
Hey, this appears to work perfectly though can anyone tell me why we need the \r\n at the end for it to work? Also am I suppose to close it for every command sent and reopen when I want to send another command?


-Gan
 
Oh.
Now that this works:
Code:
uint8_t buffer[9] = "Hello\r\n";

int len;
				
len = [oStream write:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
	NSLog(@"Command sent");
	[oStream close];
}
How do I make it convert from an NSString to the buffer array? Here's some code to show what I'm aiming for but it doesn't work:
Code:
NSString* test = @"Hello\r\n";
uint8_t buffer[test.length] = [test cStringUsingEncoding:NSUTF8StringEncoding];
				
int len;
				
len = [oStream write:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
	NSLog(@"Command sent");
	[oStream close];
}
It pops up the error: "Variable Sized object may not be initialized"
Any solutions?

Thanks,
-Gan
 
To pinpoint the issue. This:

Code:
uint8_t buffer[11] = "I send this";

is not a valid string (in C which is the level we are at now). This is:

Code:
uint8_t buffer[12] = "I send this\0";

I would suggest you brush up on your C if you are going to use this level of API.

Better still:
Code:
#include <stdio.h>
#include <string.h>

int main (int argc, const char * argv[]) 
{
	char buffer[] = "I send this";

	printf( " .. len = %d, sizeof = %d \n", strlen( buffer ), sizeof(buffer) );
}

Output:
Code:
 .. len = 11, sizeof = 12

Also try:
Code:
	char buffer[] = "I send this\0";
 
It pops up the error: "Variable Sized object may not be initialized"
Any solutions?

The non-immediate solution is for you to learn more C.

The immediate solution is for you to read the NSString reference docs, looking for methods that take buffer pointers as parameters, possibly with a buffer length param, and that copy the contents of said NSString into said buffer using some encoding (possibly provided by a parameter, possibly implied by the method name).
 
Hmmm, I looked through the docs and haven't found anything useful.
Only need a way to convert an NSString to a uint8_t array.
Could I initialize the array with the correct character length then use a for loop to feed chars from the NSString into the uint8_t array one at a time?(If so, how)

Thanks,
-Gan
 
Hmmm, I looked through the docs and haven't found anything useful.
Only need a way to convert an NSString to a uint8_t array.

What actual type is uint8_t? What does it represent in this case?

What NSString methods are there that work with that representation in a type of that size?

Which NSString methods work with pointers to types of that size?

You really need to understand the fundamentals here, not just search the NSString reference doc for uint8_t.

It may be that the immediate solution is for you to learn more C, or at least more about C types and typedefs.
 
Got it:
Code:
NSString* test =[NSString stringWithFormat:@"Hello%d\r\n", rand()];
uint8_t buffer[test.length];
for (int i = 0; i< [test length]; i++) {
	unichar charAscii = [test characterAtIndex:i];
	buffer[i] = charAscii;
}
				
int len;
				
len = [oStream write:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
	NSLog(@"Command sent");
	//[oStream close];
}

Though for some odd reason, even though this keeps being called it doesn't continue to send the data. Only sends it once. How could I change this so I can send data whenever I want to?


-Gan
 
Ahah! Got it to work. Turns out that in the Async client for some strange reason a piece of code was commented out. I uncommented it and it all works. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.