日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

輕松定義自己的網(wǎng)絡(luò)通訊協(xié)議( 二 )


///
/// 網(wǎng)絡(luò)端點(diǎn)
public void Connect(IPEndPoint iep)
{
this.m_Client.Connect(iep);
}
private byte[] CreateArgPackage(object obj)
{
IPEndPoint local = new IPEndPoint(IPAddress.Any, this.m_Port);
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
this.Serializer.Serialize(outStream, new ReceiveObjectEventArgs(obj, local));
return outStream.ToArray();
}
///
/// 將對象發(fā)送到默認(rèn)主機(jī) 。調(diào)用此方法前必須先調(diào)用Connect方法連接默認(rèn)主機(jī) 。
///
/// 要發(fā)送的對象
public void Send(object obj)
{
if (this.IsConnected)
{
byte[] data = https://www.rkxy.com.cn/dnjc/this.CreateArgPackage(obj);
this.m_Client.Send(data, data.Length);
}
else
{
throw new Exception("必須先連接默認(rèn)主機(jī)");
}
}
///
/// 將對象發(fā)送到指定的主機(jī) 。若調(diào)用了Connect方法連接了默認(rèn)主機(jī),則此方法不可用 。
///
/// 要發(fā)送的對象
/// 目標(biāo)主機(jī)的網(wǎng)絡(luò)端點(diǎn)
public void Send(object obj, IPEndPoint remoteIEP)
{
if (this.IsConnected)
{
throw new Exception("已經(jīng)連接了默認(rèn)主機(jī)");
}
else
{
byte[] data = https://www.rkxy.com.cn/dnjc/this.CreateArgPackage(obj);
this.m_Client.Send(data, data.Length, remoteIEP);
}
}
///
/// 監(jiān)聽接收數(shù)據(jù)線程方法
///
protected void Listen()
{
BinaryFormatter Serializer = new BinaryFormatter();
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
object revobj = Serializer.Deserialize(new System.IO.MemoryStream(m_Client.Receive(ref RemoteIpEndPoint)));
ReceiveObjectEventArgs revarg = (ReceiveObjectEventArgs)revobj;
RemoteIpEndPoint.Port = revarg.RemoteIEP.Port;
revarg.RemoteIEP = RemoteIpEndPoint;
if (this.ReceiveObject != null)
{
this.ReceiveObject(this, revarg);
}
}
catch
{
break;
}
}
}
#region 公共屬性區(qū)
///
/// 返回或設(shè)置接收對象的端口號
///
public int Port
{
get
{
return this.m_Port;
}
set
{
this.m_Port = value;
}
}
///
/// 返回對象發(fā)送器是否已經(jīng)初始化并開始工作
///
public bool IsStart
{
get
{
return this.m_IsStart;
}
}
///
/// 返回對象發(fā)送器是否已經(jīng)連接默認(rèn)遠(yuǎn)程主機(jī)
///
public bool IsConnected
{
get
{
return this.m_IsConnected;
}
}
#endregion
#region IDisposable 成員
public void Dispose()
{
// TODO: 添加 ObjectTransferClient.Dispose 實(shí)現(xiàn)
this.m_Client.Close();
this.ListenThread.Abort();
}
#endregion
}
///
/// 接收對象事件參數(shù)
///
[Serializable]
public class ReceiveObjectEventArgs : EventArgs
{
private object _obj;
private System.Net.IPEndPoint _iep;
///
/// 構(gòu)建一個(gè)接收對象事件的參數(shù)
///
/// 接收到的對象
/// 【輕松定義自己的網(wǎng)絡(luò)通訊協(xié)議】發(fā)送者的網(wǎng)絡(luò)端點(diǎn)
internal ReceiveObjectEventArgs(object obj, System.Net.IPEndPoint iep)
{
this._obj = obj;
this._iep = iep;
}
///
/// 構(gòu)建一個(gè)空的接收對象事件參數(shù)
///
public ReceiveObjectEventArgs():this(null, null)
{
}
///
/// 接收到的對象
///
public object Obj
{
get
{
return this._obj;
}
}
///
/// 發(fā)送方的網(wǎng)絡(luò)端點(diǎn)

推薦閱讀