2012-01-22 58 views
0

我正在做我的学习东西,所以是的,这是作业转换uint32_t到网络字节顺序

第一:我得到了一个可以工作的Java服务器应用程序,我也有这个客户端应用程序,我现在用java编写c程序。 很简单,我无法将uint32_t发送到我的java服务器。

因此,让我们炫耀一些代码:

char* callString(char func, char* str, uint32_t anz) { 

    uint32_t packageLength, funcLength, strLength; 
    funcLength = htonl(sizeof(func)); 
    strLength = htonl(strlen(str)); 
    uint32_t byte = htonl(4); 
    uint32_t trailing = htonl(1); 
    anz = htonl(anz); 

    packageLength = byte + byte + funcLength + byte + strLength + byte + byte + trailing; 
    /*packageLength = htonl(packageLength);*/ 

    char byteBuffer[ntohl(packageLength)]; 


    printf("%i\n", packageLength); 
    printf("%i\n", htonl(packageLength)); 
    printf("%i\n", ntohl(packageLength)); 

    sprintf(byteBuffer, "%i%i%c%i%s%i%i%c", packageLength, byte, func, byte, str, byte, anz, '\0'); 

    printf("%s\n", byteBuffer); 

    if(write(sock, byteBuffer, packageLength) < 0) { 
    printf("write: Konnte keine Daten zum gegenüber senden.\n"); 
    exit(EXIT_FAILURE); 
    } 
    char* retVal = "hallo"; 
    return retVal; 
} 

简单的是,我打电话与FUNC =“V”这个功能,海峡=“你好”和ANZ = 2 ,这将给我的packageLength 27个字节。

包是建立这样的:
= packageLength(INT),它是4字节
+长度函数描述符(funcLength),它是4字节
+ FUNC描述符(FUNC),其是funcLength
的+长度参数1的(strLength),它是4字节
+长度的参数1(STR)的值,它是strLength
+长度param2的也就是4字节
+长度的参数2的值(ANZ),它是4字节
的+ NULL字节(\ 0)这是1个字节

我假设我做的转换是错误的也许我使用了错误的数据类型。在服务器端,我使用Java ByteBuffer来收集数据。起初,我从网络上读取的4字节packagelength这会给我的,我有多少时间阅读,直到我得到整个数据包的信息:

byte[] msgLength = new byte[4]; 
try { 
handle.getInputStream().read(msgLength); 
} catch (IOException ex) { 
    System.out.println("could not receive byte stream: msgLength"); 
    break; 
} 
ByteBuffer receive; 

receive = ByteBuffer.wrap(msgLength); 

int packageLength = receive.getInt(); 
System.out.println("packageLength" + packageLength); 

最后的println会给我下面的输出:

packageLength875901497

因此,没有人现在在哪里我的问题是什么?如有必要,我可以为您提供更多代码,但我认为错误是数据类型(uint32_t)或转换。

帮助被appriciated。提前致谢。

干杯 丹尼尔

回答

2
funcLength = htonl(sizeof(func)); 
strLength = htonl(strlen(str)); 
uint32_t byte = htonl(4); 
uint32_t trailing = htonl(1); 
anz = htonl(anz); 

packageLength = byte + byte + funcLength + byte + strLength + byte + byte + trailing; 

你将大小网络秩序和然后相加,然后为packageLength。您应该将它们加在一起之前将它们转换为网络订单与htonl

除此之外,这一切:

sprintf(byteBuffer, "%i%i%c%i%s%i%i%c", packageLength, byte, func, byte, str, byte, anz, '\0'); 

是错误的,因为我认为你要送他们为二进制,但使用上述sprintf就送他们在基地10 ASCII表示(即你”重新发送数字作为文本!)

+0

这是一些试验和错误的作品,我做了。但我再次改变它,我得到这样的 uint32_t packageLength, funcLength, strLength; funcLength = sizeof(func); strLength = strlen(str); anz = htonl(anz); packageLength = 4 + 4 + funcLength + 4 + strLength + 4 + 4 + 1; /*packageLength = htonl(packageLength);*/ strLength = htonl(strLength); funcLength = htonl(funcLength); uint32_t byte = htonl(4); uint32_t trailing = htonl(1); char byteBuffer[packageLength]; packageLength = htonl(packageLength); Daniel

+0

在Java中我有一个ByteBuffer是我把数据,它给了我的方法.putInt().put()把东西放在一个字节缓冲区,然后发送ByteBuffer 。我不知道如何将数据打包到字符数组中。 – Daniel

+0

尽管如此,我在服务器端使用更改后的代码(请参见上文)获取相同的packageLength:packageLength875901497如何告诉c将二进制数据写入套接字? – Daniel

相关问题