Asp.net Socket客戶端(遠程發(fā)送和接收數(shù)據(jù))
更新時間:2008年11月10日 13:19:51 作者:
通過Socket遠程發(fā)送與接收數(shù)據(jù)的代碼類
復(fù)制代碼 代碼如下:
/***************************************
* 對象名稱: SocketObj
* 功能說明: 遠程發(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)建新對象
* o.Connection(url, port); //打開遠程端口
* 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 遠程發(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ù)實例
- c#(Socket)同步套接字代碼示例
- C#實現(xiàn)的Socket服務(wù)器端、客戶端代碼分享
- C#中使用Socket獲取網(wǎng)頁源代碼的代碼
- C#中異步Socket通信編程代碼實例
- C#之Socket操作類實例解析
- 使用C#開發(fā)Socket通訊的方法
- C#使用Socket實現(xiàn)發(fā)送和接收圖片的方法
- C# Socket網(wǎng)絡(luò)編程實例
- c# socket編程udp客戶端實現(xiàn)代碼分享
- asp.net使用Socket.Send發(fā)送信息及Socket.SendFile傳輸文件的方法
相關(guān)文章
.NET實現(xiàn)Repeater控件+AspNetPager控件分頁
本文給大家分享的2個示例,演示AspNetPager最基本的功能,幫助您認識AspNetPager分頁控件及了解它的工作原理。有需要的小伙伴可以參考下2015-11-11
asp.net 大文件上傳 之 改版了的SlickUpload.HttpUploadModule(Krystalware
以下代碼中所注釋的部分是所改版的地方。:) Krystalware.SlickUpload.dll2009-05-05
asp.net 在global中攔截404錯誤的實現(xiàn)方法
asp.net 在global中攔截404錯誤,增加用于體驗,不會因為提示找不到信息而直接退出的尷尬。2010-03-03
asp.net 服務(wù)器控件的 ID,ClientID,UniqueID 的區(qū)別
asp.net 服務(wù)器控件的 ID,ClientID,UniqueID 的區(qū)別分析,需要的朋友可以參考下。2010-04-04

