CodePudding user response:
If is the subcontract to send, the length of the first package should be the whole package of information, how to design this see you transfer protocolCodePudding user response:
Or a similar command line agreement, with a terminatorCodePudding user response:
The subcontract of the cases, you receive a byte [] how long how long it is.. The judge does not make sense..So you have to design your own communication protocol. The head and tail length function check these contain..
Rather than just from the "length" to lay hands on
CodePudding user response:
Why do you want to determine length? If you are afraid of packet loss, then you're writing the program contract can be defined, let's say you want to send the package as a string type, length is 25, then you can at the time of contract will increase field into a string in the front, 25 | STR, of which 25 is the length of the string, | is a delimiter, loosely defined, string STR is itself, then after receiving the data convert it into a string, separated by a delimiter into a [0], a [1], and then determine a [1]. The length is equal to an Integer. The Parse (a [0]), if you just want to know after receiving data to the length of the byte array, use length line ah, byte is not an array,CodePudding user response:
Socket. The Receive ();https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket.receive? Redirectedfrom=MSDN& View=netframework 4.8
The method's return value, is actually to receive the number of bytes,
CodePudding user response:
The pit of the TCP Socket is always fill up!Note: TCP Socket there is no definition of "how to receive the packet from the other", this kind of judgment must be solved by the business layer! Stick because TCP packet, the problem of the subcontract to a closed all the sending a package, simple method is in front of the packets sent with a head (such as 8 bytes), fill out the subsequent data length in header information, when you receive package charge baotou first (until enough 8 bytes), calculate the length of follow-up, recycling, has received the number of bytes to baotou defined in length, in order to be finished,