这个是自己项目中发现的问题,所以这个不一定适用于你的。

仅供参考。

头文件:

ESSocket.h

// ESSocket.h : header file
//
#ifndef ESSOCKET_H
#define ESSOCKET_H #pragma once #include <Winsock2.h> // win32 socket stuff #define WM_LTC_WINSOCK_MSG_RECEIVED WM_USER+001 /////////////////////////////////////////////////////////////////////////////
// CESSocket class - a replacement-class for CSocket of MFC
//
// CESSocket should not block on high data rate, as CSocket does. However,
// CESSocket is not a full replacement of CSocket. class CESSocket
{
friend class CMessageTargetWnd; public:
CESSocket();
virtual ~CESSocket(); protected:
virtual void OnReceive(int nErrorCode); public:
bool Create();
bool Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
void Close();
int GetLastError(); private:
CMessageTargetWnd *m_pWndMessageTarget;
SOCKET m_sock;
int m_nLastError; static int m_nInstanceCount;
}; //Helper class CMessageTargetWnd class CMessageTargetWnd : public CWnd
{
public:
CMessageTargetWnd(CESSocket*); protected:
LRESULT OnDataReceive(WPARAM, LPARAM);
DECLARE_MESSAGE_MAP() private:
CESSocket *m_pESSocket;
}; #endif // ESSOCKET_H

ESSocket.cpp

