Transferring a file through TCP in GCDAsyncSocket

I am currently writing a game in which I intend to transfer a .caf sound file from the iPhone to my C++ server. Right now I am getting an EXC_BAD_ACCESS when I message the sendGameUpdateWithFile function, and I really have no idea why.

Although this is not related to any uni assignments, please keep in mind that I am a CS student and not (yet) a professional network programmer, so judge my code thereafter.

Here's the code I use to transmit the data to the server (which crashes right now):

- (void) sendGameUpdateWithFile:(NSString*)filePath gameID:(NSInteger)gameID {
    NSMutableData* data = [[NSMutableData alloc] init];
    data = [NSMutableData dataWithContentsOfFile:filePath];

    fileheadPacket head;

    head.msgtype = 0x12;
    strncpy(head.data1, [myUsername cStringUsingEncoding:NSASCIIStringEncoding], [myUsername length]);
    int followingPackets = (([data length] % 1024 == 0) || ([data length] < 1024))? ([data length]/1024) : ([data length]/1024)+1;
    head.following = followingPackets;
    head.fileid = gameID;
    head.size = sizeof(packet);

    [mySock writeData:[NSData dataWithBytes:&head length:sizeof(packet)] withTimeout:-1 tag:7];

    filePacket sendPackets[followingPackets];

    for(int i = 0; i < followingPackets; i++){
        NSRange thisRange;
        thisRange.location = i*1024;
        thisRange.length = (i+1)*1024;

        filePacket tmp;
        tmp.msgtype = 0x13;
        tmp.size = sizeof(filePacket);
        memset(tmp.data1, 0, sizeof(tmp.data1));
        [data getBytes:tmp.fileBuffer range:thisRange];

        strncpy((char*)&sendPackets[i], (char*)&tmp, sizeof(tmp));
    }

    for(int i = 0; i < followingPackets; i++){
        [mySock writeData:[NSData dataWithBytes:&sendPackets[i] length:sizeof(filePacket)] withTimeout:-1 tag:3];
    }
}

The structs I use for data look like this:

typedef struct file_packet {
    int msgtype:8;
    int size:16;
    int nul:8;
    int following:24;
    int emp:8;
    char data1[64];
    char fileBuffer[1024];
} filePacket;

typedef struct filehead_packet {
    int msgtype:8;
    int size:16;
    int nul:8;
    int following:24;
    int emp:8;
    char data1[64];
    int fileid;
    char rest[60];
} fileheadPacket;

The server expects the given msgtypes -- the problem lies on the client side.

Upvotes: 0

Views: 455

Answers (0)

Related Questions