我正在为此拉头发。我正在向服务器发送几条数据,所以我把它放在缓冲区中,然后发送缓冲区。一切似乎都很好,但是在服务器上,字符串在不同的点被切断,所以例如我得到“1234567890123\0\0\0...”而不是“12345678901234567890123456789012”。
所有整数都工作正常,并且可以正常传输。任何人都知道为什么整个消息不会出现。服务器正在从流中读取整个消息,没有未读取的字节,客户端发送 68,服务器获取 68 个字节。会不会是编码?
这是 iPhone 代码:
int command = 0;
int msgLen =0;
int bytesSent;
(void)memcpy(_data, &command, sizeof(int)); msgLen++;
(void)memcpy(_data+2, &playerID, sizeof(int)); msgLen++;
NSString *response = [NSString stringWithString:@"12345678901234567890123456789012"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSUnicodeStringEncoding]];
(void)memcpy(_data+3, [data bytes], [data length]);
msgLen += [data length];
(void)memcpy(_data+1, &msgLen, sizeof(int)); msgLen++;
bytesSent = [outputStream write:(const uint8_t *)_data maxLength:msgLen];
NSLog(@"sent %d of %d",bytesSent, msgLen);
这是C#代码
private void JoinServer(TcpClient client, byte[] buffer)
{
int offset = 0;
int playerid = BitConverter.ToInt32(buffer, offset);
offset += 4;
var playerkey = Encoding.Unicode.GetString(buffer, offset, buffer.Length-4);
System.Diagnostics.Debug.WriteLine(playerkey, "JoinPlayerReceived");
try
{
GetSubscriberByClient(client).PlayerId = playerid;
SendJoinServerStatus(playerid, playerid);
}
catch (Exception)
{
SendJoinServerStatus(playerid, 0);
}
}