為Socket寫的附加方法

來源:互聯網
上載者:User

 自己封裝了一個.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寫的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.