📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Text;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private TcpListener tlListen1;
private Thread th;
private bool listenerun=true;
private NetworkStream tcpStream;
private StreamWriter reqstreamW;
private TcpClient tcpc;
private Socket skSocket;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.Label label7;
private System.ComponentModel.Container components = null;
private void Listen ( )
{
try
{
tlListen1 = new TcpListener ( Int32.Parse(textBox2.Text));
tlListen1.Start ( ) ;
statusBar1.Panels[1].Text = "正在监听..." ;
skSocket = tlListen1.AcceptSocket ( ) ;
EndPoint tempRemoteEP = skSocket.RemoteEndPoint ;
IPEndPoint tempRemoteIP = ( IPEndPoint )tempRemoteEP ;
IPHostEntry host = Dns.GetHostByAddress
( tempRemoteIP.Address ) ;
string HostName = host.HostName ;
statusBar1.Panels[1].Text = "'" + HostName +"' " ;
while (listenerun)
{
Byte[] stream = new Byte[80] ;
string time = DateTime.Now.ToString ( ) ;
int i = skSocket.ReceiveFrom ( stream, ref tempRemoteEP ) ;
string sMessage = System.Text.Encoding.UTF8.
GetString ( stream ) ;
listBox2.Items.Add(time+""+HostName+":");
listBox2.Items.Add ( sMessage ) ;
}
}
catch ( System.Security.SecurityException )
{
MessageBox.Show ( "防火墙安全错误!" ,"错误" , MessageBoxButtons.OK , MessageBoxIcon.Exclamation) ;
}
}
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
try
{
listenerun = false ;
th.Abort ( ) ;
th = null ;
tlListen1.Stop ( ) ;
skSocket.Close ( ) ;
tcpc.Close ( ) ;
}
catch { }
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(448, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 64);
this.button1.TabIndex = 0;
this.button1.Text = "连接远程主机";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(584, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 64);
this.button2.TabIndex = 1;
this.button2.Text = "监听端口";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(448, 104);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 64);
this.button3.TabIndex = 2;
this.button3.Text = "发送信息";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Enabled = false;
this.button4.Location = new System.Drawing.Point(584, 104);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 64);
this.button4.TabIndex = 3;
this.button4.Text = "短开连接";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(128, 232);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(240, 112);
this.listBox1.TabIndex = 4;
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(464, 232);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(224, 112);
this.listBox2.TabIndex = 5;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(168, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(168, 80);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(184, 21);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(168, 128);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(184, 21);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(168, 168);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(224, 21);
this.textBox4.TabIndex = 9;
this.textBox4.Text = "";
//
// statusBar1
//
this.statusBar1.AllowDrop = true;
this.statusBar1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.statusBar1.Location = new System.Drawing.Point(0, 359);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2});
this.statusBar1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(728, 22);
this.statusBar1.SizingGrip = false;
this.statusBar1.TabIndex = 100;
this.statusBar1.TabStop = true;
//
// statusBarPanel1
//
this.statusBarPanel1.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
this.statusBarPanel1.Width = 312;
//
// statusBarPanel2
//
this.statusBarPanel2.Width = 400;
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 32);
this.label1.TabIndex = 11;
this.label1.Text = "远程主机ip地址";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(32, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 23);
this.label2.TabIndex = 12;
this.label2.Text = "远程端口";
//
// label3
//
this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(32, 128);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 13;
this.label3.Text = "本地端口";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(32, 168);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 14;
this.label4.Text = "发送信息";
//
// label5
//
this.label5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label5.Location = new System.Drawing.Point(24, 224);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(96, 23);
this.label5.TabIndex = 15;
this.label5.Text = "发送的信息";
//
// label6
//
this.label6.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label6.Location = new System.Drawing.Point(376, 232);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(80, 23);
this.label6.TabIndex = 16;
this.label6.Text = "接收的信息";
//
// label7
//
this.label7.Location = new System.Drawing.Point(472, 192);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(152, 23);
this.label7.TabIndex = 101;
this.label7.Text = "label7";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(728, 381);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "t2t程序";
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
tcpc = new TcpClient ( textBox1.Text ,
Int32.Parse ( textBox3.Text ) ) ;
tcpStream = tcpc.GetStream ( ) ;
statusBar1.Panels[0].Text="成功连接远程计算机!" ;
button2.Enabled = true ;
button1.Enabled = false ;
button4.Enabled = true ;
}
catch ( Exception )
{
statusBar1.Panels[0].Text = "目标计算机拒绝连接请求!" ;
}
}
private void button4_Click(object sender, System.EventArgs e)
{
listenerun = false ;
tcpc.Close ( ) ;
statusBar1.Panels[0].Text = "断开连接!" ;
button1.Enabled = true ;
button2.Enabled = false ;
button4.Enabled = false ;
}
private void button2_Click(object sender, System.EventArgs e)
{
th = new Thread ( new ThreadStart ( Listen ) ) ;
th.Start ( ) ;
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
string sMsg = textBox4.Text ;
string MyName = Dns.GetHostName ( ) ;
label7.Text=sMsg;
reqstreamW = new StreamWriter(tcpStream) ;
reqstreamW.Write (sMsg) ;
reqstreamW.Flush ( ) ;
string time = DateTime.Now.ToString ( ) ;
listBox1.Items.Add ( time +" " + MyName +":" ) ;
listBox1.Items.Add (sMsg ) ;
textBox4.Clear ( ) ;
}
catch ( Exception )
{
statusBar1.Panels[0].Text = "无法发送信息到目标计算机!";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -