国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:21:31
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:


/***************************************
* 對象名稱: SocketObj
* 功能說明: 遠(yuǎn)程發(fā)送與接收
* 試用示例:
* using EC; //引用空間名
* string url = "218.75.111.74"; // URL也可以是()這種形式
* 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)建新對象
* o.Connection(url, port); //打開遠(yuǎn)程端口
* o.Send(SendStr); //發(fā)送數(shù)據(jù)
* Response.Write(o.Recev()); //接收數(shù)據(jù)
* o.Dispose(); //銷毀對象
**********************************************/
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>URL或者:IP地址</param>
/// <param></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></param>
/// <param></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></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
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金平| 漳平市| 青海省| 漳州市| 济宁市| 小金县| 天峨县| 临夏市| 三明市| 庆云县| 郸城县| 江阴市| 老河口市| 旬邑县| 鹤岗市| 兴海县| 江门市| 怀集县| 德令哈市| 通许县| 成安县| 泸定县| 罗山县| 扎赉特旗| 榆林市| 秦安县| 沁源县| 阳新县| 衡南县| 石家庄市| 泰兴市| 建瓯市| 永靖县| 吉安县| 桦川县| 福泉市| 嵊州市| 大田县| 乌拉特后旗| 三都| 石家庄市|