// ESSocket.cpp
//
#ifndef WINVER
#if _MSC_VER <= 1200 // up to VC6.0
#define WINVER 0x0400
#else
#define WINVER 0x0501
#endif
#endif #include <afxwin.h>
#include "ESSocket.h" #ifdef _DEBUG
#define new DEBUG_NEW
#endif /////////////////////////////////////////////////////////////////////////////
// CESSocket class
// int CESSocket::m_nInstanceCount = 0; CESSocket::CESSocket()
{
m_pWndMessageTarget = new CMessageTargetWnd(this);
m_sock = 0;
m_nLastError = 0; // keep track of #of instances; CESSocket is not designed to support multiple instances!
m_nInstanceCount++;
ASSERT(m_nInstanceCount == 1);
} CESSocket::~CESSocket()
{
Close(); // just in case the application did not call Close() if (WSACleanup())
TRACE(_T("WSACleanup() failed\n")); if (m_pWndMessageTarget)
{
if (::IsWindow(m_pWndMessageTarget->m_hWnd))
VERIFY(m_pWndMessageTarget->DestroyWindow()); delete m_pWndMessageTarget;
} m_nInstanceCount--;
} void CESSocket::OnReceive(int nErrorCode)
{
ASSERT(false); // derived class does not provide an implementation for OnReceive()
TRACE(_T("virtual OnReceive() called"));
} bool CESSocket::Create()
{
WSADATA WSAData; ASSERT(m_sock == 0); // call Create only once!
m_nLastError = 0; if (m_nInstanceCount != 1)
{
ASSERT(false);
return false; // this class does not support more than one instance
} if (!::IsWindow(m_pWndMessageTarget->m_hWnd))
{
CWnd *pWndParent = CWnd::GetDesktopWindow(); // This call may fail on Win98 / Unicode builds! use non- Unicode version in these cases
m_pWndMessageTarget->CWnd::Create(NULL, _T("cessocket_message_sink"), WS_CHILD,
CRect(0, 0, 20, 20), pWndParent, 0); if (!::IsWindow(m_pWndMessageTarget->m_hWnd))
{
ASSERT(false);
TRACE(_T("Creation or message- target window failed\n"));
return false;
} if (WSAStartup(MAKEWORD(1,1), &WSAData) == 0)
{
m_sock = socket(PF_INET, SOCK_STREAM, 0); if (m_sock == INVALID_SOCKET)
{
m_nLastError = WSAGetLastError();
m_sock = 0;
WSACleanup();
VERIFY(m_pWndMessageTarget->DestroyWindow());
TRACE(_T("Socket creation failed\n"));
return false;
}
}
else
{
TRACE(_T("WSAStartup failed\n"));
return false;
}
}
else
{
ASSERT(m_sock != 0);
ASSERT(false); // target window and socket already exists - Create should be called only once!
} return true;
} bool CESSocket::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
{
#ifdef _UNICODE
USES_CONVERSION; // for W2A macro
#endif PHOSTENT phe;
SOCKADDR_IN dest_sin;
struct in_addr address; if (!m_pWndMessageTarget)
{
ASSERT(false);
return false;
} if (m_sock == 0)
{
// Did you miss to call Create()? Did you already close the socket?
ASSERT(false);
return false;
} // Note: Once Close() is called, you cannot re-use the socket!
// CESSocket class is neither designed to support multiple
// instances not to re-use once closed connections. You must
// delete the current instance and create a new one for a
// re-connection or a connection to a different server. memset(&dest_sin, 0, sizeof dest_sin);
dest_sin.sin_family = AF_INET;
dest_sin.sin_port = htons(nHostPort); if (_tcschr(lpszHostAddress, '.') == 0)
{
#ifdef _UNICODE
phe = gethostbyname(W2A(lpszHostAddress));
#else
phe = gethostbyname(lpszHostAddress);
#endif if (phe == NULL)
{
m_nLastError = WSAGetLastError();
TRACE(_T("gethostbyname failed\n"));
return false;
} // if memcpy((char FAR *)&(dest_sin.sin_addr), phe->h_addr, phe->h_length);
} // if
else
{
#ifdef _UNICODE
address.s_addr = inet_addr(W2A(lpszHostAddress));
#else
address.s_addr = inet_addr(lpszHostAddress);
#endif dest_sin.sin_addr = address;
} if (connect(m_sock, (LPSOCKADDR)&dest_sin, sizeof dest_sin))
{
m_nLastError = WSAGetLastError();
TRACE(_T("Connection to server failed.\nCheck host-id and port# !\n"));
return false;
} if (WSAAsyncSelect(m_sock, *m_pWndMessageTarget, WM_LTC_WINSOCK_MSG_RECEIVED, FD_READ) > 0)
{
m_nLastError = WSAGetLastError();
TRACE(_T("WSAAsyncSelect failed\n"));
return false;
} // if TRACE(_T("Connection to server OK\n"));
m_nLastError = 0;
ASSERT(m_sock != 0);
return true; // success
} int CESSocket::Send(const void* lpBuf, int nBufLen, int nFlags)
{
if (send(m_sock, (const char*)lpBuf, nBufLen, nFlags) == SOCKET_ERROR)
{
m_nLastError = WSAGetLastError();
return false;
} m_nLastError = 0;
return true;
} int CESSocket::Receive(void* lpBuf, int nBufLen, int nFlags)
{
int nBytes = 0; if ((nBytes = recv(m_sock, (char*)lpBuf, nBufLen, nFlags)) == SOCKET_ERROR)
{
m_nLastError = WSAGetLastError();
return false;
} m_nLastError = 0;
return nBytes;
} void CESSocket::Close()
{
if (m_sock)
{
m_nLastError = 0;
ASSERT(m_pWndMessageTarget); // stop receiving messages
WSAAsyncSelect(m_sock, *m_pWndMessageTarget, 0, 0); if (closesocket(m_sock) == SOCKET_ERROR)
m_nLastError = WSAGetLastError(); m_sock = 0;
TRACE(_T("Socket closed\n"));
}
} int CESSocket::GetLastError()
{
return m_nLastError;
} /////////////////////////////////////////////////////////////////////////////
// CMessageTargetWnd class
// CMessageTargetWnd::CMessageTargetWnd(CESSocket *pESSocket)
{
m_pESSocket = pESSocket;
} LRESULT CMessageTargetWnd::OnDataReceive(WPARAM wParam, LPARAM lParam)
{
m_pESSocket->OnReceive(HIWORD(lParam));
return 0;
} BEGIN_MESSAGE_MAP(CMessageTargetWnd, CWnd)
ON_MESSAGE(WM_LTC_WINSOCK_MSG_RECEIVED, OnDataReceive)
END_MESSAGE_MAP()

