设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> 网络编程 >> C#应用 >> 使用C#开发一个简单的P2P应用
精品推荐
C#应用点击TOP10
·c#操作word表格
·用C#实现木马程序
·C#操作xml文件入门
·C#编写的windows计算器-源代码
·C#反射实例讲解
·在C#中应用哈希表(Hashtable)
·C#中连接DataTable的方法
·c#中的委托和事件的简单实例
·C#实现Word中表格信息读取
·C# 2.0 套接字编程实例初探
网络编程点击TOP10
·ASP.NET 程序中常用的三十三种代码
·CHK文件恢复工具
·.NET 初 级 读 本
·c#操作word表格
·『原创』C#中TreeView类操作全攻略:建立树,新增节点,删除节点,修改节点,拖动节点,与Oracle数据库交互操作(一)
·我的.NET书架 (入门篇)
·从零开始学ASP.NET(基础篇)
·十天学会ASP.net之第一天
·用C#实现木马程序
·官方水晶报表 .NET 应用程序实例下载 (C#、Visual C++.NET)
精选专题

使用C#开发一个简单的P2P应用

作者: 来源:http://www.xgdown.com/ 时间:2005-11-5 0:08:52

使用C#开发一个简单的P2P应用(2) FileStream fs = new FileStream(sbFileName2.ToString(), FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
byte[] bytes = new byte[1024];
int read;
while((read = reader.Read(bytes, 0, bytes.Length)) != 0)
{
DataStream.Write(bytes, 0, read);
}
reader.Close();
DataStream.Flush();
DataStream.Close();
}
}
catch(SocketException ex)
{
MessageBox.Show(ex.ToString());
}
}

public void DownloadToClient(String server, string remotefilename, string localfilename)
{
try
{
TcpClient tcpc = new TcpClient();
Byte[] read = new Byte[1024];

OptionsLoader ol = new OptionsLoader();
int port = 0;
if (ol.Port > 0)
{
port = ol.Port;
}
else
{
// 缺省的端口号,可以设置为使用的端口号
port = 8081;
}


// 尝试与服务器连接
IPHostEntry IPHost = Dns.Resolve(server);
string []aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;

IPEndPoint ep = new IPEndPoint(addr[0], port);
tcpc.Connect(ep);

// 获得流对象
Stream s = tcpc.GetStream();
Byte[] b = Encoding.ASCII.GetBytes(remotefilename.ToCharArray());
s.Write( b, 0, b.Length );
int bytes;
FileStream fs = new FileStream(localfilename, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs);

// 读取流对象,并将其转换为ASCII码
while( (bytes = s.Read(read, 0, read.Length)) != 0)
{
w.Write(read, 0, bytes);
read = new Byte[1024];
}

tcpc.Close();
w.Close();
fs.Close();
}
catch(Exception ex)
{
throw new Exception(ex.ToString());
}
}
}
}

共2页 9 7 [1] [28 :>

使用C#开发一个简单的P2P应用 相关文章:
使用C#开发一个简单的P2P应用 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com