先看看传输使用界面的效果,可以选择发送和接收。
需要同一局域网的电脑先选择IP(IP能自动读取到)开启接收,注意这里需要自己更改一下文件名和存储格式,也可以不改传完再改,然后另一台电脑选择文件发送,之后会存储在选的的文件夹。
现在这个功能比较简单,只能单文件发送,但是其实比U盘硬盘方便,一般的六类网线传输压缩包在50M/s以上,U盘硬盘复制就不行了,特别如果是像我们学校这种网络结构,一个学校都在一个局域网,那真的方便!!!
至于这个小东西为什么会出现,是因为项目上有些特殊的地方限制太多了,连插硬盘都需要关软件,联网又要开很不方便,网络限制一般软件只能局域网通信,还要开特定的端口等,你才能在电脑上通信,这样和机房的服务器远程的时候文件就不好拿过去,又不可能每次都去开机房的门。
普通的局域网用特定IP和端口,通信也是很方便的。
这里提醒一下,局域网的共享文件夹也是可以的,所以这算是比较方便吧,毕竟共享大家都能访问了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
}
private void Form2_Load(object sender, EventArgs e)
{
Get_Local_IPAddress();
comboBox1.SelectedIndex = 0;
radioButton1.Checked = true;
}
public void Get_Local_IPAddress()
{
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
comboBox1.Items.Add(ip.ToString());
}
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
button1.Text = "选择文件";
button2.Text = "开始发送";
textBox3.Text = "";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
button1.Text = "选择位置";
button2.Text = "等待连接";
textBox3.Text = @"C:\1.noname";
}
private void button2_Click(object sender, EventArgs e)
{
string filePath = textBox3.Text;
if (textBox3.Text == "") { MessageBox.Show("检查文件位置!"); return; }
button1.Enabled = false;
button2.Enabled = false;
if (radioButton1.Checked == true)
{
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse(comboBox1.Text), int.Parse(textBox2.Text));
FileStream fileStream = File.OpenRead(@filePath);
NetworkStream networkStream = client.GetStream();//定义网络数据流
using (FileStream fread = new FileStream(@filePath, FileMode.Open, FileAccess.Read))
{
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 5];
int a = fread.Read(buffer, 0, buffer.Length);
if (a == 0) break;
networkStream.Write(buffer, 0, a);
}
}
fileStream.Close();
client.Close();
MessageBox.Show("发送成功!");
button1.Enabled = true;
button2.Enabled = true;
}
else if (radioButton2.Checked == true)
{
TcpListener listener = new TcpListener(IPAddress.Parse(comboBox1.Text), int.Parse(textBox2.Text));// 创建一个TCP监听器并开始侦听连接
listener.Start();
// 接受连接并获取数据
TcpClient client = listener.AcceptTcpClient();
NetworkStream networkStream = client.GetStream();
using (FileStream fwrite = new FileStream(@filePath, FileMode.Create, FileAccess.Write))
{
while (true)
{
byte[] buffer = new byte[client.ReceiveBufferSize];
int a = networkStream.Read(buffer, 0, client.ReceiveBufferSize);
if (a == 0) { fwrite.Close(); break; }
fwrite.Write(buffer, 0, a);
}
}
client.Close();
listener.Stop();
MessageBox.Show("文件已接收并保存在:" + textBox3.Text);
button1.Enabled = true;
button2.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "所有文件(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
textBox3.Text = dialog.FileName;
}
}
else if (radioButton2.Checked == true)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件夹";
dialog.SelectedPath = "C:\\";
if (dialog.ShowDialog() == DialogResult.OK)
{
if (radioButton2.Checked == true)
{
textBox3.Text = dialog.SelectedPath;
}
if (radioButton2.Checked == true)
{
textBox3.Text = dialog.SelectedPath + @"\1.noname";
}
}
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}