Asp.net Socket客戶端(遠(yuǎn)程發(fā)送和接收數(shù)據(jù))
更新時(shí)間:2008年11月10日 13:19:51 作者:
通過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
}
}
您可能感興趣的文章:
- c#(Socket)異步套接字代碼示例
- C#使用Socket發(fā)送和接收TCP數(shù)據(jù)實(shí)例
- c#(Socket)同步套接字代碼示例
- C#實(shí)現(xiàn)的Socket服務(wù)器端、客戶端代碼分享
- C#中使用Socket獲取網(wǎng)頁源代碼的代碼
- C#中異步Socket通信編程代碼實(shí)例
- C#之Socket操作類實(shí)例解析
- 使用C#開發(fā)Socket通訊的方法
- C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法
- C# Socket網(wǎng)絡(luò)編程實(shí)例
- c# socket編程udp客戶端實(shí)現(xiàn)代碼分享
- asp.net使用Socket.Send發(fā)送信息及Socket.SendFile傳輸文件的方法
相關(guān)文章
基于Asp.Net MVC4 Bundle捆綁壓縮技術(shù)的介紹
本篇文章,小編將為大家介紹,Asp.Net MVC4 Bundle捆綁壓縮技術(shù),有需要的朋友可以參考一下2013-04-04支持Ajax跨域訪問ASP.NET Web Api 2(Cors)的示例教程
這篇文章主要介紹了支持Ajax跨域訪問ASP.NET Web Api 2(Cors)的示例教程,需要的朋友可以參考下2016-04-04.NET微服務(wù)架構(gòu)CI/CD自動(dòng)構(gòu)建Jenkins+Gitee
這篇文章介紹了.NET使用微服務(wù)架構(gòu)CI/CD自動(dòng)構(gòu)建Jenkins+Gitee的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01asp.net后臺(tái)彈窗如何實(shí)現(xiàn)
這篇文章主要介紹了asp.net后臺(tái)彈窗如何實(shí)現(xiàn),需要的朋友可以參考下2014-02-02ASP.NET MVC HttpPostedFileBase文件上傳的實(shí)例代碼
這篇文章主要介紹了ASP.NET MVC HttpPostedFileBase文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼
asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-12-12asp.net B2B網(wǎng)站對(duì)接支付寶接口
首先,網(wǎng)上購(gòu)物系統(tǒng)必須與支付寶公司簽訂合作協(xié)議,以確保從本購(gòu)物網(wǎng)站上傳到2010-06-06