Adam Libonatti-Roche
Adam Libonatti-Roche

Reputation: 321

Have been cursing over data packets for 36 hours now, please help!

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.

Upvotes: 0

Views: 852

Answers (2)

zeeawan
zeeawan

Reputation: 1

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

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;

Upvotes: 0

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];

Upvotes: 1

Related Questions