人們通常用電話連線來說明TCP協議,而UDP協議,則常常用郵遞來做比喻。與TCP有連接的信息傳輸方式不同,UDP協議被認為是對底層IP協議簡單的擴展:協議并不保證每個數據包都會到達目的地,也不保證到達的順序,而僅僅就是“盡力”的發送每一個數據包。我在這篇教程中有時候使用“數據包”有時候使用“數據報”,廣義的說,這兩個詞意思類似,有代表一個有大小邊緣的數據塊。但是,用“數據包”的時候,我想強調的是這個數據塊中所傳送的數據部分;而“數據報”則更強調在數據塊中對這段數據的信息和說明部分,比如IP首部,TCP和UDP首部,TCP和UDP報文段這些信息。TCP協議通過同步驗證實現了TCP層面上的“數據流”傳送,而下層的IP協議,依然是數據報形式的傳送,這個我們在前面已經描述過,比如連接握手和斷開握手,實際上都是發送的TCP數據報(TCP格式的IP數據報)。UDP格式的IP數據報為IP數據報指定了UDP端口,從而使這樣的IP數據報的目的地能夠精確到應用程序——沒有端口指定的IP數據報目的地只能精確到具有IP地址的主機。另外,與TCP的無邊緣保證相反,UDP數據包是有大小的,而其最大限制也即是IP數據包大小的最大限制:65,507字節(這里需要說明兩點:1、IP數據包的理論最大值為2^16 - 1,即65,535字節,UDP數據報因為要包含UDP首部的信息,所以比這個值小一點;2、因為MTU的存在,實際傳輸中的IP數據包會被分封到1500字節以下。)
因為UDP是無連接的,就像一個郵筒,可以接受來自任何人的郵件;也可以發送給任何人的郵件。而每一次接受,都會得到來向的地址;每一次發送,也必須指明去向的地址。我們設計一個類,分別以lastfromSockAddr和destinationSockAddr表示最后一次來向的地址以及(下一次發送的)目的地地址。需要指出的是,因為防火墻的普遍存在,最后一次來向地址變得極其重要!這一點我們將在后面的討論中看到。
class UDPServerSock: public BaseSock {
private:
sockaddr_in serverSockAddr;
protected:
mutable sockaddr_in lastfromSockAddr;
sockaddr_in destinationSockAddr;
char* preBuffer;
int preBufferSize;
mutable int preReceivedLength;
public:
explicit UDPServerSock(
unsigned short server_port,
int pre_buffer_size = 32);
virtual ~UDPServerSock();
void UDPSetDest(const char* dest_IP,
const unsigned short& dest_port);
void UDPSetDest(const sockaddr_in& dest_sock_addr);
int UDPReceive() const;
int UDPSendtoDest(const char* send_data,
const int& data_length) const;
};
我們把最后一次來向地址以及預接收緩存中的收到的數據長度設置成mutable是因為我們希望接收UDPReceive()這個方法看起來是不改變對象的。每一次接收,實際上都會刷新lastfromSockAddr,而作為服務器,往往也是通過lastfromSockAddr去決定destinationSockAddr的。
UDPServerSock::UDPServerSock(unsigned short server_port,
int pre_buffer_size):
preBufferSize(pre_buffer_size), preReceivedLength(0)
{
preBuffer = new char[preBufferSize];
memset(&serverSockAddr, 0, sizeof(serverSockAddr));
memset(&lastfromSockAddr, 0, sizeof(lastfromSockAddr));
memset(&destinationSockAddr, 0, sizeof(destinationSockAddr));
serverSockAddr.sin_family = AF_INET;
serverSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverSockAddr.sin_port = htons(server_port);
sockFD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockFD < 0) {
sockClass::error_info("sock() failed.");
}
if (bind( sockFD,
(sockaddr*)&serverSockAddr,
sizeof(serverSockAddr)) < 0) {
sockClass::error_info("bind() failed.");
}
}
UDPServerSock::~UDPServerSock()
{
delete [] preBuffer;
close(sockFD);
}
構造函數依然使用socket()建立sockFD,然后通過bind()將本機的SockAddr(主要是指定了端口)綁定到這個sockFD上。
我們重載了UDPSetDest()這個方法,可以有兩種方式去指定目標地址destinationSockAddr——既可以指定IP地址和端口,也可以直接賦值以sockaddr_in結構。
void UDPServerSock::UDPSetDest(const char* dest_IP,
const unsigned short& dest_port)
{
destinationSockAddr.sin_family = AF_INET;
destinationSockAddr.sin_addr.s_addr = inet_addr(dest_IP);
destinationSockAddr.sin_port = htons(dest_port);
}
void UDPServerSock::UDPSetDest(const sockaddr_in& dest_sock_addr)
{
destinationSockAddr.sin_family = dest_sock_addr.sin_family;
destinationSockAddr.sin_addr.s_addr = dest_sock_addr.sin_addr.s_addr;
destinationSockAddr.sin_port = dest_sock_addr.sin_port;
}
最后是接收和發送。我們知道TCP里面recv()返回0表示連接正常斷開,而UDP里面,則僅僅就是表示收到0字節的數據包。可見,數據大小為0,并不代表數據包為空,因為這個數據包實際也是一個數據報,包含著TCP數據報的各種必要信息。
int UDPServerSock::UDPReceive() const
{
socklen_t from_add_len = sizeof(lastfromSockAddr); //use int in win32
preReceivedLength = recvfrom( sockFD,
preBuffer,
preBufferSize,
0,
(sockaddr*)&lastfromSockAddr,
&from_add_len);
if ( preReceivedLength < 0) {
sockClass::error_info("recv() failed.");
}
return preReceivedLength;
}
int UDPServerSock::UDPSendtoDest(const char* send_data,
const int& data_length) const
{
int send_message_size = sendto( sockFD,
send_data,
data_length,
0,
(sockaddr*)&destinationSockAddr,
sizeof(destinationSockAddr));
if (send_message_size < 0) {
sockClass::error_info("send() failed.");
}
if (send_message_size != data_length) {
sockClass::error_info(
"send() sent a different number of bytes than expected.");
}
return send_message_size;
}
posted on 2010-06-10 12:16
lf426 閱讀(2905)
評論(0) 編輯 收藏 引用 所屬分類:
SDL入門教程 、
socket 編程入門教程