📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;//用到网络编程所需要的命名空间;
using System.IO;
using System.Threading;//多线程编程;
namespace P2Pchat
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Thread th;//定义一个线程对象;
private TcpListener tcp1;//监听;
private bool listenerRun=true;//循环结束条件;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.StatusBar statusBar1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private void Listen()//实现监听功能;
{
try
{
IPAddress ipAd=IPAddress.Parse(this.textBox1.Text.Trim());//实例化一个网络IP地址;
tcp1=new TcpListener (ipAd,5656);//初始化监听器;
tcp1.Start();//开始监听服务器端口;
this.statusBar1.Text="正在监听...";
while(listenerRun)
{
Socket s=tcp1.AcceptSocket();//用S表示新建立的套接字;
byte[] stream=new byte [100];//用于接收客户端信息;
int i=s.Receive(stream);//接收字节,并存入缓冲区;
string message=System.Text.Encoding.UTF8.GetString(stream);//返回一个包含UTF-8格式的字符编码的字符串;
this.richTextBox1.AppendText(message);//追加文本;
}
}
catch(System.Security.SecurityException)//异常处理;
{
MessageBox.Show("防火墙安全错误!","Error");// 开启了防火墙;
}
catch(Exception)//停止监听;
{
this.statusBar1.Text="已停止监听!";
}
}
private void Send()//发送消息;
{
try
{
string msg="<"+this.textBox3.Text+">"+this.textBox2.Text;//消息内容和昵称;
TcpClient tcpc=new TcpClient (this.textBox1.Text,5656);//建立客户端套接字并初始化;
NetworkStream tcpStream=tcpc.GetStream();//NetworkStream提供网络防问的数据流;
StreamWriter reqStreamW=new StreamWriter (tcpStream);//以一种特定的编码写入数据流;
reqStreamW.Write(msg);//将字符串写入数据流;
reqStreamW.Flush();//清理当前编写器的缓冲区,并将缓冲区数据写入基础流;
tcpStream.Close();//关闭流;
tcpc.Close();//关闭连接;
this.richTextBox1.AppendText(msg);
this.textBox3.Clear();
}
catch (Exception)
{
this.statusBar1.Text="目标计算机拒绝连接请求!";
}
}
private void Stop()
{
tcp1.Stop();
th.Abort();//终止线程;
}
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <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()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "目标地址:";
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "昵称:";
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(128, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(128, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(200, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Location = new System.Drawing.Point(8, 88);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 4;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button2.Location = new System.Drawing.Point(8, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 23);
this.button2.TabIndex = 5;
this.button2.Text = "停止监听";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button3.Location = new System.Drawing.Point(8, 152);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 23);
this.button3.TabIndex = 6;
this.button3.Text = "发送消息";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox3
//
this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox3.Location = new System.Drawing.Point(128, 88);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(200, 88);
this.textBox3.TabIndex = 7;
this.textBox3.Text = "";
//
// label3
//
this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label3.Location = new System.Drawing.Point(24, 184);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 16);
this.label3.TabIndex = 8;
this.label3.Text = "聊天记录:";
//
// richTextBox1
//
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBox1.Location = new System.Drawing.Point(8, 200);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(320, 96);
this.richTextBox1.TabIndex = 9;
this.richTextBox1.Text = "";
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 304);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(344, 22);
this.statusBar1.TabIndex = 10;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 326);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
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.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
this.button1.Enabled=false;
this.button2.Enabled=true;
th=new Thread (new ThreadStart(Listen));
th.Start();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.button1.Enabled=true;
this.button2.Enabled=false;
listenerRun=false;
Stop();
}
private void button3_Click(object sender, System.EventArgs e)
{
Send();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -