资源简介
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网络对战TCP五子棋
- VC++源码字识别系统
- VC关联指定后缀名和打开程序
- STK与VC++ 6.0联合编程
- VC++MFC串口调试助手完整编辑过程介绍
- VC++端口转发程序源码
- C++ 聊天软件源代码(内容加密RSA加密
- VC++操作WPS表格的类(VC6.0编译通过)
- TCP、UDP端口及ICMP网络扫描工具
- php_memcache-5.3-VC6-x86.zip
- RSA加解密源码VC++
- 在VC++6.0中编写的音乐播放器源代码
- wav生成波形图
- 图书管理系统MFC+SQL+VC
- vc++ mfc 基于udp协议在不同ip地址之间
- 基于LIBPCAP的网络流量实时采集与信息
- VC++2005测试版运行库 Microsoft.VC80.Debu
- 基于MFCVC6.0的简单计算器程序
- 猎豹网校C++ Primer初中高全套无密版
- MFC实时网络连接装状况监测代码
- VC++基于mfc 实现对话框打开读取txt文件
- Visual C++ Build Tools 2015 离线包.part1/7
- vc++ 2005 express 完整安装包
- VC++之MFC类库中文手册一怀去意---
- MFC写的windows网络热点共享源代码
- 无需调用cmd即可实现的发现网络中的
- Visual C++开发大全提高卷)光盘资料
- MFC视频播放器(带播放列表VC++6.0源代
- 基于MFC的软键盘
- VC++调用大漠插件
评论
共有 条评论