⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winall.cs

📁 一个即时通信 Messenger C#代码
💻 CS
字号:
using System;
using System.IO;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace TalkClient
{
	public class WinAll : System.Windows.Forms.Form
	{
		private WinCom WC = new WinCom();
		private WinClient xWinClient;

		private string localID;
		private string clientID;
		private string threadID;
		private string remoteID;

		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.TextBox addrTextBox;
		private System.Windows.Forms.Label addrLabel;
		private System.Windows.Forms.TextBox toTextBox;
		private System.Windows.Forms.Label toLabel;
		private System.Windows.Forms.Button msgButton;
		private System.Windows.Forms.GroupBox topGroupBox;
		private System.Windows.Forms.TextBox showTextBox;
		private System.Windows.Forms.TextBox writeTextBox;
		private System.Windows.Forms.GroupBox bottomGroupBox;

		public WinAll(WinClient tWinClient, string tLocalID, string tClientID)
		{
			xWinClient = tWinClient;
			localID = tLocalID;
			clientID = tClientID;
		}

		public void WinThread()
		{
			this.Initialize();
			this.ShowDialog();
		}

        #region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.bottomGroupBox = new System.Windows.Forms.GroupBox();
			this.writeTextBox = new System.Windows.Forms.TextBox();
			this.msgButton = new System.Windows.Forms.Button();
			this.showTextBox = new System.Windows.Forms.TextBox();
			this.topGroupBox = new System.Windows.Forms.GroupBox();
			this.addrTextBox = new System.Windows.Forms.TextBox();
			this.addrLabel = new System.Windows.Forms.Label();
			this.toTextBox = new System.Windows.Forms.TextBox();
			this.toLabel = new System.Windows.Forms.Label();
			this.bottomGroupBox.SuspendLayout();
			this.topGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// bottomGroupBox
			// 
			this.bottomGroupBox.Controls.AddRange(new System.Windows.Forms.Control[] {
																						 this.writeTextBox,
																						 this.msgButton});
			this.bottomGroupBox.Location = new System.Drawing.Point(8, 168);
			this.bottomGroupBox.Name = "bottomGroupBox";
			this.bottomGroupBox.Size = new System.Drawing.Size(360, 136);
			this.bottomGroupBox.TabIndex = 0;
			this.bottomGroupBox.TabStop = false;
			// 
			// writeTextBox
			// 
			this.writeTextBox.Location = new System.Drawing.Point(8, 16);
			this.writeTextBox.Multiline = true;
			this.writeTextBox.Name = "writeTextBox";
			this.writeTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.writeTextBox.Size = new System.Drawing.Size(344, 80);
			this.writeTextBox.TabIndex = 1;
			this.writeTextBox.Text = "";
			// 
			// msgButton
			// 
			this.msgButton.Location = new System.Drawing.Point(8, 104);
			this.msgButton.Name = "msgButton";
			this.msgButton.Size = new System.Drawing.Size(144, 24);
			this.msgButton.TabIndex = 2;
			this.msgButton.Text = "Send Message";
			this.msgButton.Click += new System.EventHandler(this.msgButton_Click);
			// 
			// showTextBox
			// 
			this.showTextBox.Location = new System.Drawing.Point(8, 72);
			this.showTextBox.Multiline = true;
			this.showTextBox.Name = "showTextBox";
			this.showTextBox.ReadOnly = true;
			this.showTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.showTextBox.Size = new System.Drawing.Size(360, 88);
			this.showTextBox.TabIndex = 0;
			this.showTextBox.Text = "";
			// 
			// topGroupBox
			// 
			this.topGroupBox.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.addrTextBox,
																					  this.addrLabel,
																					  this.toTextBox,
																					  this.toLabel});
			this.topGroupBox.Location = new System.Drawing.Point(8, 8);
			this.topGroupBox.Name = "topGroupBox";
			this.topGroupBox.Size = new System.Drawing.Size(360, 56);
			this.topGroupBox.TabIndex = 0;
			this.topGroupBox.TabStop = false;
			this.topGroupBox.Text = "Client";
			// 
			// addrTextBox
			// 
			this.addrTextBox.Location = new System.Drawing.Point(208, 24);
			this.addrTextBox.Name = "addrTextBox";
			this.addrTextBox.ReadOnly = true;
			this.addrTextBox.Size = new System.Drawing.Size(144, 21);
			this.addrTextBox.TabIndex = 0;
			this.addrTextBox.Text = "";
			// 
			// addrLabel
			// 
			this.addrLabel.Location = new System.Drawing.Point(152, 32);
			this.addrLabel.Name = "addrLabel";
			this.addrLabel.Size = new System.Drawing.Size(56, 16);
			this.addrLabel.TabIndex = 0;
			this.addrLabel.Text = "Address";
			// 
			// toTextBox
			// 
			this.toTextBox.Location = new System.Drawing.Point(32, 24);
			this.toTextBox.Name = "toTextBox";
			this.toTextBox.ReadOnly = true;
			this.toTextBox.Size = new System.Drawing.Size(112, 21);
			this.toTextBox.TabIndex = 0;
			this.toTextBox.Text = "";
			// 
			// toLabel
			// 
			this.toLabel.Location = new System.Drawing.Point(8, 32);
			this.toLabel.Name = "toLabel";
			this.toLabel.Size = new System.Drawing.Size(32, 16);
			this.toLabel.TabIndex = 0;
			this.toLabel.Text = "ID";
			// 
			// WinAll
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(376, 309);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.topGroupBox,
																		  this.bottomGroupBox,
																		  this.showTextBox});
			this.MaximizeBox = false;
			this.Name = "WinAll";
			this.Text = "Aicaca WinDialog";
			this.Closed += new System.EventHandler(this.OnClosed);
			this.Activated += new System.EventHandler(this.OnActivated);
			this.bottomGroupBox.ResumeLayout(false);
			this.topGroupBox.ResumeLayout(false);
			this.ResumeLayout(false);

		}
        #endregion

		private void OnActivated(object sender, System.EventArgs e)
		{
			this.writeTextBox.Focus();
		}

		private void OnClosed(object sender, System.EventArgs e)
		{
			xWinClient.RemoveThread(threadID);
		}

		private void msgButton_Click (object sender, System.EventArgs e)
		{
			this.StartMsg();
		}

		private void Initialize()
		{
			threadID = "WinAll";

			string remoteSort = "WinDialog";
			string remoteName = WC.GetXML(localID, "ln");
			string remoteIP = WC.GetXML(localID, "li");
			string remotePort = WC.GetXML(localID, "lp");
			remoteID = WC.MakeRemoteID(remoteSort, remoteName, remoteIP, remotePort);

			xWinClient.AddThread(threadID, this);
			InitializeComponent();

			this.toTextBox.Text = "All";
			this.addrTextBox.Text = "All";
		}

		private void StartMsg()
		{
			int firstPos = -1;
			int count = 1;

			while ((firstPos = clientID.IndexOf("<cid>", firstPos+1)) != -1)
			{
				string tempID = WC.GetXML(clientID, "cid", firstPos);

				string tempThreadSort = "WinDialog";
				string tempThreadName = WC.GetXML(tempID, "cn");
				string tempThreadIP = WC.GetXML(tempID, "ci");
				string tempThreadPort = WC.GetXML(tempID, "cp");
				string tempThreadID = WC.MakeThreadID(tempThreadSort, tempThreadName, tempThreadIP, tempThreadPort);

				string sendStr = WC.MakeXML("ms", "ACMSG") + tempThreadID + remoteID + "<body>" + this.writeTextBox.Text;

				this.ClientProc(tempID, sendStr);

				this.showTextBox.AppendText("Sent to " + count.ToString() + " People\r\n");
				count++;
			}
 
			this.writeTextBox.Text = "";
			this.writeTextBox.Focus();
		}

		private void ClientProc(string tempID, string sendStr)
		{
			string clientIP = WC.GetXML(tempID, "ci");
			int clientPort = Convert.ToInt32(WC.GetXML(tempID, "cp"));

			Socket AllSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			IPAddress host_addr = Dns.GetHostByAddress(clientIP).AddressList[0];
			IPEndPoint ep = new IPEndPoint(host_addr, clientPort);

			try
			{
				AllSocket.Connect(ep);
				NetworkStream stream = new NetworkStream(AllSocket);
				TextWriter SocketWriter = new StreamWriter(stream);
				
				sendStr = WC.ReplaceStr(sendStr, "\n", "&Line;") + "\n";
				SocketWriter.Write(sendStr);
				SocketWriter.Flush();
			}
			catch (Exception e)
			{
				xWinClient.SendRootMsg(sendStr);
				return;
			}

			if (AllSocket != null)
			{
				AllSocket.Close();
				AllSocket = null;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -