📄 form1.cs
字号:
//
this.label8.Location = new System.Drawing.Point(32, 56);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(48, 16);
this.label8.TabIndex = 3;
this.label8.Text = "目标:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(256, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 23);
this.button2.TabIndex = 2;
this.button2.Text = "浏览";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(88, 16);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(144, 21);
this.textBox4.TabIndex = 1;
this.textBox4.Text = "";
//
// label7
//
this.label7.Location = new System.Drawing.Point(24, 24);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(56, 16);
this.label7.TabIndex = 0;
this.label7.Text = "源文件:";
//
// label10
//
this.label10.Location = new System.Drawing.Point(8, 88);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(80, 16);
this.label10.TabIndex = 26;
this.label10.Text = "文件传送端口";
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(224, 424);
this.listBox1.TabIndex = 24;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label9
//
this.label9.Location = new System.Drawing.Point(8, 0);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(72, 16);
this.label9.TabIndex = 25;
this.label9.Text = "远程主机:";
//
// Form1
//
this.AcceptButton = this.button1;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 461);
this.Controls.Add(this.label9);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label4);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Random rm=new Random();
PortS=7485;
IP=Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
this.Text="本地IP:"+IP+" 开放端口:"+PortS;
//获得IP地址对象
IPAddress ipAdd=IPAddress.Parse(IP);
//封装IP地址端
IPEndPoint endPoint=new IPEndPoint(ipAdd,PortS);
//创建服务器端Socket
socketS=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//绑定端口
socketS.Bind(endPoint);
td1=new Thread(new ThreadStart(Start));
td1.Start();
}
#region Start()
void Start()
{
while(true)
{
//打开端口监听
socketS.Listen(10);
//接受远程连接
socketR=socketS.Accept();
//创建缓存数组
byte[] dataC=new byte[1024];
//接收数据
socketR.Receive(dataC,0,dataC.Length,SocketFlags.None);
//将数据转换为字符
text=Encoding.UTF8.GetString(dataC);
if(text.LastIndexOf("&")>0) //含有&字符则代表着有主机连上来了
{
listBox1.Items.Add(text);
}
else
{
//显示所接收的内容
richTextBox1.AppendText(text);
richTextBox1.AppendText("\r");
}
}
}
#endregion
#region 发送消息
private void button1_Click(object sender, System.EventArgs e)
{
Send(textBox3.Text);
}
void Send(string message)
{
try
{
//远程主机IP及端口
ip=IPAddress.Parse(textBox1.Text);
PortC=int.Parse(textBox2.Text);
//封装服务器的IP,端口
IPEndPoint endPoint=new IPEndPoint(ip,PortC);
//创建客户端Socket
socketC=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//连接服务器端
socketC.Connect(endPoint);
//将发送的聊天内容显示出来
richTextBox1.AppendText("发送:"+IP+"-"+message+"-"+PortS+"\r");
//把聊天内容转换为数据流发送给服务器端
byte[] dataS=Encoding.UTF8.GetBytes(IP+"-"+message+"-"+PortS);
socketC.Send(dataS,0,dataS.Length,SocketFlags.None);
}
catch
{
MessageBox.Show("IP地址或端口号不正确!","??错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
//跳出Catch结构继续下面的代码
return ;
}
}
#endregion
private void linkLabel1_Click(object sender, System.EventArgs e)
{
Form2 f2=new Form2();
f2.Show();
}
private void button3_Click(object sender, System.EventArgs e) //扫描远程主机的端口
{
richTextBox1.AppendText("\r");
//起用线程
td2=new Thread(new ThreadStart(ScenProcess));
td2.Start();
}
#region ScenProcess()
void ScenProcess()
{
int i,j;
i=Convert.ToInt32(numericUpDown1.Value);
j=Convert.ToInt32(numericUpDown2.Value);
for(;i<=j;i++)
{
try
{
//创建Tcp连接对象
TcpClient tcp=new TcpClient();
//创建IP地址对象
ip=IPAddress.Parse(textBox1.Text);
//连接到服务器
tcp.Connect(ip,i);
//关闭连接
tcp.Close();
richTextBox1.AppendText(" "+"端口"+i+"连接成功!"+"\r");
}
catch
{
richTextBox1.AppendText("端口"+i+"连接失败!"+"\r");
}
}
}
#endregion
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//在监听端口时会启用一个线程一直挂着socketS
//所以就算关掉了窗口,但进程仍然还在
//我们就需要先关掉socketS
socketS.Close();
//然后关掉窗体的同时停掉所有的进程
Application.Exit();
}
private void button2_Click(object sender, System.EventArgs e) //获得源文件路径
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox4.Text=openFileDialog1.FileName;
}
}
string FilePath;
private void button4_Click(object sender, System.EventArgs e) //获得目标文件路径
{
FilePath=textBox5.Text;
PathFile();
// new Thread(new ThreadStart(PathFile)).Start();
}
void PathFile()
{
try
{
// //远程主机IP及端口
// ip=IPAddress.Parse(textBox1.Text);
// PortC=int.Parse(textBox2.Text);
//
// //封装服务器的IP,端口
// IPEndPoint endPoint=new IPEndPoint(ip,PortC);
// //创建客户端Socket
// socketC=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
// //连接服务器端
// socketC.Connect(endPoint);
// //将发送的聊天内容显示出来
// richTextBox1.AppendText("目标文件夹已确定为:"+FilePath+"\r");
// //把聊天内容转换为数据流发送给服务器端
// byte[] dataS=Encoding.UTF8.GetBytes("ClientFilePath"+"-"+FilePath);
// socketC.Send(dataS,0,dataS.Length,SocketFlags.None);
Send("ClientFilePath"+"-"+FilePath);
}
catch
{
MessageBox.Show("IP地址或端口号不正确!","??错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
//跳出Catch结构继续下面的代码
return ;
}
}
#region 流传输
private void button5_Click(object sender, System.EventArgs e) //发送文件
{
IPAddress ip=IPAddress.Parse(textBox1.Text);
int PortFile=Convert.ToInt32(textBox6.Text);
//封装服务器的IP,端口
IPEndPoint endPoint=new IPEndPoint(ip,PortFile);
//创建客户端Socket
socketC=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//连接服务器端
socketC.Connect(endPoint);
//把聊天内容转换为数据流发送给服务器端
FileStream fsSource =new FileStream(textBox4.Text,FileMode.Open,FileAccess.Read);
byte[] buffer =new byte[255];
int read=fsSource.Read(buffer,0,buffer.Length);
while(read>0)
{
read=fsSource.Read(buffer,0,buffer.Length);
}
fsSource.Close();
socketC.Send(buffer,0,buffer.Length,SocketFlags.None);
}
#endregion
#region 下面的代码是转送文件夹的
//下面的代码是转送文件夹的!!
// DirectoryInfo dif1=new DirectoryInfo(textBox1.Text);
// DirectoryInfo dif2=new DirectoryInfo(textBox2.Text);
//
// FileInfo[] inputs=dif1.GetFiles();
// FileInfo[] outputs=dif2.GetFiles();
//
// System.IO.Stream inputStream=null;
// System.IO.Stream outputStream=null;
// foreach(FileInfo k in inputs)
// {
// //inputStream目录一定要和outputStream目录一致,不能有丝毫的差错;
// inputStream = File.OpenRead(textBox1.Text+@"\"+k.ToString());
// //这个只允许一个文件夹中包含多个文件,但不允许一个文件夹中还有文件夹!
// outputStream = File.OpenWrite(textBox2.Text+@"\"+k.ToString());
//
// const int SizeBuff = 255;
// byte[] buffer = new Byte[SizeBuff];
//
// int bytesRead;
// bytesRead = inputStream.Read(buffer,0,SizeBuff);
// while( bytesRead>0 )
// {
// outputStream.Write(buffer,0,bytesRead);
// Console.WriteLine("Bytes read: {0}", bytesRead);
// bytesRead = inputStream.Read(buffer,0,SizeBuff);
// }
//
// }
// inputStream.Close();
// outputStream.Close();
#endregion
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string item=listBox1.SelectedItem.ToString();
string[] str=text.Split('&');
textBox1.Text=str[0];
textBox2.Text=str[1];
textBox6.Text=str[2];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -