.net2003 C# 基于pda的winsock通讯
源代码在线查看: message.cs
using System;
using System.Text;
using System.Data.SqlServerCe;
using System.Collections;
using System.Windows.Forms;
namespace SmartDeviceApplication5
{
public class Message
{
public enum CommandHeader:byte
{
SendMessage = 1
}
public Connection SendToOrReceivedFrom;
public int MessageLength =0;
public CommandHeader Command;
public byte MainVersion =1;
public byte SecondVersion=1;
public string MessageBody;
public bool Sent;
public Message()
{
SendToOrReceivedFrom = null;
Sent = false;
}
public Message(CommandHeader command,byte mainVersion,byte secondVersion,string messageBody):this()
{
this.Command = command;
this.MainVersion = mainVersion;
this.SecondVersion = secondVersion;
this.MessageBody = messageBody;
}
public Message(string messageBody):this()
{
this.MessageBody = messageBody;
}
public static void ShowErrors(SqlCeException e)
{
SqlCeErrorCollection errorCollection = e.Errors;
StringBuilder bld = new StringBuilder();
foreach (SqlCeError err in errorCollection)
{
bld.Append("\n Error Code: " + err.HResult.ToString("X"));
bld.Append("\n Message : " + err.Message);
bld.Append("\n Minor Err.: " + err.NativeError);
bld.Append("\n Source : " + err.Source);
foreach (int numPar in err.NumericErrorParameters)
{
if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
}
foreach (string errPar in err.ErrorParameters)
{
if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
}
MessageBox.Show(bld.ToString());
bld.Remove(0, bld.Length);
}
}
public byte[] ToBytes()
{
////////////////////////
//this.MessageLength = 7 + SocketFactory.DefaultEncoding.GetByteCount(this.MessageBody);//计算消息总长度。消息头长度为7加上消息体的长度。
//byte[] buffer = new byte[this.MessageLength];
////先将长度的4个字节写入到数组中。
//BitConverter.GetBytes(this.MessageLength).CopyTo(buffer,0);
////将CommandHeader写入到数组中
//buffer[4] = (byte)this.Command;
////将主版本号写入到数组中
//buffer[5] = (byte)this.MainVersion;
////将次版本号写入到数组中
//buffer[6] = (byte)this.SecondVersion;
////消息头已写完,现在写消息体。
//byte[] body = new byte[this.MessageLength - 7];
//SocketFactory.DefaultEncoding.GetBytes(this.MessageBody).CopyTo(buffer,7);
//byte[] buffer = Encoding.ASCII.GetBytes(this.MessageBody);
byte[] buffer = new byte[this.MessageBody.Length * 2];
buffer = System.Text.Encoding.Unicode.GetBytes(this.MessageBody);
return buffer;
}
public static Message Parse(Connection connection)
{
Message message = new Message();
int numberOfBytesRead;
byte[] myReadBuffer = new byte[102400];
string myCompleteMessage ="";
do
{
numberOfBytesRead = connection.NetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
//myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
myCompleteMessage = String.Concat(myCompleteMessage, System.Text.Encoding.Unicode.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while(connection.NetworkStream.DataAvailable);
/////////////////////////////
//先读出前四个字节,即Message长度
//byte[] buffer = new byte[4];
//if(connection.NetworkStream.DataAvailable)
//{
// int count = connection.NetworkStream.Read(buffer,0,4);
// if(count == 4) {
// message.MessageLength = BitConverter.ToInt32(buffer,0);
// }
// else
// throw new Exception("网络流长度不正确");
//}
//else
// throw new Exception("目前网络不可读");
//读出消息的其它字节
//buffer = new byte[message.MessageLength - 4];
//if(connection.NetworkStream.DataAvailable) {
// int count = connection.NetworkStream.Read(buffer,0,buffer.Length);
// if(count == message.MessageLength -4) {
// message.Command = (CommandHeader)buffer[0];
// message.MainVersion = buffer[1];
// message.SecondVersion = buffer[2];
//读出消息体
// message.MessageBody = SocketFactory.DefaultEncoding.GetString(buffer,3,buffer.Length - 3);
// message.SendToOrReceivedFrom = connection;
// return message;
// }
// else
// throw new Exception("网络流长度不正确");
//}
//else
// throw new Exception("目前网络不可读");
/////////////////////////
message.SendToOrReceivedFrom = connection;
message.MessageBody = myCompleteMessage;
return message;
}
}
}