📄 chatfrm.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.Threading;
using System.IO;
namespace LtsClient
{
/// <summary>
/// chatFrm 的摘要说明。
/// </summary>
public class chatFrm : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button CloseWin;
private System.Windows.Forms.Button ViewText;
private System.Windows.Forms.TextBox PortStr;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button ExitBtn;
private System.Windows.Forms.Button OpenBtn;
private System.Windows.Forms.Button SendBtn;
private System.Windows.Forms.TextBox RevStr;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox SendStr;
private System.Windows.Forms.TextBox IPStr;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public chatFrm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.CloseWin = new System.Windows.Forms.Button();
this.ViewText = new System.Windows.Forms.Button();
this.PortStr = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.ExitBtn = new System.Windows.Forms.Button();
this.OpenBtn = new System.Windows.Forms.Button();
this.SendBtn = new System.Windows.Forms.Button();
this.RevStr = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.SendStr = new System.Windows.Forms.TextBox();
this.IPStr = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox2
//
this.groupBox2.Controls.Add(this.CloseWin);
this.groupBox2.Controls.Add(this.ViewText);
this.groupBox2.Controls.Add(this.PortStr);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Controls.Add(this.ExitBtn);
this.groupBox2.Controls.Add(this.OpenBtn);
this.groupBox2.Controls.Add(this.SendBtn);
this.groupBox2.Controls.Add(this.RevStr);
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.SendStr);
this.groupBox2.Controls.Add(this.IPStr);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(0, 4);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(365, 285);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Enter += new System.EventHandler(this.groupBox2_Enter);
//
// CloseWin
//
this.CloseWin.Location = new System.Drawing.Point(307, 250);
this.CloseWin.Name = "CloseWin";
this.CloseWin.Size = new System.Drawing.Size(58, 25);
this.CloseWin.TabIndex = 22;
this.CloseWin.Text = "退出";
this.CloseWin.Click += new System.EventHandler(this.CloseWin_Click);
//
// ViewText
//
this.ViewText.Enabled = false;
this.ViewText.Location = new System.Drawing.Point(96, 250);
this.ViewText.Name = "ViewText";
this.ViewText.Size = new System.Drawing.Size(67, 25);
this.ViewText.TabIndex = 21;
this.ViewText.Text = "纪录(&T)";
this.ViewText.Click += new System.EventHandler(this.ViewText_Click);
//
// PortStr
//
this.PortStr.Location = new System.Drawing.Point(278, 17);
this.PortStr.Name = "PortStr";
this.PortStr.Size = new System.Drawing.Size(77, 21);
this.PortStr.TabIndex = 20;
this.PortStr.Text = "6868";
//
// label4
//
this.label4.Location = new System.Drawing.Point(240, 17);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(38, 17);
this.label4.TabIndex = 19;
this.label4.Text = "Port";
//
// ExitBtn
//
this.ExitBtn.Enabled = false;
this.ExitBtn.Location = new System.Drawing.Point(240, 250);
this.ExitBtn.Name = "ExitBtn";
this.ExitBtn.Size = new System.Drawing.Size(67, 25);
this.ExitBtn.TabIndex = 18;
this.ExitBtn.Text = "停止(&E)";
this.ExitBtn.Click += new System.EventHandler(this.ExitBtn_Click);
//
// OpenBtn
//
this.OpenBtn.Location = new System.Drawing.Point(163, 250);
this.OpenBtn.Name = "OpenBtn";
this.OpenBtn.Size = new System.Drawing.Size(77, 25);
this.OpenBtn.TabIndex = 17;
this.OpenBtn.Text = "启动(&O)";
this.OpenBtn.Click += new System.EventHandler(this.OpenBtn_Click);
//
// SendBtn
//
this.SendBtn.Location = new System.Drawing.Point(29, 250);
this.SendBtn.Name = "SendBtn";
this.SendBtn.Size = new System.Drawing.Size(67, 25);
this.SendBtn.TabIndex = 16;
this.SendBtn.Text = "发送(&S)";
this.SendBtn.Click += new System.EventHandler(this.SendBtn_Click);
//
// RevStr
//
this.RevStr.BackColor = System.Drawing.Color.White;
this.RevStr.Location = new System.Drawing.Point(29, 146);
this.RevStr.Multiline = true;
this.RevStr.Name = "RevStr";
this.RevStr.ReadOnly = true;
this.RevStr.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.RevStr.Size = new System.Drawing.Size(326, 104);
this.RevStr.TabIndex = 15;
this.RevStr.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(10, 146);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(57, 25);
this.label3.TabIndex = 14;
this.label3.Text = "收";
//
// SendStr
//
this.SendStr.Location = new System.Drawing.Point(29, 43);
this.SendStr.MaxLength = 100;
this.SendStr.Multiline = true;
this.SendStr.Name = "SendStr";
this.SendStr.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.SendStr.Size = new System.Drawing.Size(326, 103);
this.SendStr.TabIndex = 13;
this.SendStr.Text = "";
//
// IPStr
//
this.IPStr.Location = new System.Drawing.Point(29, 17);
this.IPStr.Name = "IPStr";
this.IPStr.Size = new System.Drawing.Size(201, 21);
this.IPStr.TabIndex = 12;
this.IPStr.Text = "192.168.0.0";
//
// label2
//
this.label2.Location = new System.Drawing.Point(10, 43);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 17);
this.label2.TabIndex = 11;
this.label2.Text = "发";
//
// label1
//
this.label1.Location = new System.Drawing.Point(10, 17);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 17);
this.label1.TabIndex = 10;
this.label1.Text = "IP";
//
// chatFrm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(374, 294);
this.Controls.Add(this.groupBox2);
this.Name = "chatFrm";
this.Text = "chatFrm";
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void CloseBtn_Click(object sender, System.EventArgs e)
{
this.Close();
}//退出
private string RFileName="ChatRecord.Txt";
private void ViewText_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start(RFileName);
}//看纪录
public void Wfile(string filename,string filecont)
{
FileStream fs=new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write);
StreamWriter wfile=new StreamWriter(fs);
wfile.BaseStream.Seek(0,SeekOrigin.End);
wfile.WriteLine(filecont);
wfile.Flush();
wfile.Close();
}//写纪录
private TcpListener Ilistener;
private Thread Iprocessor;
private Socket Iclientsocket2;
private Thread Iclientservice;
private void ExitBtn_Click(object sender, System.EventArgs e)
{
if(Iprocessor.IsAlive)Iprocessor.Abort();
Ilistener.Stop();
RevStr.Text="["+System.DateTime.Now+"]:终止服务\n";
OpenBtn.Enabled=true;//启动键有效
ExitBtn.Enabled=false;//停止键无效
ViewText.Enabled=false;//纪录有效
SendBtn.Enabled=false;//发送有效
}//停止
private void OpenBtn_Click(object sender, System.EventArgs e)
{
Iprocessor = new Thread(new ThreadStart(IStartListening));
Iprocessor.Start();
RevStr.Text="["+System.DateTime.Now+"]:启动服务\n";
if(File.Exists(RFileName))File.Delete(RFileName);
Wfile(RFileName,RevStr.Text);
OpenBtn.Enabled=false;//启动键无效
ExitBtn.Enabled=true;//停止键有效
ViewText.Enabled=true;//纪录有效
SendBtn.Enabled=true;//发送有效
}//启动
private void IStartListening()
{
int PortNumber=6868;
if(PortStr.Text.ToString().Trim()!="")PortNumber=int.Parse(PortStr.Text.ToString());
Ilistener = new TcpListener(PortNumber);
try
{
Ilistener.Start();
}
catch(Exception e)
{
if(e!=null)
{
MessageBox.Show("无法打开端口","Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
ExitBtn_Click(null,null);
}
}
while (Iprocessor.IsAlive)
{
try
{
Iclientsocket2 = Ilistener.AcceptSocket();
Iclientservice = new Thread(new ThreadStart(IServiceClient));
Iclientservice.Start();
}
catch(Exception e)
{
if(e!=null)
{
MessageBox.Show("无法建立端口服务,服务终止","Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if(Iclientservice.IsAlive)Iclientservice.Abort();
break;
}
}
}
}//启动服务
private void IServiceClient()
{
bool Ikeepalive=true;
int RCharNum=256;
string ReceiveMsg ="";
if ((Iclientsocket2.Connected)&&(Ikeepalive))
{
try
{
Byte[] buffer = new Byte[RCharNum];
Iclientsocket2.Receive(buffer,0,RCharNum,SocketFlags.None);
string Iclientcommand = System.Text.Encoding.Unicode.GetString(buffer);
ReceiveMsg = Iclientcommand;
}
catch(Exception e)
{
if(e!=null)MessageBox.Show("传输出错"+e.ToString());
Ikeepalive=false;
}
}
RevStr.Text = "["+System.DateTime.Now+"]收:"+ReceiveMsg;
Wfile(RFileName,RevStr.Text);
Iclientsocket2.Shutdown(SocketShutdown.Both);
Iclientsocket2.Close();
}//接收
private NetworkStream ns;
private TcpClient clientsocket;
private System.Net.IPAddress UserIP=new System.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address);
private void SendBtn_Click(object sender, System.EventArgs e)
{
int ToPortStr=6868;
if(PortStr.Text.ToString().Trim()!="")ToPortStr=int.Parse(PortStr.Text.ToString());
string ToIpStr=IPStr.Text;
string ToCont=SendStr.Text.ToString();
string ToContentStr=UserIP.ToString()+ToCont;
SendStr.Text="";
try
{
clientsocket = new TcpClient(ToIpStr,ToPortStr);
clientsocket.SendTimeout=1000;
clientsocket.ReceiveTimeout=1000;
clientsocket.ReceiveBufferSize=256;
ns = clientsocket.GetStream();
//
Byte[] outbytes = System.Text.Encoding.Unicode.GetBytes(ToContentStr.ToCharArray());
int WriteLength=(int)clientsocket.ReceiveBufferSize;
if(outbytes.Length<WriteLength)WriteLength=outbytes.Length;
ns.Write(outbytes,0,outbytes.Length);
RevStr.Text = "["+System.DateTime.Now+"]发:"+ToContentStr;
Wfile(RFileName,RevStr.Text);
clientsocket.Close();
}
catch(Exception ex)
{
if(ex!=null)MessageBox.Show("无法连接对方机器或者未启动服务","Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}//发送
private void CloseWin_Click(object sender, System.EventArgs e)
{
CloseBtn_Click(sender,e);
}
private void groupBox2_Enter(object sender, System.EventArgs e)
{
}//退出
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -