惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - BearOcean

LOG.ZS.0001.基于Freetype的游戏字体渲染优化思路 const 和指针 C++ 下啥时候用struct, 啥时候用class C++ 和 Java 中的变参 解决站点关键数据,状态数据,无须持久化数据的一些思路 BS程序代码与安全与基本攻击/防御模式 Struts 实现的I18N Ant 阅读笔记 进度,效率,与个人事务管理 Personal Task 1.0 MySql与Java的时间类型 数据挖掘概述 解决Thread 的关闭问题和参数传递时想到的办法. Command 模式 .Net标准控件与自定义控件(2) ToolTipButton 内网聊天工具FreeChat 2.0 FreeChat 2.0 ...大改 模型和架构 局域网聊天工具FreeChat 1.0 开发日志 内网聊天工具FreeChat Beta .Net 事件
为Socket写的附加方法
BearOcean · 2006-01-08 · via 博客园 - BearOcean

 自己封装了一个.Net的异步socket
 在Server端写了一个对象池来提供服务.

 然后为在网络中传递的数据写了一个Pack和Unpack方法.
 打算放到Socket类中

 基本的思路是:在Send以前调用PackMessage
 在Message(以Byte数组的方式存在)前面加上一些附加信息.
 例如发送的消息字节数.

 然后在Receive端接收打了包的字节流,解析出Pack在前面的大小信息.
 再根据这个大小判断是否接收到完整的数据.

 还可以写得更复杂,加上一些别的东西.
 但暂时就是这样,也觉得代码写得不好...最近好象没有什么状态.

 测试代码如下:

 1#include "stdafx.h"
 2
 3#using <mscorlib.dll>
 4
 5using namespace System;
 6const int ADDITIONINFO=10;
 7
 8Byte PackMessage(String * message) []
 9{
10    Byte btRealMessage[]=Text::Encoding::ASCII->GetBytes(message);
11    int iSize=btRealMessage->Length;
12    String *strSize=iSize.ToString();
13    Console::WriteLine("RealMessage Length:{0}",strSize);
14
15    Byte btSizeBuffer[]=Text::Encoding::ASCII->GetBytes(strSize);
16
17    Byte btAdditionInfo[]=new Byte[ADDITIONINFO];
18    Array::Copy(btSizeBuffer,0,btAdditionInfo,0,btSizeBuffer->Length);
19    Byte btPackedMessage[]=new Byte[ADDITIONINFO+iSize];
20    Array::Copy(btAdditionInfo,0,btPackedMessage,0,ADDITIONINFO);
21    Array::Copy(btRealMessage,0,btPackedMessage,ADDITIONINFO,iSize);
22    return btPackedMessage;
23}
;
24
25
26Byte UnPackMessage(Byte btPackedMessage[])[]
27{
28    Byte btSize[]=new Byte[ADDITIONINFO];
29    Array::Copy(btPackedMessage,0,btSize,0,ADDITIONINFO);
30    String *strSize=Text::Encoding::ASCII->GetString(btSize);
31    Console::WriteLine("The message Length is {0}",strSize);
32
33    Byte btRealMessage[]=new Byte[Convert::ToInt32(strSize)];
34    Array::Copy(btPackedMessage,ADDITIONINFO,btRealMessage,0,Convert::ToInt32(strSize));
35    
36    return btRealMessage;
37}

38
39int _tmain()
40{
41    // TODO: 请用您自己的代码替换下面的示例代码。
42    Console::WriteLine("Please input Message:");
43    String *buffer=Console::ReadLine();
44    Byte btResult[]=PackMessage(buffer);
45    Console::WriteLine("The message after pack is:");
46
47    System::Collections::IEnumerator* myEnumerator = btResult->GetEnumerator();
48     while ( myEnumerator->MoveNext() )
49    {
50        Console::Write( "-{0}", myEnumerator->Current );
51    }

52    Console::WriteLine();
53 
54    Byte realMessage[]=UnPackMessage(btResult);
55    
56    Console::WriteLine("The Message after unpack is:");
57    System::Collections::IEnumerator* myEnumerator1 = realMessage->GetEnumerator();
58     while ( myEnumerator1->MoveNext() )
59    {
60        Console::Write( "-{0}", myEnumerator1->Current );
61    }

62    Console::WriteLine();
63
64
65    return 0;
66}

67


这个是用C++Managed写的.