Home > other >  Sending hundreds of KB or several megabytes of data, is written in TCP is good, still use UDP custom
Sending hundreds of KB or several megabytes of data, is written in TCP is good, still use UDP custom

Time:04-28

Myself small white, the problems in the study, the current environment is local area net communication, between the client in a few simple words, smaller pictures, using UDP send and receive all no problem, but when the data over UDP MTU (maximum transmission unit), then remove the IP, UDP datagram first, is the last 1472 bytes are available,
1472 bytes to transfer a larger image or audio file is not enough, so I thought of the two methods to deal with,

1. UDP send before will send data size, custom packets, each packet fixed size, can calculate how many packets are needed to complete data to send, label to each packet, send in the form of structure to send, the receiver receive, according to the label ordering again, (check information said UDP subcontract transfer does not guarantee order) ,
Struct MessageType
{
Int total;//the total number of packets
Int number;//packet label
int size;//data total size
QByteArray data;//the data I used here is Qt
};

Data size 2. Is the use of TCP, judgment, when the data is small (UDP can one-time send), or the use of UDP, when the data is large, open threads, UDP messages, (attached), waiting for the receiving end of feedback, the receiver to receive after open threads, TCP listener port, after the success of the listener, send a confirmation message, the sender to connect to the receiver, after receipt of the message to send data, when sent, release the connection,

is now also don't know with what method to deal with is better, the above methods sure there are more or less problems, also want to give that says which method is better, or have other better solution, also hope to glad,

CodePudding user response:

Now is I do not know with what method to deal with is better, the above methods sure there are more or less problems, also want to give that says which method is better, or have other better solution, also hope to glad,


UP UP UP
  • Related