已经被数据包诅咒了36小时了,请帮忙!

发布于 2024-08-05 05:33:37 字数 2323 浏览 4 评论 0原文

好的,这就是问题所在。我正在尝试通过苹果蓝牙发送网络数据包代码通过结构或任何内容发送。要发送的代码如下

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

}

如果有人可以梳理一下并解释发生了什么,我们将不胜感激。接下来是接收代码。

- (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);

作为目标c的新手,我正在努力理解这

一切是如何工作的,我如何访问我发送的字符串,因为记录它的所有努力都会导致程序崩溃。

以下是用于启动发送的代码:

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

任何帮助将不胜感激。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-08-12 05:33:37

您确实需要将 NSString 对象更改为要发送的 UTF8 表示形式,或者将其存档为以全长发送的 NSData 对象 - 您不能只是将 NSString 转换为 C 字符串(无论是用于发送还是接收! !!)。

您可以使用以下方法获取 NSString 的 C 字符串版本:

const char *sendString = [bigWord cStringUsingEncoding:NSUTF8StringEncoding]

然后使用以下命令将其从传入的 NSData 对象中取出

NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

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];
傾城如夢未必闌珊 2024-08-12 05:33:37

这家伙通过网络发送和接收字符串一切正常。但是,我在接收端仍然遇到错误。

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

终于明白了。 GKTank 使用结构传输数据,所以我将字符串放在结构中,其余的就是历史记录。

typedef struct {
    NSString *strInfo;
} myInfo;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文