解决CSocket高数据传输问题的更多相关文章

  1. 解决Safari高版本浏览器中默认禁用第三方COOKIE(含demo)

    前段时间在项目里遇到了一个比较头疼的问题,就是高版本的Safari中默认会阻止第三方cookie,这使得使用Safari浏览器的用户无法按照正常的业务逻辑进行操作. 问题展现 知识点 什么是第三方co ...

  2. Android中ListView嵌套GridView的简单消息流UI(解决宽高问题)

    最近搞一个项目,需要用到类似于新浪微博的消息流,即每一项有文字.有九宫格图片,因此这就涉及到ListView或者ScrollView嵌套GridView的问题.其中GridView的高度问题在网上都很 ...

  3. Java集群--大型网站是怎样解决多用户高并发访问的

    时间过得真快,再次登录博客园来写博,才发现距离上次的写博时间已经过去了一个月了,虽然是因为自己找了实习,但这也说明自己对时间的掌控能力还是没那么的强,哈哈,看来还需不断的努力啊!(这里得特别说明一下本 ...

  4. 利用redis + lua解决抢红包高并发的问题

    抢红包的需求分析 抢红包的场景有点像秒杀,但是要比秒杀简单点.因为秒杀通常要和库存相关.而抢红包则可以允许有些红包没有被抢到,因为发红包的人不会有损失,没抢完的钱再退回给发红包的人即可.另外像小米这样 ...

  5. 解决vs-code高cpu占用率问题

    (microsoft.vscode.cpp.extension.darwin进程高cpu占用问题) 免费的vs-code现在已经成为mac/linux平台的码农新宠,毕竟从windows平台开发vir ...

  6. oracle解决导入高版本dmp报错问题:IMP-00058: ORACLE error 12547 encountered

    低版本oracle导入高版本的dmp时,导过的人都应该清楚,直接导入是会报错的,报错信息如下,其实解决这个问题很简单, 只要修改一下dmp内的版本号就可以了. 修改版本不能随便使用文本工具打开,否知会 ...

  7. 解决eclipse高版本JDK编译的项目到低版本JDK服务器上不能运行的问题

    错误提示信息:Unsupported major.minor version 52.0,意思是说,当前jdk的版本不支持更高版本jdk编译出来的class文件. 我的编译环境,eclipse使用的是j ...

  8. Redis:解决分布式高并发修改同一个Key的问题

    本篇文章是通过watch(监控)+mutil(事务)实现应用于在分布式高并发处理等相关场景.下边先通过redis-cli.exe来测试多个线程修改时,遇到问题及解决问题. 高并发下修改同一个key遇到 ...

  9. 翻屏类 h5 适配方案:解决宽高自适应难题

    表格 图片等 宽度自适应  :width:100%;  box-sizing: border-box; 基于淘宝适配方案flexible + 翻屏h5 适配方案adaptive flexible解读及 ...

随机推荐

  1. 夏令营501-511NOIP训练18——高三楼

    传送门:QAQQAQ 题意:定义矩阵A与矩阵B重复,当且仅当A可以通过任意次行列交换得到B,例如下图A,B即为合法矩阵 现求对于$n*n$的矩阵有多少个不重复的矩阵 数据范围: 对于10%的数据 N≤ ...

  2. 树链剖分(模板) 洛谷P3384

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  3. MySQL5.1的安装过程

    开始配置文件

  4. 机器学习-线性回归算法(单变量)Linear Regression with One Variable

    1 线性回归算法 http://www.cnblogs.com/wangxin37/p/8297988.html 回归一词指的是,我们根据之前的数据预测出一个准确的输出值,对于这个例子就是价格,回归= ...

  5. Django static静态配置文件

    对于Django来说静态文件一般交由Web服务器处理,Django本身不处理静态文件.为了使Django开发环境能够处理静态文件,Django有和生产环境不同的静态文件配置方式. Django 版本: ...

  6. Entity Framework Code First 模式-建立多对多联系

    Entity Framework 在建立多对多的联系时,会生成一个中间表,用来表示这个多对多的关系.这和数据库设计时从概念模型到逻辑模型转化时,多对多的关系不能和任何一端的实体合并,需要将关系也转化为 ...

  7. csps-模拟7778lrd两试

    题面:https://www.cnblogs.com/Juve/articles/11707775.html 位运算: 大力分类讨论 第一次发现若a^b=c,则c^b=a,c^a=b #include ...

  8. csp-s模拟测试54x,y,z题解

    题面:https://www.cnblogs.com/Juve/articles/11606834.html x: 并差集,把不能分到两个集合里的元素和并到一起,设连通块个数为cnt,则答案为:$2^ ...

  9. 使用 SourceTree 操作时弹出 password required

    通过 https 的方式克隆仓库的,SourceTree 推送等操作的时候会弹出提示要求输入密码. 在仓库里面设置: 远程仓库-选中仓库-点击编辑-修改 url 路径, 路径格式,以码云为例: htt ...

  10. canvas石英钟

    canvas石英钟:demo <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...