class UDPClientSock: public BaseSock {
protected:
mutable sockaddr_in lastfromSockAddr;
sockaddr_in destinationSockAddr;
char* preBuffer;
int preBufferSize;
mutable int preReceivedLength;
public:
explicit UDPClientSock(int pre_buffer_size = 32);
virtual ~UDPClientSock();
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;
};
在最初設(shè)計(jì)這個(gè)類(lèi)的時(shí)候,我曾經(jīng)考慮過(guò)安排一個(gè)服務(wù)器地址的私有數(shù)據(jù)成員,并且在構(gòu)造函數(shù)里面指定服務(wù)器的地址。但是,后來(lái)我覺(jué)得使用“目的地”比“服務(wù)器”更加能體現(xiàn)出UDP無(wú)連接的本質(zhì)特點(diǎn)。TCP之所以有個(gè)服務(wù)器,是因?yàn)門(mén)CP的客戶端只能和自己的服務(wù)器端通訊。而UDP的客戶端可以與任何一個(gè)UDP端口通訊——只要知道對(duì)方的地址(IP地址和UDP端口)就可以發(fā)送數(shù)據(jù)包。況且,在網(wǎng)絡(luò)情況越來(lái)越復(fù)雜的今天,很多服務(wù)器都不僅僅使用一個(gè)IP地址或者域名,比如網(wǎng)站和游戲服務(wù)器,而對(duì)于客戶端來(lái)說(shuō),只是在意連接到了指定的網(wǎng)站,比如google,而并不清楚是連接到google的哪個(gè)服務(wù)器。程序內(nèi)部可能會(huì)根據(jù)網(wǎng)絡(luò)條件對(duì)具體連接的服務(wù)器地址進(jìn)行調(diào)整,所以,可以隨時(shí)根據(jù)具體情況指定“目的地”,而不是一開(kāi)始就指定一個(gè)“服務(wù)器”地址,這種策略顯得更加靈活。protected:
mutable sockaddr_in lastfromSockAddr;
sockaddr_in destinationSockAddr;
char* preBuffer;
int preBufferSize;
mutable int preReceivedLength;
public:
explicit UDPClientSock(int pre_buffer_size = 32);
virtual ~UDPClientSock();
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;
};
通常情況下,客戶端也并不在意lastfromSockAddr,因?yàn)樽詈笠淮蝸?lái)向的地址,往往就是目的地服務(wù)器的地址。我們說(shuō)過(guò),服務(wù)器的端口是指定的,這是為了讓客戶端明確的知道,可以去連接。而客戶端的端口的端口則是系統(tǒng)指定的——我們并沒(méi)有在客戶端調(diào)用bind(),所以socket機(jī)制會(huì)自動(dòng)幫我們綁定一個(gè)端口。通常客戶端自己也不需要知道這個(gè)端口號(hào)是多少,只有接收到這次UDP數(shù)據(jù)報(bào)的服務(wù)器端知道,并且按照這個(gè)端口號(hào)將服務(wù)器的信息傳送過(guò)來(lái)——沒(méi)有收到這個(gè)端口發(fā)出的數(shù)據(jù)報(bào)的UDP端口很難知道這個(gè)系統(tǒng)指定的端口號(hào)是多少。但是,因?yàn)檫@個(gè)UDP端口實(shí)際上是可以接受來(lái)自其他任何UDP端口的數(shù)據(jù)的,所以,如果你需要驗(yàn)證發(fā)送某次數(shù)據(jù)的地址是不是你所期望的,比如是不是來(lái)自服務(wù)器,可能就會(huì)用到lastfromSockAddr。
UDPClientSock::UDPClientSock(int pre_buffer_size):
preBufferSize(pre_buffer_size), preReceivedLength(0)
{
preBuffer = new char[preBufferSize];
memset(&lastfromSockAddr, 0, sizeof(lastfromSockAddr));
memset(&destinationSockAddr, 0, sizeof(destinationSockAddr));
sockFD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockFD < 0) {
sockClass::error_info("sock() failed.");
}
}
UDPClientSock::~UDPClientSock()
{
delete [] preBuffer;
close(sockFD);
}
其它4個(gè)類(lèi)方法,跟server端的簡(jiǎn)直一模一樣。preBufferSize(pre_buffer_size), preReceivedLength(0)
{
preBuffer = new char[preBufferSize];
memset(&lastfromSockAddr, 0, sizeof(lastfromSockAddr));
memset(&destinationSockAddr, 0, sizeof(destinationSockAddr));
sockFD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockFD < 0) {
sockClass::error_info("sock() failed.");
}
}
UDPClientSock::~UDPClientSock()
{
delete [] preBuffer;
close(sockFD);
}
void UDPClientSock::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 UDPClientSock::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;
}
int UDPClientSock::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 UDPClientSock::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;
}
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 UDPClientSock::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;
}
int UDPClientSock::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 UDPClientSock::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 19:37 lf426 閱讀(1723) 評(píng)論(0) 編輯 收藏 引用 所屬分類(lèi): SDL入門(mén)教程 、socket 編程入門(mén)教程