views:

340

answers:

4

Ok Here is the problem. I am trying to send through a structure or anything through the apple bluetooth send network packet code. The code to send is below

-(void)sendMessagePacket:(GKSession *)session packetID:(int)packetID withData:(void *)data ofLength:(int)length reliable:(BOOL)howtosend {

static unsigned char networkPacket[maxPacketSize];
const unsigned int packetHeaderSize = 2 * sizeof(int); // we have two "ints" for our header

if(length < (maxPacketSize - packetHeaderSize)) { // our networkPacket buffer size minus the size of the header info
 int *pIntData = (int *)&networkPacket[0];
 // header info
 pIntData[0] = 10;
 pIntData[1] = packetID;

 // copy data in after the header
 memcpy( &networkPacket[packetHeaderSize], data, length ); 

 NSData *packet = [NSData dataWithBytes: networkPacket length: (length+8)];

 if(howtosend == YES) { 
  [session sendDataToAllPeers:packet withDataMode:GKSendDataReliable error:nil];
 } else {
  [session sendDataToAllPeers:packet withDataMode:GKSendDataUnreliable error:nil];
 }
}

}

If anyone could go through this with a comb and explain what is going on that would be greatly appreciated. Next, the receive code.

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context {
// Caller whenever data is received from the session
unsigned char *incomingPacket = (unsigned char *)[data bytes]; 

//EXPECTS THAT WHAT IS IN INCOMING PACKET [0] TO BE OF DATA TYPE INTEGER
int *pIntData = (int *)&incomingPacket[0];

int packetTime = pIntData[0];

int packetID = pIntData[1];

static int lastPacketTime = -1;

 switch (packetID) {
 case NETWORK_COINTOSS:
  //DO SOMETHING
  break;

  case NETWORK_TEXT_EVENT:

   NSString *fewMore = (NSString *)&incomingPacket[8];

    fewThings *bitMore = &fewMore[peer];

   //NSLog(@"Trace Bit More: %@",fewMore);
   break;
 default:
  break;
}


NSInteger lengthBytes = [data length];
NSLog(@"Length of data : %i", lengthBytes);

}

What i am struggling to understnad being a newcome to objective c is how all this works, how do i access the string that i have send as all efforts to log it cause the program to crash.

Below is the code used to start the send:

    NSString *bigWord = @"BigWordBigWord";
NSInteger len = [bigWord length];
[self sendMessagePacket:gameSession  packetID:NETWORK_TEXT_EVENT withData:bigWord ofLength:(len) reliable:YES];

Any help would be so appreciated. Thank You.

+1  A: 

You really need to change the NSString object into either a UTF8 representation to send, or to archive it as an NSData object you send with the full length - you can't just cast an NSString as a C string (either for sending or receiving!!!).

You can get a C string version of NSString using:

const char *sendString = [bigWord cStringUsingEncoding:NSUTF8StringEncoding]

And then get it out of your incoming NSData object with

NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Kendall Helmstetter Gelner
A: 

This guy is sending and receiving the string over network just fine. But, I'm also still getting error on the receiving end.

http://discussions.apple.com/thread.jspa?messageID=10215848

zeeawan
A: 

I'm experiencing the same issue. The following code works just fine if placed inside a single function. But real trouble starts when I do the same over the network. //Data to be sent part works just fine for sending data, but on the receiving end //data received is throwing bad excess for NSLog(@"receive string %@", strReceived);

It is odd, it should work in receiveData method of GKTank application.

    //data to be sent
NSString *strToBeTransfered = @"abcdefefghijklmnopqurstuvwxyz";
NSData* nsdataToBeTransfered = [strToBeTransfered dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger len = [nsdataToBeTransfered length];
static unsigned char networkPacketToBeTransfered[1024*3];
const unsigned int packetHeaderSize = 2 * sizeof(int); 
int *pIntData = (int *)&networkPacketToBeTransfered[0];
// header info
pIntData[0] = 0;
pIntData[1] = 1;
// copy data in after the header
memcpy( &networkPacketToBeTransfered[packetHeaderSize], nsdataToBeTransfered, len );
//
//data received
NSData *nsdataReceived = [NSData dataWithBytes: networkPacketToBeTransfered length: (len+8)];
unsigned char *incomingPacket = (unsigned char *)[nsdataReceived bytes];
int *pIntData1 = (int *)&incomingPacket[0];
int packetTime = pIntData1[0];
int packetID = pIntData1[1];
NSString *strReceived = (NSString *)&incomingPacket[8];
zeeawan
A: 

At last got it. GKTank is transferring the data using struct, so I placed my string inside the struct and rest is the history.

typedef struct { NSString *strInfo; } myInfo;

zeeawan