📄 net.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net.Sockets;
using System.IO;
namespace WindowsApplication1
{
/// <summary>
/// Net 的摘要说明。
/// </summary>
public class Net : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox richTextBox3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox7;
private System.Windows.Forms.TextBox textBox1;
//声明监听线程对象
private Thread MyListenThread;
//声明网络监听对象
private TcpListener MyTcpListener;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Net()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void StartListen()
{
MyTcpListener=new TcpListener(5000);
//开始监听
MyTcpListener.Start();
while(true)
{//获取TcpClient
TcpClient MyTcpClient=MyTcpListener.AcceptTcpClient();
NetworkStream MyStream=MyTcpClient.GetStream();
byte[] MyBytes = new byte[1024];
int MyBytesRead=MyStream.Read(MyBytes,0,MyBytes.Length);
string MyMessage=System.Text.Encoding.Default.GetString(MyBytes,0,MyBytesRead);
this.richTextBox2.Text+=MyMessage;
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Net));
this.label2 = new System.Windows.Forms.Label();
this.textBox7 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.richTextBox3 = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.label6 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label7 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label2
//
this.label2.Location = new System.Drawing.Point(188, -32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 18);
this.label2.TabIndex = 13;
this.label2.Text = "姓名:";
//
// textBox7
//
this.textBox7.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox7.ForeColor = System.Drawing.Color.Red;
this.textBox7.Location = new System.Drawing.Point(12, -40);
this.textBox7.Name = "textBox7";
this.textBox7.Size = new System.Drawing.Size(168, 26);
this.textBox7.TabIndex = 12;
this.textBox7.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(-92, -32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 18);
this.label1.TabIndex = 11;
this.label1.Text = "接收主机IP地址:";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.textBox2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox2.ForeColor = System.Drawing.Color.Red;
this.textBox2.Location = new System.Drawing.Point(344, 24);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(144, 26);
this.textBox2.TabIndex = 17;
this.textBox2.Text = "";
//
// label3
//
this.label3.BackColor = System.Drawing.Color.Transparent;
this.label3.Location = new System.Drawing.Point(296, 32);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 16);
this.label3.TabIndex = 16;
this.label3.Text = "姓名:";
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox1.ForeColor = System.Drawing.Color.Red;
this.textBox1.Location = new System.Drawing.Point(120, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 26);
this.textBox1.TabIndex = 15;
this.textBox1.Text = "";
//
// label4
//
this.label4.BackColor = System.Drawing.Color.Transparent;
this.label4.Location = new System.Drawing.Point(16, 32);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(104, 16);
this.label4.TabIndex = 14;
this.label4.Text = "接收主机IP地址:";
this.label4.Click += new System.EventHandler(this.label4_Click);
//
// richTextBox3
//
this.richTextBox3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.richTextBox3.ForeColor = System.Drawing.Color.Blue;
this.richTextBox3.Location = new System.Drawing.Point(16, 80);
this.richTextBox3.Name = "richTextBox3";
this.richTextBox3.ReadOnly = true;
this.richTextBox3.Size = new System.Drawing.Size(232, 152);
this.richTextBox3.TabIndex = 19;
this.richTextBox3.Text = "";
//
// label5
//
this.label5.BackColor = System.Drawing.Color.Transparent;
this.label5.Location = new System.Drawing.Point(48, 64);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(112, 16);
this.label5.TabIndex = 18;
this.label5.Text = "已经发送的内容:";
//
// richTextBox2
//
this.richTextBox2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.richTextBox2.ForeColor = System.Drawing.Color.Red;
this.richTextBox2.Location = new System.Drawing.Point(288, 80);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.ReadOnly = true;
this.richTextBox2.Size = new System.Drawing.Size(208, 152);
this.richTextBox2.TabIndex = 21;
this.richTextBox2.Text = "";
//
// label6
//
this.label6.BackColor = System.Drawing.Color.Transparent;
this.label6.Location = new System.Drawing.Point(280, 64);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(112, 16);
this.label6.TabIndex = 20;
this.label6.Text = "已经收到的内容:";
//
// richTextBox1
//
this.richTextBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.richTextBox1.Location = new System.Drawing.Point(16, 264);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(480, 88);
this.richTextBox1.TabIndex = 23;
this.richTextBox1.Text = "";
//
// label7
//
this.label7.BackColor = System.Drawing.Color.Transparent;
this.label7.Location = new System.Drawing.Point(48, 248);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(128, 16);
this.label7.TabIndex = 22;
this.label7.Text = "将要发送的内容:";
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Transparent;
this.button1.Location = new System.Drawing.Point(424, 360);
this.button1.Name = "button1";
this.button1.TabIndex = 24;
this.button1.Text = "发送";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Net
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(520, 392);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label7);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.label6);
this.Controls.Add(this.richTextBox3);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox7);
this.Controls.Add(this.label1);
this.Name = "Net";
this.Text = "Net";
this.Load += new System.EventHandler(this.Net_Load);
this.Closed += new System.EventHandler(this.Net_Closed);
this.ResumeLayout(false);
}
#endregion
private void Net_Load(object sender, System.EventArgs e)
{
//开始监听
//创建监听线程
this.MyListenThread= new Thread(new ThreadStart(StartListen));
//启动线程
this.MyListenThread.Start();
}
private void label4_Click(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
//发送聊天内容
if(this.textBox1.Text.Length<1||this.textBox2.Text.Length<1||this.richTextBox1.Text.Length<1)
return;
try
{
string MyMessage=DateTime.Now.ToString()+"\n"+this.textBox2.Text + ":" + this.richTextBox1.Text + "\n";
//根据目标计算机地址建立连接
TcpClient MyTcpClient = new TcpClient(this.textBox1.Text, 5000);
//获得用于网络访问的数据流
NetworkStream MyTcpStream =MyTcpClient.GetStream();
StreamWriter MyStream = new StreamWriter(MyTcpStream);
//将字符串写入流
MyStream.Write(MyMessage,System.Text.Encoding.Default);
//将缓冲数据写入基础流
MyStream.Flush();
//关闭网络流
MyStream.Close();
MyTcpClient.Close();
this.richTextBox3.AppendText(MyMessage);
this.richTextBox1.Clear();
}
catch(Exception Err)
{
MessageBox.Show(Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void Net_Closed(object sender, System.EventArgs e)
{
//关闭SOCKET
try
{
if (this.MyTcpListener!=null)
{//关闭监听器
this.MyTcpListener.Stop();
}
if (this.MyListenThread!=null)
{ //如果线程还处于运行状态就关闭它
if (this.MyListenThread.ThreadState==ThreadState.Running)
{
this.MyListenThread.Abort();
}
}
}
catch(Exception Err)
{
MessageBox.Show(Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -