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

📄 winreceive.cs

📁 一个即时通信 Messenger C#代码
💻 CS
字号:
/*

<Aicaca Community Messenger Client>

Copyright (c) Aicaca.com, All rights reserved.

Writer                : Byung-ku, Cho (Aicaca)
E-mail                : aicaca@hanmail.net
Personal Homepage URL : http://www.aicaca.com
Last Updated Date     : Feb. 27th, 2002

Programming Language  : C#
Editor & Compiler     : VisualStudio.NET

*/


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 WinReceive : System.Windows.Forms.Form
	{
		private WinCom WC = new WinCom();
		private WinClient xWinClient;
		private WinStream xWinStream;

		private Socket FileSocket = null;
		private TextWriter SocketWriter = null;

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

		private bool lockMode;
		private bool directMode;

		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.SaveFileDialog saveDialog;
		private System.Windows.Forms.TextBox idTextBox;
		private System.Windows.Forms.TextBox pathTextBox;
		private System.Windows.Forms.Label idLabel;
		private System.Windows.Forms.GroupBox downGroupBox;
		private System.Windows.Forms.Label fileLabel;
		private System.Windows.Forms.TextBox mainTextBox;
		private System.Windows.Forms.Button acceptButton;
		private System.Windows.Forms.Button rejectButton;

		public WinReceive(WinClient tWinClient, Socket tFileSocket, string tLocalID, string tClientID, string tMsgBuffer)
		{
			xWinClient = tWinClient;
			this.SetSocket(tFileSocket, false);
			localID = tLocalID;
			clientID = tClientID;
			msgBuffer = tMsgBuffer;
		}

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

		public void WinThread_Accept()
		{
			InitializeAll();
			this.StartWinStream_Accept();
            this.OnILOVEAICACAFNO(msgBuffer);
			this.ShowDialog();
		}

		public void WinThread_RoundMsg()
		{
			InitializeAll();
            this.OnILOVEAICACAFNO(msgBuffer);
			this.ShowDialog();
		}

        #region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.idLabel = new System.Windows.Forms.Label();
			this.pathTextBox = new System.Windows.Forms.TextBox();
			this.saveDialog = new System.Windows.Forms.SaveFileDialog();
			this.acceptButton = new System.Windows.Forms.Button();
			this.downGroupBox = new System.Windows.Forms.GroupBox();
			this.mainTextBox = new System.Windows.Forms.TextBox();
			this.fileLabel = new System.Windows.Forms.Label();
			this.rejectButton = new System.Windows.Forms.Button();
			this.idTextBox = new System.Windows.Forms.TextBox();
			this.downGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// idLabel
			// 
			this.idLabel.Location = new System.Drawing.Point(8, 24);
			this.idLabel.Name = "idLabel";
			this.idLabel.Size = new System.Drawing.Size(64, 16);
			this.idLabel.TabIndex = 0;
			this.idLabel.Text = "Client ID";
			this.idLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// pathTextBox
			// 
			this.pathTextBox.Location = new System.Drawing.Point(80, 40);
			this.pathTextBox.Name = "pathTextBox";
			this.pathTextBox.ReadOnly = true;
			this.pathTextBox.Size = new System.Drawing.Size(168, 21);
			this.pathTextBox.TabIndex = 0;
			this.pathTextBox.Text = "";
			// 
			// saveDialog
			// 
			this.saveDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.saveButton_Click);
			// 
			// acceptButton
			// 
			this.acceptButton.Location = new System.Drawing.Point(8, 128);
			this.acceptButton.Name = "acceptButton";
			this.acceptButton.Size = new System.Drawing.Size(120, 24);
			this.acceptButton.TabIndex = 1;
			this.acceptButton.Text = "Accept";
			this.acceptButton.Click += new System.EventHandler(this.acceptButton_Click);
			// 
			// downGroupBox
			// 
			this.downGroupBox.Controls.AddRange(new System.Windows.Forms.Control[] {
																					   this.mainTextBox,
																					   this.fileLabel,
																					   this.acceptButton,
																					   this.rejectButton,
																					   this.pathTextBox,
																					   this.idLabel,
																					   this.idTextBox});
			this.downGroupBox.Name = "downGroupBox";
			this.downGroupBox.Size = new System.Drawing.Size(256, 160);
			this.downGroupBox.TabIndex = 0;
			this.downGroupBox.TabStop = false;
			// 
			// mainTextBox
			// 
			this.mainTextBox.Location = new System.Drawing.Point(8, 72);
			this.mainTextBox.MaxLength = 1000;
			this.mainTextBox.Multiline = true;
			this.mainTextBox.Name = "mainTextBox";
			this.mainTextBox.ReadOnly = true;
			this.mainTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.mainTextBox.Size = new System.Drawing.Size(240, 48);
			this.mainTextBox.TabIndex = 0;
			this.mainTextBox.Text = "";
			// 
			// fileLabel
			// 
			this.fileLabel.Location = new System.Drawing.Point(8, 48);
			this.fileLabel.Name = "fileLabel";
			this.fileLabel.Size = new System.Drawing.Size(64, 16);
			this.fileLabel.TabIndex = 0;
			this.fileLabel.Text = "File Name";
			this.fileLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// rejectButton
			// 
			this.rejectButton.Location = new System.Drawing.Point(136, 128);
			this.rejectButton.Name = "rejectButton";
			this.rejectButton.Size = new System.Drawing.Size(112, 24);
			this.rejectButton.TabIndex = 2;
			this.rejectButton.Text = "Reject";
			this.rejectButton.Click += new System.EventHandler(this.rejectButton_Click);
			// 
			// idTextBox
			// 
			this.idTextBox.Location = new System.Drawing.Point(80, 16);
			this.idTextBox.Name = "idTextBox";
			this.idTextBox.ReadOnly = true;
			this.idTextBox.Size = new System.Drawing.Size(168, 21);
			this.idTextBox.TabIndex = 0;
			this.idTextBox.Text = "";
			// 
			// WinReceive
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(256, 165);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.downGroupBox});
			this.MaximizeBox = false;
			this.Name = "WinReceive";
			this.Text = "Aicaca WinReceive";
			this.Closed += new System.EventHandler(this.OnClosed);
			this.downGroupBox.ResumeLayout(false);
			this.ResumeLayout(false);

		}
        #endregion

		private void OnClosed(object sender, System.EventArgs e)
		{
			if (lockMode)
			{
				this.SendILOVEAICACAFRE();
			}

			xWinClient.RemoveThread(threadID);
			if (xWinStream != null) xWinStream.CloseThis();
		}

		private void acceptButton_Click (object sender, System.EventArgs e)
		{
			saveDialog.ShowDialog();
		}

		private void rejectButton_Click (object sender, System.EventArgs e)
		{
			this.SendILOVEAICACAFRE();
			lockMode = false;
			this.Close();
		}

		private void saveButton_Click (object sender, System.ComponentModel.CancelEventArgs e)
		{
			string changeFileName = saveDialog.FileName;
			this.SendILOVEAICACAFAC(changeFileName);
		}

		private void InitializeAll()
		{
			this.StartWinMode();

			string threadSort = "WinReceive";
			string threadName = WC.GetXML(clientID, "cn");
			string threadIP = WC.GetXML(clientID, "ci");
			string threadPort = WC.GetXML(clientID, "cp");
			threadID = WC.MakeThreadID(threadSort, threadName, threadIP, threadPort);

			string remoteSort = "WinSend";
			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.idTextBox.Text = WC.GetXML(clientID, "cn") + ":" + WC.GetXML(clientID, "ci") + ":" + WC.GetXML(clientID, "cp");
		}

		private void AddCommingText(string str)
		{
			str = WC.ReplaceStr(str, "&Line;", "\n");
			this.mainTextBox.Text = str + "\r\n";
		}

		private void SendILOVEAICACAFAC(string changeFileName)
		{
			string sendStr = sendStr = WC.MakeXML("ms", "ILOVEAICACAFAC") + threadID + remoteID + WC.MakeXML("fc", changeFileName);

			if (FileSocket != null && FileSocket.Connected)
			{
				this.SendMsg(sendStr);
			}
			else if (directMode == true)
			{
				this.StartWinStream_Connect(sendStr, null);
			}
			else
			{
				xWinClient.SendRootMsg(sendStr);
			}

			acceptButton.Enabled = false;
			rejectButton.Enabled = false;
		}

		private void SendILOVEAICACAFRE()
		{
			string sendStr = sendStr = WC.MakeXML("ms", "ILOVEAICACAFRE") + threadID + remoteID;

			if (FileSocket != null && FileSocket.Connected)
			{
				this.SendMsg(sendStr);
			}
			else if (directMode == true)
			{
				this.StartWinStream_Connect(sendStr, null);
			}
			else
			{
				xWinClient.SendRootMsg(sendStr);
			}
		}

		private void StartWinMode()
		{
			WinMode xWinMode = new WinMode(this, clientID);
			Thread ModeThread = new Thread(new ThreadStart(xWinMode.WinThread_Receive));
			ModeThread.Start();
		}

		private void StartWinStream_Connect(string sendStr, byte[] sendStream)
		{
			xWinStream = new WinStream(xWinClient, this, localID, clientID, sendStr, sendStream);
			Thread StreamThread = new Thread(new ThreadStart(xWinStream.ClientProc));
			StreamThread.Start();
		}

		private void StartWinStream_Accept()
		{
			xWinStream = new WinStream(xWinClient, this, FileSocket);
			Thread StreamThread = new Thread(new ThreadStart(xWinStream.ServerProc));
			StreamThread.Start();
		}

		public void SetSocket(Socket sk, bool threadMode)
		{
			lock(this)
			{
				if (sk != null && sk.Connected)
				{
					FileSocket = sk;
					NetworkStream stream = new NetworkStream(FileSocket);
					SocketWriter = new StreamWriter(stream);

					if (threadMode == true) this.StartWinStream_Accept();
				}
			}
		}

		public void SendMsg(string str)
		{
			lock(this)
			{
				str = WC.ReplaceStr(str, "\n", "&Line;") + "\n";
		    
				SocketWriter.Write(str);
				SocketWriter.Flush();
			}
		}

		public void SendStream(byte[] b)
		{
			lock(this)
			{
				FileSocket.Send(b, b.Length, 0);
			}
		}

		public void OnILOVEAICACAFNO(string str)
		{
			lock(this)
			{
				string originFileName = WC.GetXML(str, "fo");
				string shortFileName = new FileInfo(originFileName).Name;
				int fileLen = Convert.ToInt32(WC.GetXML(str, "fl"));
				string fileMsg = WC.GetNextXML(str, "body");

				this.pathTextBox.Text = shortFileName;
				this.saveDialog.FileName = shortFileName;
				this.AddCommingText(fileMsg);

				this.TopMost = true;
				this.TopMost = false;

				lockMode = true;
			}
		}

		public void OnILOVEAICACAFCA()
		{
			lock(this)
			{
				MessageBox.Show(this, WC.lineStr14);
				lockMode = false;
				this.Close();
			}
		}

		public void OnILOVEAICACAFIL(string str, byte[] b)
		{
			lock(this)
			{
				string changeFileName = WC.GetXML(str, "fc");
				int fileLen = Convert.ToInt32(WC.GetXML(str, "fl"));

				FileStream fs = new FileStream(changeFileName, FileMode.Create);
				fs.Write(b,0,fileLen);
				fs.Close();

				lockMode = false;
				this.Close();
			}
		}

		public void OnILOVEAICACAERR(string str)
		{
			lock(this)
			{
				MessageBox.Show(this, WC.GetXML(str, "er"));
				lockMode = false;
				this.Close();
			}
		}

		public Socket pFileSocket
		{
			set
			{
				this.SetSocket(value, false);
			}
		}

		public bool pDirectMode
		{
			set
			{
				this.directMode = value;
			}
		}
	}
}

⌨️ 快捷键说明

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