C#——文件传输

一、

1、发送端

[csharp] view plaincopyprint?

  1.         string path = “E:\\fuxin.txt”;
  2.         TcpClient tcpClient = new TcpClient();
  3.         tcpClient.Connect(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);
  4.         FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  5.         BinaryReader binaryReader = new BinaryReader(fileStream);
  6.         byte[] data = new byte[4098];
  7.         int count;
  8.         Console.WriteLine(“正在发送文件”);
  9.         while (0 != (count = binaryReader.Read(data, 0, 4098)))
  10.             tcpClient.Client.Send(data, count, SocketFlags.None);  //发送文件流到目标机器
  11.         tcpClient.Client.Shutdown(SocketShutdown.Both);
  12.         binaryReader.Close();
  13.         fileStream.Close();
  14.         Console.ReadLine();

TcpClient tcpClient = new TcpClient();

tcpClient.Connect(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);

FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

BinaryReader binaryReader = new BinaryReader(fileStream);

byte[] data = new byte[4098];

int count;

Console.WriteLine(“正在发送文件”);

while (0 != (count = binaryReader.Read(data, 0, 4098)))

tcpClient.Client.Send(data, count, SocketFlags.None);  //发送文件流到目标机器

tcpClient.Client.Shutdown(SocketShutdown.Both);

binaryReader.Close();

fileStream.Close();

Console.ReadLine();

 

2、接收端

[csharp] view plaincopyprint?

  1. string path = “D:\\fuxin.cs”;
  2. TcpListener tcpListener = new TcpListener(50000);
  3. tcpListener.Start();
  4. Console.WriteLine(“等待接受”);
  5. Socket socket =tcpListener.AcceptSocket();
  6. FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  7. BinaryWriter binaryWriter = new BinaryWriter(fileStream);
  8. int count;
  9. byte[] data = new byte[4098];
  10. while (0 != (count = socket.Receive(data, 4098, SocketFlags.None)))
  11.     binaryWriter.Write(data, 0, count);//将接收的流用写成文件
  12. binaryWriter.Close();
  13. fileStream.Close();
  14. socket.Close();
  15. tcpListener.Stop();
  16. Console.WriteLine(“文件传输完毕”);
  17. Console.ReadLine();

TcpListener tcpListener = new TcpListener(50000);

tcpListener.Start();

Console.WriteLine(“等待接受”);

Socket socket =tcpListener.AcceptSocket();

FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

BinaryWriter binaryWriter = new BinaryWriter(fileStream);

int count;

byte[] data = new byte[4098];

while (0 != (count = socket.Receive(data, 4098, SocketFlags.None)))

binaryWriter.Write(data, 0, count);//将接收的流用写成文件

binaryWriter.Close();

fileStream.Close();

socket.Close();

tcpListener.Stop();

Console.WriteLine(“文件传输完毕”);

Console.ReadLine();

 

 

二、用Socket实现

1、发送端

[csharp] view plaincopyprint?

  1.         string path = “E:\\fuxin.txt”;
  2.         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3.         socket.Connect(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);
  4.         FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  5.         BinaryReader binaryReader = new BinaryReader(fileStream);
  6.         byte[] data = new byte[4098];
  7.         int count;
  8.         Console.WriteLine(“正在发送文件”);
  9.         while (0 != (count = binaryReader.Read(data, 0, 4098)))
  10.             socket.Send(data, count, SocketFlags.None);  //发送文件流到目标机器
  11.         socket.Shutdown(SocketShutdown.Both);
  12.         binaryReader.Close();
  13.         fileStream.Close();
  14.         Console.ReadLine();

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socket.Connect(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);

FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

BinaryReader binaryReader = new BinaryReader(fileStream);

byte[] data = new byte[4098];

int count;

Console.WriteLine(“正在发送文件”);

while (0 != (count = binaryReader.Read(data, 0, 4098)))

socket.Send(data, count, SocketFlags.None);  //发送文件流到目标机器

socket.Shutdown(SocketShutdown.Both);

binaryReader.Close();

fileStream.Close();

Console.ReadLine();

 

2、接收端

[csharp] view plaincopyprint?

  1. string path = “D:\\fuxin.cs”;
  2. IPEndPoint ipep = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);
  3. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  4. socket.Bind(ipep);
  5. socket.Listen(10);
  6. Console.WriteLine(“等待接受”);
  7. Socket newsocket = socket.Accept();
  8. FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  9. BinaryWriter binaryWriter = new BinaryWriter(fileStream);
  10. int count;
  11. byte[] data = new byte[4098];
  12. while (0 != (count = newsocket.Receive(data, 4098, SocketFlags.None)))
  13.     binaryWriter.Write(data, 0, count);//将接收的流用写成文件
  14. binaryWriter.Close();
  15. fileStream.Close();
  16. socket.Close();
  17. newsocket.Close();
  18. Console.WriteLine(“文件传输完毕”);
  19. Console.ReadLine();

IPEndPoint ipep = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socket.Bind(ipep);

socket.Listen(10);

Console.WriteLine(“等待接受”);

Socket newsocket = socket.Accept();

FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

BinaryWriter binaryWriter = new BinaryWriter(fileStream);

int count;

byte[] data = new byte[4098];

while (0 != (count = newsocket.Receive(data, 4098, SocketFlags.None)))

binaryWriter.Write(data, 0, count);//将接收的流用写成文件

binaryWriter.Close();

fileStream.Close();

socket.Close();

newsocket.Close();

Console.WriteLine(“文件传输完毕”);

Console.ReadLine();

 

3、发送端另一种精简版

[csharp] view plaincopyprint?

  1. string path = “E:\\fuxin.txt”;
  2. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  3. socket.Connect(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);
  4. Console.WriteLine(“正在发送文件”);
  5. socket.SendFile(path);
  6. socket.Shutdown(SocketShutdown.Both);

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socket.Connect(Dns.GetHostAddresses(Dns.GetHostName())[0], 50000);

Console.WriteLine(“正在发送文件”);

socket.SendFile(path);

socket.Shutdown(SocketShutdown.Both);

来源URL:http://blog.csdn.net/hellofuxin/article/details/7173543