CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

请问哪位有二进制文件的传输程序

楼主ddwinter(骑士)2003-12-03 17:54:47 在 .NET技术 / C# 提问

因为工作需要,我要做一个Server/Client的文件传输程序,要求:  
  能传文本文件,二进制文件  
   
  我查看了很多资料,说是要用TCP通讯。请问哪位有源码或是相关文章? 问题点数:0、回复次数:3Top

1 楼Javadays(流星雨)回复于 2003-12-04 08:30:34 得分 0

我也在关注这个问题:我写了一段代码,但不是很好,仅供参考:  
  [Server]  
   
  [Client]  
  using   System.Collections   ;  
  using   System.ComponentModel   ;  
  using   System.Windows.Forms   ;  
  using   System.Data   ;  
  using   System.Net   ;  
  using   System;  
  using   System.Xml;  
  using   System.IO;  
  using   System.Text;  
  using   System.Windows;  
  using   System.Net.Sockets   ;  
  using   System.Runtime.InteropServices   ;  
  //程序引入WinAPI函数要使用到  
   
   
  namespace   UDPClient  
  {  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
   
  private   UdpClient   client   ;  
  //创建UDP网络服务  
  private   IPEndPoint   receivePoint   ;  
  private   int   port   =   9000   ;    
  //定义接收服务器端程序发送对时信息对应的端口号  
  private   string   timeString   =   DateTime.Now.ToString     (   )   ;  
  //存放时间日期信息字符串  
  private   System.DateTime     temp   ;  
  private   System.Windows.Forms.RichTextBox   RbMessage;  
  //定义一个时间类型,用以修改当前时间和日期  
  private   System.ComponentModel.Container   components   =   null;  
   
  public   Form1()  
  {  
  InitializeComponent();  
  }  
   
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if   (components   !=   null)    
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  [STAThread]  
   
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
   
  private   void   button1_Click   (   object   sender   ,   System.EventArgs   e   )  
  {  
  StartClient()   ;  
  }  
   
  private   void   StartClient()  
  {  
  client   =   new   UdpClient   (   port   )   ;  
  IPAddress   a   =   IPAddress.Parse     (   "127001"   )   ;  
  receivePoint   =   new   IPEndPoint   (   a     ,   port   )   ;  
  IPAddress   HostIP   ;  
  bool   continueLoop   =   true   ;  
  while   (   continueLoop   )  
  {  
  string   hostName   =   Dns.GetHostName   (   )   ;  
  System.Text.ASCIIEncoding   encode   =   new   System.Text.ASCIIEncoding   (   )   ;  
  //定义发送到服务器端的请求信息  
  //请求信息是一个字符串,为客户端名称和接收服务器反馈信息的端口号组成的字符串  
  string   TargetStr   =   "";  
  StringToSend(out   TargetStr);  
  string   sendString   =   hostName   +   "/"   +   port.ToString   (   )   ;  
   
  byte[]   sendData   =   encode.GetBytes   (   sendString   )   ;  
  //判断使用者输入的是IP地址还是计算机名称  
  try  
  {  
  HostIP   =   IPAddress.Parse     (   textBox3.Text   )   ;  
  }  
  catch  
  {  
  //如果输入的是计算机名称,则按照执行下列代码。  
  //发送请求信息  
  //client.Send   (   sendData   ,   sendData.Length   ,   textBox3.Text   ,   port   )   ;  
  client.Send   (   encode.GetBytes(TargetStr),   TargetStr.Length   ,   textBox3.Text   ,   port   )   ;  
  //接收来自服务器端的信息  
  byte[]   recData   =   client.Receive   (   ref   receivePoint   )   ;  
  timeString   =   encode.GetString   (   recData   )   ;  
  client.Close   (   )   ;  
  continueLoop=false   ;  
  return   ;  
  }  
  //输入的是IP地址,则执行下列代码  
  IPEndPoint   host   =   new   IPEndPoint   (   HostIP   ,port   )   ;  
  //发送请求信息  
  //client.Send   (   sendData   ,   sendData.Length   ,   host   )   ;  
  client.Send   (   encode.GetBytes(TargetStr),   TargetStr.Length   ,   textBox3.Text   ,   port   )   ;  
   
  //接收来自服务器端的信息  
  byte[]   recData1   =   client.Receive   (   ref   receivePoint   )   ;  
  //获取服务器端的时间和日期  
  timeString   =   encode.GetString   (   recData1   )   ;  
  client.Close   (   )   ;  
  //退出循环  
  continueLoop=false   ;  
  }  
  }  
   
  private   void   button2_Click   (   object   sender   ,   System.EventArgs   e   )  
  {  
  StartClient   (   )   ;  
  //把接收来的数据转换时间日期格式  
  try  
  {  
  temp   =   DateTime.Parse     (   timeString   )   ;  
  }  
  catch  
  {  
  MessageBox.Show     (   "错误时间"   )   ;  
  return   ;  
  }  
  //根据得到的时间日期,来定义时间、日期  
  SystemTime   st=   new   SystemTime   (   )   ;  
  st.year=     (   short   )temp.Year   ;  
  st.Month=   (   short   )temp.Month   ;  
  st.DayOfWeek=   (   short   )temp.DayOfWeek   ;  
  st.Day=   (   short   )temp.Day   ;  
  st.Hour=Convert.ToInt16   (   temp.Hour   )   ;  
  if   (   st.Hour>=12   )  
  {  
  st.Hour-=   (   short   )8   ;  
  }  
  else   if   (   st.Hour   >=   8   )  
  {  
  st.Hour-=   (   short   )8   ;  
  }  
  else  
  {  
  st.Hour+=   (   short   )16   ;  
  }  
  st.Minute=Convert.ToInt16   (   temp.Minute   )   ;  
  st.Second=Convert.ToInt16   (   temp.Second   )   ;  
  st.Milliseconds=Convert.ToInt16   (   temp.Millisecond   )   ;  
  //修改本地端的时间和日期  
  if   (   SetSystemTime   (   st   )   )  
  {  
  MessageBox.Show   (   DateTime.Now.ToString   (   )   ,"修改成功"   )   ;  
  }    
  else  
  MessageBox.Show   (   "不成功!"   ,"不成功"   )   ;  
  }  
   
  private   void   StringToSend(out   string   TargetStr)  
  {  
  TargetStr   ="";  
  OpenFileDialog   MyDlg   =   new   OpenFileDialog();  
  try  
  {  
  if   (   MyDlg.ShowDialog   (   )   ==   DialogResult.OK   )  
  {  
  FileStream   fs   =   new   FileStream   (   MyDlg.FileName     ,   FileMode.Open   ,   FileAccess.Read   )   ;  
  TargetStr   =   fs.ToString();  
  StreamReader   m_streamReader   =   new   StreamReader   (   fs,System.Text.Encoding.GetEncoding("GB2312"))   ;   //使用StreamReader类来读取文件  
  m_streamReader.BaseStream.Seek   (   0   ,   SeekOrigin.Begin   )   ;//   从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容  
  RbMessage.Text   =   ""   ;  
  string   strLine   =   m_streamReader.ReadLine   (   )   ;  
  while   (   strLine   !=   null   )  
  {  
  RbMessage.Text   +=   strLine   +   "\r\n"   ;  
  strLine   =   m_streamReader.ReadLine   (   )   ;  
  }  
  //关闭此StreamReader对象  
  m_streamReader.Close   (   )   ;  
  TargetStr   =   RbMessage.Text;  
  }    
  }  
  catch   (   Exception   em   )  
  {  
  MessageBox.Show   (   em.Message.ToString   (   )   )   ;  
  }  
  }  
   
  private   void   StringToSave()  
  {  
  SaveFileDialog   MyDlg   =   new   SaveFileDialog();  
  try  
  {  
  //获得另存为的文件名称  
  if   (   MyDlg.ShowDialog   (   )   ==   DialogResult.OK   )  
  {    
  //创建一个文件流,用以写入或者创建一个StreamWriter  
  FileStream   fs   =   new   FileStream   (   @MyDlg.FileName     ,   FileMode.OpenOrCreate   ,   FileAccess.Write   )   ;  
  StreamWriter   m_streamWriter   =   new   StreamWriter   (   fs   )   ;  
  m_streamWriter.Flush   (   )   ; //   使用StreamWriter来往文件中写入内容  
  m_streamWriter.BaseStream.Seek   (   0   ,   SeekOrigin.Begin   )   ; //   把richTextBox1中的内容写入文件  
  m_streamWriter.Write   (   RbMessage.Text   )   ; //关闭此文件  
  m_streamWriter.Flush   (   )   ;  
  m_streamWriter.Close   (   )   ;  
  }  
  }  
  catch   (   Exception   em   )  
  {  
  Console.WriteLine   (   em.Message.ToString   (   )   )   ;  
  }  
   
  }  
   
  Top

2 楼szsbell()回复于 2003-12-04 09:03:51 得分 0

upTop

3 楼ddwinter(骑士)回复于 2003-12-05 11:03:54 得分 0

你用的是UDP通讯,不过还是谢谢。希望有机会好好交流。Top

相关问题

  • socket传输二进制文件输出是不是要用DataOutputStream?
  • 怎样用winsock将文件用二进制传输?
  • 二进制文件?
  • 使用VB开发CGI程序怎么提交二进制文件呢?
  • 请问谁可以帮助编一个二进制文件合并的程序
  • 二进制文件加密程序,欢迎提出修改意见!!
  • python的程序可以编译成二进制可执行文件么?
  • 如何把一个二进制文件编译到程序当中
  • 如何将一个二进制的文件嵌入到c程序或库中?
  • 关于二进制文件

关键词

  • .net
  • 文件
  • 服务器
  • client
  • 信息
  • recdata
  • targetstr
  • receivepoint
  • 程序
  • senddata

得分解答快速导航

  • 帖主:ddwinter

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo