User Datagram Protocol or UDP is a simple protocol. This protocol is unreliable - when a data packet is sent, it cannot be not known if it will reach its destination or not. There is no acknowledgement, retransmission, or timeout. When multiple data is sent at once, the order which they arrive cannot be predicted. They might be out of order. The data that is sent with sequence 1,2,3 might be recieved as 2,3,1. So, sending files through UDP socket is a bit difficult.
Normally in a realtime game such as FPS, UDP protocol is used to send player location and behaviour. This data will be send continuously to provide a realtime player action to other player. But when the data is not arrived, the data will be skipped without retransmission so that the gameplay will be realtime, and not delayed. When sending files, a new protocol must be constructed as the file data must be sent completely. It cannot be skipped as it will corrupt the file.
About This Project
This is a simple project to demonstrate sending a file through UDP Socket. The code here might not perfect and may contains several flaws and bugs which might prevent the file from being sent perfectly.
Download Project file + Demo
UDP
To start this project, a simple UDP socket class must be constructed to assists sending and receiving data packet through UDP socket. This class will be named CSockUDP and will contain this method:
class CSockUDP
{
public:
static int InitWinsock();
static void DestroyWinsock();
void SetCallback(cbDataRecieved fnRecieved);
int CreateSocket(int nPort = 31313);
void StartListen();
void StopListen();
bool SetServerAddress(char* szServer, int nPort);
int SendData(LPVOID lpData, int nSize);
int ResendData();
}
InitWinsock() and DestroyWinsock() is a call to WinsockAPI to initiates and stop the use of Winsock Dll. It must be called on very early when the program is started and on the very end before the program terminated.
A call to CreateSocket() will create a new UDP socket and assign it with a port number. Then, SetCallback() is called to set receive event function. StartListen() will create a new thread which will executes a loop code and listen for any incoming data. Other method is self explanation.
----
To send files through UDP socket, firstly a protocol must be constructed. Every data packet that is sent must have some sort of acknowledgement and identification so the receiver can understand what is it and what should it do. We can start by creating a data header followed by the data itself.
struct TUDPHeader { char szID[3]; // 'IKH' - unique id DWORD dwCRC32; // Checksum of Data char type; // eDataType int nLength; // Data length // Data (TUDPFileHeader/TUDPFileReply/TUDPFileTransfer) };Inside this header, szID is an identification. It will contain unique data, "IKH" in this case and send it to the receiver. When the receiver read the data and found that the first 3 bytes is equal to "IKH", it will continue read the packet but when it different, the packet will simply be skipped as it might come from other software that is also sending a data to the same port.
dwCRC32 will contain a data checksum (not including TUDPHeader). The sender will calculate the data checksum and fill dwCRC32 with it. When it arrives at destination, the receiver will calculate the data checksum again and compare it with dwCRC32. If the checksum calculated by receiver differ from what is sent by sender, it will indicates that the data is corrupted or altered before its reach its destination. If this happen, the receiver will request that the data must be sent back again.
type explains to the receiver what must it do with the data.
enum eDataType { eDataTypeConnect, // ping eDataTypeConnected, // Reply if server got connect message eDataTypeResend, // Corrupted data, request for resend eDataTypeQuit, // Exit the program, client and server eDataTypeFileHeader, // TUDPFileHeader eDataTypeFileTransfer, // TUDPFileTransfer eDataTypeFileReplyAccept, // Accept the file send request eDataTypeFileReplyReject, // Denied the file send request eDataTypeFileReplyOK, // Proceed to next stream eDataTypeFileReplyFinish // Finished transfer };
Lastly inside this header (TUDPHeader), is nLength. nLength will tell how big is the data sent by sender. Without it, the receiver does not know when the data end.
----
When a file is being sent, a packet is constructed from TUDPHeader followed by TUDPFileHeader. It will send a file name followed by size of the file in bytes.
struct TUDPFileHeader { char szFileName[128]; int nFileSize; };
After that, receiver will reply either to accept the file or reject it like we can see in eDataType, eDataTypeFileReplyAccept/eDataTypeFileReplyReject. If the file is accepted, the receiver will create a new file and the sender will immediately send the file data using TUDPFileTransfer which contain file offset, data size and the file data itself.
struct TUDPFileTransfer { int nFileOffset; int nLength; char szData[2048]; };
The receiver then writes the newly created file with the data its received in TUDPFileTransfer until the file completed.
Example of sending data packet with TUDPHeader + TUDPFileHeader |
Excellent!!!
ReplyDeletePopular Risk Options
ReplyDeleteSbobet It is a great way to win online games. Providing users in the mobile games. Because it can play online gambling. Use the service through the website or download the application, depending on the preferences. The risky game is that any time it is popular. Because it is a game that is exciting and challenging for your. Online casino games are a great way to keep you entertained. Every game is ready to serve the customer to your fingertips. BETTING BETS Real-time online betting that will make you feel like playing from a live casino. Opportunities of gambling that bring wealth to the user. Every game is designed to meet all your needs. Games to play online at any time. Ready to serve Speed From the team that serves all gamblers like professionals. Better to gamble at online casino games that customers please. It is also an alternative to many people who use the service. Since the bets are easy and the money is suddenly high, every bet. คาสิโน
This is great written
ReplyDeleteสล็อต แตก ง่าย พนันออนไลน์ยอดนิยมยอดนิยมเยอะที่สุดในขณะนี้เนื่องด้วยเป็นเกมที่มีต้นแบบการเล่นที่ง่าย ไม่สลับซับซ้อน พีจีแตกต่างจากพนันอื่นๆที่มีข้อตกลง กรรมวิธีการเล่นที่ยุ่งยาก
ReplyDeletesuper slot เว็บสล็อตออนไลน์ มาแรงชั้นที่ 1 เว็บไซต์ พนัน ออนไลน์ ฟรี เครดิต ของประเทศไทย ในปี 2022 ควรต้องยก ให้เว็บ pg ความรู้ความเข้าใจของคนไทย สร้างเอง ไม่ผ่านคนกลาง
ReplyDeleteทดลอง เล่น สล็อต PG SLOT ทดลองเล่นฟรีไม่ต้องสมัครก็สามารถเล่นได้ไม่จำกัดวงเงินปั่นรัวๆได้ กับเว็บ สล็อตออนไลน์ PG-SLOT.GAME ที่นำเกมสล็อตจากค่าย PG SLOT มารวมไว้ในที่เดียว
ReplyDeleteโปรโมชั่น pg slot มากมาย เล่นง่ายจ่ายจริง แตกจริง ต้อง PG-สล็อต เท่านั้น! เล่นสล็อต พีจีสล็อต เว็บไซต์ตรงผู้ให้บริการเกมสล็อตออนไลน์ชั้นหนึ่ง ทกลอง เล่น ฟรี พร้อมโบนัส
ReplyDeleteBayburt
ReplyDeleteKars
Batman
Urfa
İzmir
7UQE
denizli evden eve nakliyat
ReplyDeletekars evden eve nakliyat
çorum evden eve nakliyat
kars evden eve nakliyat
malatya evden eve nakliyat
V0BA7
3B3B4
ReplyDeleteÇankırı Şehir İçi Nakliyat
Osmaniye Evden Eve Nakliyat
Hakkari Parça Eşya Taşıma
Tekirdağ Parça Eşya Taşıma
Ünye Çelik Kapı
Yalova Lojistik
Bingöl Lojistik
Ergo Coin Hangi Borsada
Ünye Oto Boya
57076
ReplyDeleteSilivri Cam Balkon
Bursa Evden Eve Nakliyat
Gümüşhane Evden Eve Nakliyat
turinabol
Ağrı Evden Eve Nakliyat
Tekirdağ Çatı Ustası
steroid cycles
order deca durabolin
clenbuterol for sale
5583F
ReplyDeleteSoundcloud Dinlenme Hilesi
Youtube İzlenme Satın Al
Binance Nasıl Üye Olunur
Görüntülü Sohbet Parasız
Threads Takipçi Hilesi
Twitter Takipçi Satın Al
Bitcoin Üretme
Mith Coin Hangi Borsada
Twitter Trend Topic Hilesi
77C5A185B8
ReplyDeletebeğeni satın al