我有个问题想请教下:
有计算机A 和计算机B,A上存有   一些.txt的文件  但是非常大估计能有1M或更大,然后计算机B要发送指令  给A
,比如发送  123    A就需要把123.txt发送到B的指定目录下面,怎么实现思路?   文件过大 不方便读入数据库然后共享然后读出  会耗费大量时间 前提是不能把A拿硬盘全考给B 然后数据库存路径~~~~
求解决办法    SOCKET可以吗?  SOCKET可以发送文件吗?   A上是否应该建立服务?  还有.NET的服务程序怎么建立?

解决方案 »

  1.   

    知道2台电脑的路径,可以直接用file.copy()来做
      

  2.   

    如何传输:在B机器中做个磁盘映射,映射到A机器存有这些txt的磁盘,然后B机器上面跑的程序就像操作自己机器硬盘一样,想要什么文件直接去哪就行了。
    .net服务器程序:不知道楼主说的是不是window服务的意思,.net建议windows服务还是比较方便,具体的可以上网搜索一下,说不太清楚。
      

  3.   

    System.Sockes命名空间了实现 Berkeley 套接字接口。通过这个类,我们可以实现网络计算机之间的消息传输和发送.而在我下面要讨论的这个议题里,我们将讨论的是用套节子实现文件的传输.这种方法有别于FTP协议实现的的文件传输方法,利用ftp的方法需要一个专门的服务器和客户端,无疑于我们要实现的点对点的文件传输太为复杂了一些。在这里,我们实现一个轻量级的方法来实现点对点的文件传输,这样就达到了intenet上任何两个计算机的文件共享。在两台计算机传输文件之前,必需得先有一台计算机建立套节子连接并绑定一个固定得端口,并在这个端口侦听另外一台计算机的连接请求。socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
    socket.Blocking = true ;
    IPEndPoint computernode1 = new IPEndPoint(serverIpadress, 8080);socket.Bind(computernode1);socket.Listen(-1);当有其他的计算机发出连接请求的时候,被请求的计算机将对每一个连接请求分配一个线程,用于处理文件传输和其他服务。while ( true )   {     clientsock = socket.Accept();     if ( clientsock.Connected )     {        Thread tc = new Thread(new ThreadStart(listenclient));       tc.Start();     }   }下面的代码展示了listenclient方法是如何处理另外一台计算机发送过来的请求。首先并对发送过来的请求字符串作出判断,看看是何种请求,然后决定相应的处理方法。void listenclient()
    {
       Socket sock = clientsock ;
       try
       {
         while ( sock != null ) 
         {
           byte[] recs = new byte[32767];
           int rcount = sock.Receive(recs,recs.Length,0) ;
           string message = System.Text.Encoding.ASCII.GetString(recs) ;
          //对message作出处理,解析处请求字符和参数存储在cmdList 中
               execmd=cmdList[0];
           sender = null ;
           sender = new Byte[32767];
      
           string parm1 = "";
    //目录列举     
    if ( execmd == "LISTING" ) 
           {
             ListFiles(message);
             continue ;
           } 
    //文件传输
           if ( execmd == "GETOK" )
           {
             cmd = "BEGINSEND "   + filepath + " " + filesize ;
             sender = new Byte[1024];
             sender = Encoding.ASCII.GetBytes(cmd);
             sock.Send(sender, sender.Length , 0 );
                    //转到文件下载处理
             DownloadingFile(sock);
             continue ;
           }   
         }
       }
       catch(Exception Se)
       {
         string s = Se.Message;
         Console.WriteLine(s);
       }
    }至此,基本的工作已经完成了,下面我们看看如何处理文件传输的。
    while(rdby < total && nfs.CanWrite)
       {
    //从要传输的文件读取指定长度的数据
    len =fin.Read(buffed,0,buffed.Length) ;
         //将读取的数据发送到对应的计算机
         nfs.Write(buffed, 0,len);
         //增加已经发送的长度
         rdby=rdby+len ;          
       }
    从上面的代码可以看出是完成文件转换成FileStream 流,然后通过NetworkStream绑定对应的套节子,最后调用他的write方法发送到对应的计算机。
    我们再看看接受端是如何接受传输过来的流,并且转换成文件的:
    NetworkStream nfs = new NetworkStream(sock) ;
    try
    {
             //一直循环直到指定的文件长度
             while(rby < size)
       {
           byte[] buffer = new byte[1024] ;
           //读取发送过来的文件流
           int i = nfs.Read(buffer,0,buffer.Length) ;
           fout.Write(buffer,0,(int)i) ;
           rby=rby+i ;
         }  
       fout.Close() ;从上面可以看出接受与发送恰好是互为相反的过程,非常简单。//取得预保存的文件名 
    string fileName="test.rar"; 
    //远程主机 
    string hostName=TextBoxHost.Text.Trim(); 
    //端口 
    int port=80; 
    //得到主机信息 
    IPHostEntry ipInfo=Dns.GetHostByName(hostName); 
    //取得IPAddress[] 
    IPAddress[] ipAddr=ipInfo.AddressList; 
    //得到ip 
    IPAddress ip=ipAddr[0]; 
    //组合出远程终结点 
    IPEndPoint hostEP=new IPEndPoint(ip,port); 
    //创建Socket 实例 
    Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 
    try 

    //尝试连接 
    socket.Connect(hostEP); 

    catch(Exception se) 

    LeixunCMS.Common.MessageBox.Show(this.Page,"连接错误"+se.Message);}
      

  4.   

    今天终于把文件传输问题给研究明白了,特发此代码用来帮助正在学习的人们。send :
      string path = "E:\\c#\\convey_file\\convey_file\\Form1.cs";                //要传输的文件
               TcpClient client = new TcpClient();
               client.Connect(IPAddress.Parse("192.168.0.52"),9999);
                FileStream file = new FileStream(path,FileMode.Open,FileAccess.Read);  //注意与receive的filestream的区别
                BinaryReader binaryreader = new BinaryReader(file);
                byte[] b = new byte[4098];
                int data;
                Console.WriteLine("正在发送文件");
                while ((data = binaryreader.Read(b, 0, 4098)) != 0)                 //这个注意是将文件写成流的形式
                {
                    client.Client.Send(b,data,SocketFlags.None);           //发送文件流到目标机器
                }
               client.Client.Shutdown(SocketShutdown.Both);
               binaryreader.Close();
               file.Close();
    receive:
     private Socket s;
            TcpListener tl;
            public void lis()
            {
                string path = "d:\\1.cs";                 //要传入文件的路径
                tl = new TcpListener(9999);
                tl.Start();
                Console.WriteLine("等待接受");
                s = tl.AcceptSocket();
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);   //注意这个的属性和send端有所不同
                BinaryWriter binarywrite = new BinaryWriter(fs);
                int count;
                byte[] b = new byte[4098];
                while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0)             //这个是接受文件流
                {
                    binarywrite.Write(b,0,count);                          //将接收的流用写成文件
                }
                binarywrite.Close();
                fs.Close();
                s.Close();
                tl.Stop();
                Console.WriteLine("文件传输完毕");
     
      

  5.   


    今天终于把文件传输问题给研究明白了,特发此代码用来帮助正在学习的人们。send :
      string path = "E:\\c#\\convey_file\\convey_file\\Form1.cs";                //要传输的文件
               TcpClient client = new TcpClient();
               client.Connect(IPAddress.Parse("192.168.0.52"),9999);
                FileStream file = new FileStream(path,FileMode.Open,FileAccess.Read);  //注意与receive的filestream的区别
                BinaryReader binaryreader = new BinaryReader(file);
                byte[] b = new byte[4098];
                int data;
                Console.WriteLine("正在发送文件");
                while ((data = binaryreader.Read(b, 0, 4098)) != 0)                 //这个注意是将文件写成流的形式
                {
                    client.Client.Send(b,data,SocketFlags.None);           //发送文件流到目标机器
                }
               client.Client.Shutdown(SocketShutdown.Both);
               binaryreader.Close();
               file.Close();
    receive:
     private Socket s;
            TcpListener tl;
            public void lis()
            {
                string path = "d:\\1.cs";                 //要传入文件的路径
                tl = new TcpListener(9999);
                tl.Start();
                Console.WriteLine("等待接受");
                s = tl.AcceptSocket();
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);   //注意这个的属性和send端有所不同
                BinaryWriter binarywrite = new BinaryWriter(fs);
                int count;
                byte[] b = new byte[4098];
                while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0)             //这个是接受文件流
                {
                    binarywrite.Write(b,0,count);                          //将接收的流用写成文件
                }
                binarywrite.Close();
                fs.Close();
                s.Close();
                tl.Stop();
                Console.WriteLine("文件传输完毕");
     
      

  6.   

    7楼很牛,其实代码不难,但是.net很少去做这些事情,看起来用C/VC取做这些事情更舒服!