关于VC网络编程中用 char 发送结构体的说明
关于VC网络编程中用 char 发送结构体的说明
在VC中要发送struct的程序,如果需要中间打包的话,首先因该将结构体转换为字节型的。然后再用
send (SOCKET s,const char FAR * buf,int len,int flags)发送。
这里要注意的是buf可以包含\0字符,而且len必须正确。
包含有\0字符的struct中间过程转换成char可能会有问题。
这里是一个字符转换的CLASS
// Buffer.cpp: implementation of the CBuffer class.
//
//
#include "stdafx.h"
#include "Buffer.h"
#include "Math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
//
// FUNCTION: CBuffer
//
// DESCRIPTION: Constructs the buffer with a default size
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
CBuffer::CBuffer()
{
// Initial size
m_nSize = 0;
m_pPtr = m_pBase = NULL;
}
//
// FUNCTION: ~CBuffer
//
// DESCRIPTION: Deallocates the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
CBuffer::~CBuffer()
{
if (m_pBase)
VirtualFree(m_pBase,0,MEM_RELEASE);
}
//
// FUNCTION: Write
//
// DESCRIPTION: Writes data into the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
BOOL CBuffer::Write(PBYTE pData, UINT nSize)
{
ReAllocateBuffer(nSize + GetBufferLen());
CopyMemory(m_pPtr,pData,nSize);
// Advance Pointer
m_pPtr+=nSize;
return nSize;
}
//
// FUNCTION: Insert
//
// DESCRIPTION: Insert data into the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
BOOL CBuffer::Insert(PBYTE pData, UINT nSize)
{
ReAllocateBuffer(nSize + GetBufferLen());
MoveMemory(m_pBase+nSize,m_pBase,GetMemSize() - nSize);
CopyMemory(m_pBase,pData,nSize);
// Advance Pointer
m_pPtr+=nSize;
return nSize;
}
//
// FUNCTION: Read
//
// DESCRIPTION: Reads data from the buffer and deletes what it reads
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
UINT CBuffer::Read(PBYTE pData, UINT nSize)
{
// Trying to byte off more than ya can chew - eh?
if (nSize > GetMemSize())
return 0;
// all that we have
if (nSize > GetBufferLen())
nSize = GetBufferLen();
if (nSize)
{
// Copy over required amount and its not up to us
// to terminate the buffer - got that!!!
CopyMemory(pData,m_pBase,nSize);
// Slide the buffer back - like sinking the data
MoveMemory(m_pBase,m_pBase+nSize,GetMemSize() - nSize);
m_pPtr -= nSize;
}
DeAllocateBuffer(GetBufferLen());
return nSize;
}
//
// FUNCTION: GetMemSize
//
// DESCRIPTION: Returns the phyical memory allocated to the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
UINT CBuffer::GetMemSize()
{
return m_nSize;
}
//
// FUNCTION: GetBufferLen
//
// DESCRIPTION: Get the buffer 'data' length
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
UINT CBuffer::GetBufferLen()
{
if (m_pBase == NULL)
return 0;
int nSize =
m_pPtr - m_pBase;
return nSize;
}
//
// FUNCTION: ReAllocateBuffer
//
// DESCRIPTION: ReAllocateBuffer the Buffer to the requested size
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
UINT CBuffer::ReAllocateBuffer(UINT nRequestedSize)
{
if (nRequestedSize < GetMemSize())
return 0;
// Allocate new size
UINT nNewSize = (UINT) ceil(nRequestedSize / 1024.0) * 1024;
// New Copy Data Over
PBYTE pNewBuffer = (PBYTE) VirtualAlloc(NULL,nNewSize,MEM_COMMIT,PAGE_READWRITE);
UINT nBufferLen = GetBufferLen();
CopyMemory(pNewBuffer,m_pBase,nBufferLen);
if (m_pBase)
VirtualFree(m_pBase,0,MEM_RELEASE);
// Hand over the pointer
m_pBase = pNewBuffer;
// Realign position pointer
m_pPtr = m_pBase + nBufferLen;
m_nSize = nNewSize;
return m_nSize;
}
//
// FUNCTION: DeAllocateBuffer
//
// DESCRIPTION: DeAllocates the Buffer to the requested size
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
UINT CBuffer::DeAllocateBuffer(UINT nRequestedSize)
{
if (nRequestedSize < GetBufferLen())
return 0;
// Allocate new size
UINT nNewSize = (UINT) ceil(nRequestedSize / 1024.0) * 1024;
if (nNewSize < GetMemSize())
return 0;
// New Copy Data Over
PBYTE pNewBuffer = (PBYTE) VirtualAlloc(NULL,nNewSize,MEM_COMMIT,PAGE_READWRITE);
UINT nBufferLen = GetBufferLen();
CopyMemory(pNewBuffer,m_pBase,nBufferLen);
VirtualFree(m_pBase,0,MEM_RELEASE);
// Hand over the pointer
m_pBase = pNewBuffer;
// Realign position pointer
m_pPtr = m_pBase + nBufferLen;
m_nSize = nNewSize;
return m_nSize;
}
//
// FUNCTION: Scan
//
// DESCRIPTION: Scans the buffer for a given byte sequence
//
// RETURNS: Logical offset
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
int CBuffer::Scan(PBYTE pScan,UINT nPos)
{
if (nPos > GetBufferLen() )
return -1;
PBYTE pStr = (PBYTE) strstr((char*)(m_pBase+nPos),(char*)pScan);
int nOffset = 0;
if (pStr)
nOffset = (pStr - m_pBase) + strlen((char*)pScan);
return nOffset;
}
//
// FUNCTION: ClearBuffer
//
// DESCRIPTION: Clears/Resets the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
void CBuffer::ClearBuffer()
{
// Force the buffer to be empty
m_pPtr = m_pBase;
DeAllocateBuffer(1024);
}
//
// FUNCTION: Write
//
// DESCRIPTION: Writes a string a the end of the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
BOOL CBuffer::Write(CString& strData)
{
int nSize = strData.GetLength();
return Write((PBYTE) strData.GetBuffer(nSize), nSize);
}
//
// FUNCTION: Insert
//
// DESCRIPTION: Insert a string at the beginning of the buffer
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
BOOL CBuffer::Insert(CString& strData)
{
int nSize = strData.GetLength();
return Insert((PBYTE) strData.GetBuffer(nSize), nSize);
}
//
// FUNCTION: Copy
//
// DESCRIPTION: Copy from one buffer object to another...
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
void CBuffer::Copy(CBuffer& buffer)
{
int nReSize = buffer.GetMemSize();
int nSize = buffer.GetBufferLen();
ClearBuffer();
ReAllocateBuffer(nReSize);
m_pPtr = m_pBase + nSize;
CopyMemory(m_pBase,buffer.GetBuffer(),buffer.GetBufferLen());
}
//
// FUNCTION: GetBuffer
//
// DESCRIPTION: Returns a pointer to the physical memory determined by the offset
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
PBYTE CBuffer::GetBuffer(UINT nPos)
{
return m_pBase+nPos;
}
//
// FUNCTION: GetBuffer
//
// DESCRIPTION: Returns a pointer to the physical memory determined by the offset
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
///
void CBuffer::FileWrite(const CString& strFileName)
{
CFile file;
if (file.Open(strFileName, CFile::modeWrite | CFile::modeCreate))
{
file.Write(m_pBase,GetBufferLen());
file.Close();
}
}
//
// FUNCTION: Delete
//
// DESCRIPTION: Delete data from the buffer and deletes what it reads
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// N T ALMOND 270400 1.0 Origin
//
UINT CBuffer::Delete(UINT nSize)
{
// Trying to byte off more than ya can chew - eh?
if (nSize > GetMemSize())
return 0;
// all that we have
if (nSize > GetBufferLen())
nSize = GetBufferLen();
if (nSize)
{
// Slide the buffer back - like sinking the data
MoveMemory(m_pBase,m_pBase+nSize,GetMemSize() - nSize);
m_pPtr -= nSize;
}
DeAllocateBuffer(GetBufferLen());
return nSize;
}
关于VC网络编程中用 char 发送结构体的说明相关推荐
- c语言send发送结构体,Socket编程中用send发送结构体
Socket编程中用send发送结构体 原创 2010年04月28日 19:17:00 标签:socket /编程 /struct /google /string /input 11868 最近在开发 ...
- linux socket 结构定义 send,Linux下Socket编程中用send发送结构体
Linux网络通信 Linux下多客户端聊天软件 最近在开发一个Linux下的聊天软件,好久没有做C语言的开发了,感觉到很多东西已经生疏了,这下又碰到用Socket传递结构体的问题,google了一下 ...
- Linux下Socket编程中用send发送结构体
转自:http://tech.ddvip.com/2008-10/122543769687623.html Linux网络通信 Linux下多客户端聊天软件 Linux程序设计 Linux socke ...
- 基于Winsock API的VC网络编程实战
基于Winsock API的VC网络编程实战 随着计算机信息技术的飞速发展,互联网与人类社会的工作.生活越来越紧密相关,它已经成为人类获取.交流信息的重要途径和手段.所以当前对于开发人员来说,网络编程 ...
- VC驿站《VC++网络编程班》开课啦!
原文链接:http://www.cctry.com/thread-240361-1-1.html 〓课程声明〓 VC驿站的各位网友大家好,非常感谢大家的支持.应广大网友要求,VC驿站特推出<VC ...
- Java网络编程(二) 连续发送数据
有了建立连接的基础后,接下来尝试连续地发送和接收数据. 对于发送端比较好理解,有数据时就可以发送出去.对于接收端,就需要一直监听是否有数据发送过来.一个基本的方法就是使用 while(true) 循环 ...
- c语言发送结构体 文件
Linux C Socket编程发送结构体.文件详解及实例 利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式 ...
- JNA参数类型转换(含接收、发送结构体)——JNA-JNI(五)
JNA参数类型转换(含接收.发送结构体)--JNA-JNI(五) 系列文章: Java通过JNI调用C++动态链接库dll,并打在jar包内 --JNA-JNI(一) Java使用JNA调用C++动态 ...
- vc 网络编程(socket)TCP/UDP 介绍
在网上找了很多的资料,现将这些资料整合起来,详细介绍一下VC下的socket编程,并提供一个服务器客户端具体的实例.希望对您有所帮助 一.原理部分 在网络编程中最常用的方案便是Client/Serve ...
最新文章
- 用NVIDIA Tensor Cores和TensorFlow 2加速医学图像分割
- 五步让你玩转CocoaPods
- (Pwn)CTF工具 ROPgadget 的安装与使用介绍
- java ueditor 图片上传加水印_Ueditor编辑器上传图片加水印【亲测可用】-帝国CMS整合...
- 对你的的应用程序进行Debug
- 用matplotlib画sigmoid函数
- 电阻用计算机怎么算,电阻分压计算器_电阻分压计算公式_电阻分压计算软件 - 电子发烧友(www.elecfans.com)...
- 计算机软件故障及其处理方法,计算机常见软件故障及其解决方法
- 乔布斯其人的演讲技巧
- Python小白的飞桨之旅
- Rest Stops 题解
- 通过youtube上传视频赚钱并免费宣传你的业务
- 开机提示小娜无法在本计算机运行,解决Win10正式版开始菜单与小娜打不开问题的三种方法...
- 山地自行车轮市场前景分析及行业研究报告
- html网页设计代码作业——代码质量好-重庆火锅(5页) HTML+CSS+JavaScript 大学生网页制作期末作业
- 【字符串】1374. 生成每种字符都是奇数个的字符串(简单)
- C++教程网编程视频
- linux vi 剪切板,让Vim支持在终端剪贴板共享
- 谷歌宣布收购全球最大数据科学社区Kaggle
- 上海积分落户计算机水平加分,2019年上海应届生落户打分72分标准拿分策略