資源簡介
代碼片段和文件信息
//Download?by?http://www.NewXing.com
//?Copyright?(c)?1999?Lee?Patterson
//?leepatterson@home.com
//?blocksock.cpp?(CBlockingSocketException?CBlockingSocket?CHttpBlockingSocket)
#include?
#include?
#include?
#include?“blockingsocket.h“
#define?ASSERT?assert
#define?VERIFY?ASSERT
//?Class?CBlockingSocket
void?CBlockingSocket::Cleanup()
{
//?doesn‘t?throw?an?exception?because?it‘s?called?in?a?catch?block
if(m_hSocket?==?NULL)?return;
closesocket(m_hSocket);
m_hSocket?=?NULL;
}
void?CBlockingSocket::Create(int?nType?/*?=?SOCK_STREAM?*/)
{
ASSERT(m_hSocket?==?NULL);
if((m_hSocket?=?socket(AF_INET?nType?0))?==?INVALID_SOCKET)?{
throw?“Create“;
}
}
void?CBlockingSocket::Bind(LPCSOCKADDR?psa)
{
ASSERT(m_hSocket?!=?NULL);
if(bind(m_hSocket?psa?sizeof(SOCKADDR))?==?SOCKET_ERROR)?{
throw?“Bind“;
}
}
void?CBlockingSocket::Listen()
{
ASSERT(m_hSocket?!=?NULL);
if(listen(m_hSocket?5)?==?SOCKET_ERROR)?{
//throw?“Listen“;
throw?“Listen“;
}
}
bool?CBlockingSocket::Accept(CBlockingSocket&?sConnect?LPSOCKADDR?psa)
{
ASSERT(m_hSocket?!=?NULL);
ASSERT(sConnect.m_hSocket?==?NULL);
int?nLengthAddr?=?sizeof(SOCKADDR);
sConnect.m_hSocket?=?accept(m_hSocket?psa?&nLengthAddr);
if(sConnect?==?INVALID_SOCKET)?{
//?no?exception?if?the?listen?was?canceled
if(WSAGetLastError()?!=?WSAEINTR)?{
throw?“Accept“;
}
return?FALSE;
}
return?TRUE;
}
void?CBlockingSocket::Close()
{
ASSERT(m_hSocket?!=?NULL);
if(closesocket(m_hSocket)?==?SOCKET_ERROR)?{
//?should?be?OK?to?close?if?closed?already
throw?“Close“;
}
m_hSocket?=?NULL;
}
void?CBlockingSocket::Connect(LPCSOCKADDR?psa)
{
ASSERT(m_hSocket?!=?NULL);
//?should?timeout?by?itself
if(connect(m_hSocket?psa?sizeof(SOCKADDR))?==?SOCKET_ERROR)?{
throw?“Connect“;
}
}
int?CBlockingSocket::Write(const?char*?pch?const?int?nSize?const?int?nSecs)
{
int?nBytesSent?=?0;
int?nBytesThisTime;
const?char*?pch1?=?pch;
do?{
nBytesThisTime?=?Send(pch1?nSize?-?nBytesSent?nSecs);
nBytesSent?+=?nBytesThisTime;
pch1?+=?nBytesThisTime;
}?while(nBytesSent? return?nBytesSent;
}
int?CBlockingSocket::Send(const?char*?pch?const?int?nSize?const?int?nSecs)
{
ASSERT(m_hSocket?!=?NULL);
//?returned?value?will?be?less?than?nSize?if?client?cancels?the?reading
FD_SET?fd?=?{1?m_hSocket};
TIMEVAL?tv?=?{nSecs?0};
if(select(0?NULL?&fd?NULL?&tv)?==?0)?{
throw?“Send?timeout“;
}
int?nBytesSent;
if((nBytesSent?=?send(m_hSocket?pch?nSize?0))?==?SOCKET_ERROR)?{
throw?“Send“;
}
return?nBytesSent;
}
int?CBlockingSocket::Receive(char*?pch?const?int?nSize?const?int?nSecs)
{
ASSERT(m_hSocket?!=?NULL);
FD_SET?fd?=?{1?m_hSocket};
TIMEVAL?tv?=?{nSecs?0};
if(select(0?&fd?NULL?NULL?&tv)?==?0)?{
throw?“Receive?timeout“;
}
int?nBytesReceived;
if((nBytesReceived?=?recv(m_hSocket?pch?nSize?0))?==?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????8155??2011-05-16?16:32??www.NewXing.com\blockingsocket.cpp
?????文件???????8598??2011-05-16?16:32??www.NewXing.com\gameob.cpp
?????文件???????5141??2011-05-16?16:32??www.NewXing.com\llist.cpp
?????文件??????12946??2011-05-16?16:32??www.NewXing.com\server.cpp
????.......??????3943??1999-07-02?19:30??www.NewXing.com\server.dsp
????.......???????535??1998-10-19?21:13??www.NewXing.com\server.dsw
?????文件???????3471??2011-05-16?16:32??www.NewXing.com\blockingsocket.h
?????文件???????3188??2011-05-16?16:32??www.NewXing.com\gameob.h
?????文件???????1496??2011-05-16?16:32??www.NewXing.com\llist.h
-----------?---------??----------?-----??----
????????????????47473????????????????????9
- 上一篇:基于QT的黑白棋游戲
- 下一篇:c語言萬年歷的課程設計及源碼
評論
共有 條評論