复制代码 代码如下:
/***************************************
* 对象名称: 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
}
}
/***************************************
* 对象名称: 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
}
}
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]