Article Title: UDP server design process summary of the original author: Li Liang Gang's original: Vczx.com Release: Li Liang Gang Release Type: Original release Date: 2004-09-01 Today, Today, Total View: 2294 Download this article
Screenshot of the program running effect: VCZX_UDP UDP server design process summary. Recently, a video transmission project is taken into account, and UDP is the main network communication technology. Because of management of multiple clients. It is necessary to simulate a plurality of client connections to verify via UDP. Design selection: In order to improve reliability and stability, I use the asynchronous overlap mode of the WINSOCK API of the event model. Only, there is no choice but to finish, because our client is not a lot, 200 is enough. This 200 is because I have to send an image data of about 1-4K per second to 200 clients. If more, it is impossible to complete. Message mode is based on messages, reliability and efficiency have no event model. Winsock1.1 does not consider. It is set to Winsock 2.2, but fortunately, even Win98 comes with Winsock 2.2. Why don't you use multicast? First, some networks don't support multicast, the other is that multicast is more troublesome, and it will be considered in this regard. Design Idea: Create a socket. And listen to the event. Start the thread to receive data. Use a dimgetclist Coblist to save all the customers in the same, and notify the join success. This customer has a chance to handle this incident and do some actions. When the customer is disconnected, send an event to the server so that the server can also do some things, of course, some of this should happen. The key location is the transmission and reception section, and the data processing section. The transmission and reception section should be used to use the advantages of overlapping mode, that is, efficient and no CPU.
Hair following code: BOOL CUdpSock :: SendBuffer (char * buff, DWORD dwBufSize, struct sockaddr FAR * lpTo) {m_lock.Lock (); WSABUF wsabuf; WSAOVERLAPPED over; DWORD dwRecv; DWORD dwFlags = 0; DWORD dwRet; BOOL fPending; int nRet; // // Setup the WSABUF and WSAOVERLAPPED structures // memset (& over, 0, sizeof (WSAOVERLAPPED)); wsabuf.buf = buff; wsabuf.len = dwBufSize; over.hEvent = WSACreateEvent (); fPending = FALSE ; nRet = WSASendTo (m_Socket, // Socket & wsabuf, // WSABUF 1, // Number of buffers & dwRecv, // Bytes received dwFlags, // Flags lpTo, sizeof (sockaddr), & over, // WSAOVERLAPPED NULL); // Completion function if (nret! = 0) {int erro = wsagetlasterror (); if (erro == WSA_IO_PENDING) Fpeding = true; else {trace1 ("Cudpsock :: SendBuffer Erro% D / N", Erro); CloseHandle (over) . HEVENT); RETURN FALSE;}} // // if the I / o isn't finished ... // if (fpedding) {/// Wait for the request to completion // or the exit event to be Signaled // dwret = WaitForsingleObject (Over.hevent, 60000); /// Was The Recv Event Signa LED? // if (dwret == Wait_timeout) // Wait_Object_0 / Wait_Timeout {CloseHandle (over.hevent); Trace ("Wait_Timeout Send Fail / N", NULL); Return False;} if (dwret! = Wait_Object_0) // WAIT_OBJECT_0 / WAIT_TIMEOUT {CloseHandle (Over.hevent); Trace ("Send Failure / N", NULL); Return False;} /// Get I / O Result // IF (! WsagetoverlappedResult (M_Socket, & over, & DwRecv, False , & dwflags)) {CloseHandle (Over.hevent); Trace ("WsageToverlappedResult Send Fail / N", NULL); Return False;}} CloseHandle (Over.hevent); Trace ("Send Success / N", NULL); M_LOCK .Unlock (); Return True;
} Received: BOOL CUdpSock :: RecvRequest (LPBYTE pBuf, DWORD dwBufSize, struct sockaddr FAR * lpFrom) {WSAOVERLAPPED over; WSABUF wsabuf; DWORD dwRecv; DWORD dwFlags; DWORD dwRet; HANDLE hEvents [2]; BOOL fPending; int nRet; / / // zero the buffer so the recv is null-terminated // MEMSET (PBUF, 0, dwbufsize); // // setup the wsabuf and wsabuf.buf = (char *) PBUF; wsabuf.len = dwBufSize; memset (& over, 0, sizeof (WSAOVERLAPPED)); over.hEvent = m_hEventSock; dwFlags = 0; fPending = FALSE; int sizeAddr = sizeof (sockaddr_in); nRet = WSARecvFrom (m_Socket, // Socket & wsabuf, // WSABUF 1, // Number of buffers & dwRecv, // Bytes received & dwFlags, // Flags lpFrom, & sizeAddr, & over, // WSAOVERLAPPED NULL);! // Completion function if (! nRet = 0) {if (WSAGetLastError () = WSA_IO_PENDING ) {RETURN FALSE;} else fpending = true;} /// if the I / o isn't finished ... // if (fpedding) {/// Wait for the request to completion / / hEvents [0] = over.hevent; hevents [1] = m_heventex it; dwRet = WaitForMultipleObjects (2, hEvents, FALSE, INFINITE);? // // Was the recv event signaled // if (dwRet = 0!) {return FALSE;}! if (WSAGetOverlappedResult (m_Socket, & over, & dwRecv, False, & dwflags) RETURN FALSE;} /// RECV EVENT IS Complete - Keep Statistics // m_translate = dwrecv; Return True;} Data processing section.
BOOL CUdpSock :: DelWithResData (struct sockaddr FAR * lpFrom) {DWORD lenPag = sizeof (PackHead); DWORD start = 0; DWORD onePagLeft = 0; SockPags pags; if (m_bFillHead) {onePagLeft = m_PackHead.len - lenPag; if (m_SimpleIOBuffer .GetBufferlen ()