色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Asp.net Socket客戶端(遠(yuǎn)程發(fā)送和接收數(shù)據(jù))

復(fù)制代碼 代碼如下:
/***************************************
* 對(duì)象名稱: SocketObj
* 功能說明: 遠(yuǎn)程發(fā)送與接收
* 試用示例:
* using EC; //引用空間名
* string url = "218.75.111.74"; // URL也可以是(http://www.baidu.com/)這種形式
* int port = 8000; //端口
* string SendStr = "domainname/n"; //組織要發(fā)送的字符串
* SendStr += "check/n";
* SendStr += "entityname:domains/n";
* SendStr += "domainname:" + this.TextBox1.Text + "/n";
* SendStr += "./n";
* EBSocketObj o = new SocketObj(); //創(chuàng)建新對(duì)象
* o.Connection(url, port); //打開遠(yuǎn)程端口
* o.Send(SendStr); //發(fā)送數(shù)據(jù)
* Response.Write(o.Recev()); //接收數(shù)據(jù)
* o.Dispose(); //銷毀對(duì)象
**********************************************/
using System;
using System.IO;
using System.NET;
using System.NET.Sockets;
using System.Text;
namespace EC
{
/// <summary>
/// Socket 遠(yuǎn)程發(fā)送與接收
/// </summary>
public class SocketObj
{
private NETworkStream ns;
private bool _alreadyDispose = false;
#region 構(gòu)造與釋構(gòu)
public EBSocketObj()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public EBSocketObj(string url, int port)
{
Connection(url, port);
}
~EBSocketObj()
{
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
if (ns != null)
{
try
{
ns.Close();
}
catch (Exception E) { }
ns.Dispose();
}
}
_alreadyDispose = true;
}
#endregion
#region IDisposable 成員
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 打開端口
/// <summary>
/// 打開端口
/// </summary>
/// <param name="url">URL或者:IP地址</param>
/// <param name="port"></param>
/// <returns></returns>
public virtual void Connection(string url, int port)
{
if (url == null || url == "") return;
if (port < 0) return;
if (port.ToString()==string.Empty) port = 80;
TcpClient tcp = null;
try
{
tcp = new TcpClient(url, port);
}
catch (Exception E)
{
throw new Exception("Can't connection:" + url);
}
this.ns = tcp.GetStream();
}
#endregion
#region 發(fā)送Socket
/// <summary>
/// 發(fā)送Socket
/// </summary>
/// <param name="ns"></param>
/// <param name="message"></param>
/// <returns></returns>
public virtual bool Send(string message)
{
if (ns == null) return false;
if (message == null || message == "") return false;
byte[] buf = Encoding.ASCII.GetBytes(message);
try
{
ns.Write(buf, 0, buf.Length);
}
catch (Exception E)
{
throw new Exception("Send Date Fail!");
}
return true;
}
#endregion
#region 收取信息
/// <summary>
/// 收取信息
/// </summary>
/// <param name="ns"></param>
/// <returns></returns>
public string Recev()
{
if (ns == null) return null;
byte[] buf = new byte[4096];
int length = 0;
try
{
length = ns.Read(buf, 0, buf.Length);
}
catch (Exception E)
{
throw new Exception("Receive data fail!");
}
return Encoding.ASCII.GetString(buf, 0, length);
}
#endregion
}
}

AspNet技術(shù)Asp.net Socket客戶端(遠(yuǎn)程發(fā)送和接收數(shù)據(jù)),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 清流县| 抚松县| 哈巴河县| 丹江口市| 徐水县| 淮北市| 东明县| 贡嘎县| 承德县| 枞阳县| 湟中县| 蓝山县| 乐山市| 保靖县| 张家界市| 普兰店市| 五家渠市| 渝北区| 永修县| 区。| 故城县| 进贤县| 北海市| 墨玉县| 桃园市| 武邑县| 广河县| 华坪县| 镇原县| 德钦县| 台安县| 新民市| 崇州市| 安义县| 绥滨县| 南部县| 临夏市| 嵊州市| 新宁县| 长治县| 利辛县|