Asp.net Socket用戶端(遠程發送和接收資料)

來源:互聯網
上載者:User

複製代碼 代碼如下:/***************************************
* 對象名稱: SocketObj
* 功能說明: 遠程發送與接收
* 試用樣本:
* using EC; //引用空間名
* string url = "218.75.111.74"; // URL也可以是(http://www.baidu.com/)這種形式
* int port = 8000; //連接埠
* string SendStr = "domainname\n"; //組織要發送的字串
* SendStr += "check\n";
* SendStr += "entityname:domains\n";
* SendStr += "domainname:" + this.TextBox1.Text + "\n";
* SendStr += ".\n";
* EBSocketObj o = new SocketObj(); //建立新對象
* o.Connection(url, port); //開啟遠程連接埠
* o.Send(SendStr); //發送資料
* Response.Write(o.Recev()); //接收資料
* o.Dispose(); //銷毀對象
**********************************************/
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace EC
{
/// <summary>
/// Socket 遠程發送與接收
/// </summary>
public class SocketObj
{
private NetworkStream ns;
private bool _alreadyDispose = false;
#region 構造與釋構
public EBSocketObj()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
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 發送Socket
/// <summary>
/// 發送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
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.