简体   繁体   English

iOS NSInputStream

[英]IOS NSInputStream

I got a problem when using NSInputStream. 使用NSInputStream时出现问题。 I have client app which connect to a server then server will start to send message to my client app through TCP repeatedly about 1 message per second. 我有连接到服务器的客户端应用程序,然后服务器将开始以每秒大约1条消息的方式反复通过TCP向我的客户端应用程序发送消息。 Server is just broadcasting message to client and message is xml format. 服务器只是向客户端广播消息,消息是xml格式。 The server send a message as one packet. 服务器将消息作为一个数据包发送。

Now the problem is that when I read byte from NSInputStream the data got truncated which mean instead of receive 1 complete message, I got 2 separate data(partial xml) respond from time to time. 现在的问题是,当我从NSInputStream读取字节时,数据被截断了,这意味着不是收到1条完整消息,而是不时收到2条单独的数据(部分xml)。 I am not able to debug because it already happen when I read data byte from NSInputStream. 我无法调试,因为当我从NSInputStream读取数据字节时已经发生了。

I use Wireshark to analyse every packet I receive and when it happen data got truncated too, because TCP so partial data retransmit to my client. 我使用Wireshark来分析我收到的每个数据包,并在发生数据时也将其截断,因为TCP会将部分数据重新传输到我的客户端。 I have tried to log every partial data byte, the sum of partial data always around 1600 byte. 我尝试记录每个部分数据字节,部分数据的总和始终在1600字节左右。

I have no idea how did they design and implement server side, but I do know there are many of people connect to that server and continuous get broadcasting message from it. 我不知道他们是如何设计和实现服务器端的,但是我确实知道有很多人连接到该服务器并不断从该服务器获取广播消息。

Does anyone encounter this problem? 有人遇到这个问题吗? Can anyone help? 有人可以帮忙吗? Is it possible that data is over the max size and get splited? 数据是否可能超过最大大小并被拆分?

This is not a problem per se. 这本身不是问题。 It is part of the design of TCP and also of NSInputStream . 它是TCP和NSInputStream设计的一部分。 You may receive partial messages. 您可能会收到部分消息。 It's your job to deal with that fact, wait until you receive a full message, and then process the completed message. 处理这个事实是您的工作,等待直到收到完整的消息,然后处理完成的消息。

1600 bytes is a little strange. 1600字节有点奇怪。 I would expect 1500 bytes, since that's the largest legal Ethernet packet (or especially somewhere around 1472, which is a really common MTU, minus some for the headers). 我希望有1500个字节,因为这是最大的合法以太网数据包(或者尤其是1472左右的某个地方,这是一个非常常见的MTU,减去一些报头)。 Or I might expect a multiple of 1k or 4k due to buffering in NSInputStream . 或者由于NSInputStream缓冲,我可能期望1k或4k的倍数。 But none of that matters. 但这无关紧要。 You have to deal with the fact that you will not get messages necessarily at their boundaries. 您必须处理这样一个事实,即您不一定会在其边界处收到消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM