一、
1、发送端
- string path = “E:\\fuxin.txt”;
- 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();
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、接收端
- string path = “D:\\fuxin.cs”;
- 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();
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、发送端
- string path = “E:\\fuxin.txt”;
- 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();
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、接收端
- string path = “D:\\fuxin.cs”;
- 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();
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、发送端另一种精简版
- string path = “E:\\fuxin.txt”;
- 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);
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