这二天在研究webabcd的socket多人聊天室,想尝试增加一些功能,比如:允许用户除发送文字外,还能发送图片或文件。
问题:
socket发送数据时,只能发送byte[]数组,且无法附带其它用户自定义参数,服务端如何知道用户发过来的是文字还是图片或是文件呢,所以只能在byte数组上动脑筋了.
silverlight中,用户选择的图片或文件,都能变成FileStream,近而变成byte数组,初步想法是在数组前后加一些标记之类,比如最开始几个字节是"img,txt,file"中对应的一个,这样服务端接收到以后,通过拆解分析就能知道用户发过来的何种格式。
下面演示如何解码:
using System;
using System.Text;
namespace byteStudy
{
class Program
{
static void Main(string[] args)
{
string msg = "^txt^jimmy^Hello^";//格式:^类型^用户名^内容(或文件流)^
byte[] b = UTF32Encoding.UTF8.GetBytes(msg);//模拟接收到的byte数组
int _i1 = -1, _i2 = -1, _i3 = -1, _i4 = -1;//用来记录分隔字符“^”出现的四次位置
for (int i = 0; i < b.Length; i++)
{
if (b[i] == (byte)'^')
{
if (_i1 == -1)
{
_i1 = i;
}
else if (_i2 == -1)
{
_i2 = i;
}
else if (_i3 == -1)
{
_i3 = i;
}
else if (_i4 == -1)
{
_i4 = i;
}
}
Console.WriteLine(b[i].ToString());
}
Console.WriteLine("-------------------------------");
if (_i1 == 0 && _i4 == b.Length - 1 && _i3 != -1 && _i2 != -1)
{
Console.WriteLine("_i1={0}", _i1);
Console.WriteLine("_i2={0}", _i2);
Console.WriteLine("_i3={0}", _i3);
Console.WriteLine("_i4={0}", _i4);
byte[] bFlag = new byte[_i2 - _i1 - 1];
for (int i = _i1 + 1; i < _i2; i++)
{
bFlag[i - _i1 - 1] = b[i];
}
string _flag = UTF32Encoding.UTF8.GetString(bFlag);
byte[] bName = new byte[_i3 - _i2 - 1];
for (int i = _i2 + 1; i < _i3; i++)
{
bName[i - _i2 - 1] = b[i];
}
string _name = UTF32Encoding.UTF8.GetString(bName);
byte[] bContent = new byte[_i4 - _i3 - 1];
for (int i = _i3 + 1; i < _i4; i++)
{
bContent[i - _i3 - 1] = b[i];
}
string _content = UTF32Encoding.UTF8.GetString(bContent);//如果是文件的话,用其它办法还原成流,近而变成文件或图片
Console.WriteLine("-------------------------------");
Console.WriteLine("类型:" + _flag);
Console.WriteLine("用户:" + _name);
Console.WriteLine("内容:" + _content);
}
Console.Read();
}
}
}
using System.Text;
namespace byteStudy
{
class Program
{
static void Main(string[] args)
{
string msg = "^txt^jimmy^Hello^";//格式:^类型^用户名^内容(或文件流)^
byte[] b = UTF32Encoding.UTF8.GetBytes(msg);//模拟接收到的byte数组
int _i1 = -1, _i2 = -1, _i3 = -1, _i4 = -1;//用来记录分隔字符“^”出现的四次位置
for (int i = 0; i < b.Length; i++)
{
if (b[i] == (byte)'^')
{
if (_i1 == -1)
{
_i1 = i;
}
else if (_i2 == -1)
{
_i2 = i;
}
else if (_i3 == -1)
{
_i3 = i;
}
else if (_i4 == -1)
{
_i4 = i;
}
}
Console.WriteLine(b[i].ToString());
}
Console.WriteLine("-------------------------------");
if (_i1 == 0 && _i4 == b.Length - 1 && _i3 != -1 && _i2 != -1)
{
Console.WriteLine("_i1={0}", _i1);
Console.WriteLine("_i2={0}", _i2);
Console.WriteLine("_i3={0}", _i3);
Console.WriteLine("_i4={0}", _i4);
byte[] bFlag = new byte[_i2 - _i1 - 1];
for (int i = _i1 + 1; i < _i2; i++)
{
bFlag[i - _i1 - 1] = b[i];
}
string _flag = UTF32Encoding.UTF8.GetString(bFlag);
byte[] bName = new byte[_i3 - _i2 - 1];
for (int i = _i2 + 1; i < _i3; i++)
{
bName[i - _i2 - 1] = b[i];
}
string _name = UTF32Encoding.UTF8.GetString(bName);
byte[] bContent = new byte[_i4 - _i3 - 1];
for (int i = _i3 + 1; i < _i4; i++)
{
bContent[i - _i3 - 1] = b[i];
}
string _content = UTF32Encoding.UTF8.GetString(bContent);//如果是文件的话,用其它办法还原成流,近而变成文件或图片
Console.WriteLine("-------------------------------");
Console.WriteLine("类型:" + _flag);
Console.WriteLine("用户:" + _name);
Console.WriteLine("内容:" + _content);
}
Console.Read();
}
}
}
运行结果如下:
94
116
120
116
94
106
105
109
109
121
94
72
101
108
108
111
94
-------------------------------
_i1=0
_i2=4
_i3=10
_i4=16
-------------------------------
类型:txt
用户:jimmy
内容:Hello