资源简介
C++ 多线程TELNET服务程序

代码片段和文件信息
//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 < nSize);
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语言万年历的课程设计及源码
相关资源
- 基于mfc的多线程文件传输
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 移木块游戏,可以自编自玩,vc6.0编写
- MFC数字钟(基于VC6.0)
- 安科瑞智能电能表MODBUS通讯程序 VC6
- VC++MFC小游戏实例教程(实例)+MFC类库
- c语言看发的网络协议 ,源代码
- VC6LineNumberAddin.dll
- 用VC6.0实现多边形扫描线填充算法
- 基于C语言实现的网络爬虫(搜索引擎
- VC++实现CMD命令执行与获得返回信息
- VC助手 VC6.0助手
- TCP/IP客户端和服务器端源代码,好用
- modbus tcp/ip 简单通讯
- windows网络编程_文件传输
- TCP/IP与串口调试
- VC++基于OpenGL模拟的一个3维空间模型
- 基于VC++的SolidWorks二次开发SolidWorks
- 西北大学2015年计算机网络复试真题回
- VC6 USB开发源码
- VC操作SQLSERVER数据库
- aes加解密(vc源程序)
- vc_串口通讯
- 吕鑫vc6c++数据结构视频源码
- 派克变换VC++源码(附文档)
- 基于opencv漫水填充算法综合
- VC++ 串口
- VC++ 大富翁4_大富翁游戏源码
- MFC的异步网络通讯应用程序
评论
共有 条评论