考虑以下情况:(伪代码)
//All our luscious data
char theChar = 123;
int theInt = 4324;
char[] theCharArray = "sometext";
//Make an array to hold all of that data.
byte[] allTheVars = new *byte[sizeOfArray];
//Copy all vars into "allTheVars"
copyToEndOfArray(theChar, allTheVars);
copyToEndOfArray(theInt, allTheVars);
copyToEndOfArray(theCharArray, allTheVars);
所以这个想法是你最终会得到一堆变量串在一起成同一个字节数组。然后通过 Internet 传递该数组。现在说所有这些变量都被发送到调用远程函数,如下所示。
//This is the function that will take in the data we sent over the network.
void remotelyCalledInternetFunction(char aChar, int anInt, char[] aCharArray)
{
}
与其通过从字节数组中进行繁琐的复制来手动将每个变量拆分为指定的类型,您可以通过执行类似的操作来“自动拆分”变量吗?
//Pass the byte array. The method knows what types it needs, maybe it will auto-split the data correctly?
remotelyCalledInternetFunction(allTheVars);
如果没有,我能做类似的事情吗?
编辑:有什么办法可以做这样的事情吗?
remotelyCalledInternetFunction(allTheVars);
//Takes first 2 bytes of the array, the next 4 bytes, and the rest for the char[]?
void remotelyCalledInternetFunction(char aChar, int anInt, char[] aCharArray)
{
}