RTRTRTRT

解决方案 »

  1.   

    msdn上就有现成的代码,难道楼主就不知道吗?下面的代码示例创建并连接一个套接字。文件“test.txt”位于本地计算机的根目录中。在此示例中,我们将创建数据的预缓冲区和后缓冲区,并将它们随该文件发送到远程主机。在此,使用了默认的 TransmitFileOptions。C#  复制代码 
    // Establish the local endpoint for the socket.
    IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
    IPAddress  ipAddr = ipHost.AddressList[0];
    IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);// Create a TCP socket.
    Socket client = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);// Connect the socket to the remote endpoint.
    client.Connect(ipEndPoint);// Send file fileName to the remote host with preBuffer and postBuffer data.
    // There is a text file test.txt located in the root directory.
    string fileName = "C:\\test.txt";
           
    // Create the preBuffer data.
    string string1 = String.Format("This is text data that precedes the file.{0}", Environment.NewLine);
    byte[] preBuf = Encoding.ASCII.GetBytes(string1);// Create the postBuffer data.
    string string2 = String.Format("This is text data that will follow the file.{0}", Environment.NewLine);
    byte[] postBuf = Encoding.ASCII.GetBytes(string2);//Send file fileName with buffers and default flags to the remote device.
    Console.WriteLine("Sending {0} with buffers to the host.{1}", fileName, Environment.NewLine);
    client.SendFile(fileName, preBuf, postBuf, TransmitFileOptions.UseDefaultWorkerThread);// Release the socket.
    client.Shutdown(SocketShutdown.Both);
    client.Close();