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

📄 mainform.cs

📁 c#写的网络聊天工具
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;

namespace UseSocket
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components=null;
		private System.Windows.Forms.Label lState;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.TextBox txUserName;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.RichTextBox richMessageBox;
		private System.Windows.Forms.Button btnConnect;
		private System.Windows.Forms.TextBox txtIp;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnCreate;
		private System.Windows.Forms.TextBox txtInput;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem mmiSendFile;
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.SaveFileDialog saveFileDialog;
		private System.Windows.Forms.TextBox txtPort;
		
		
		
		enum UserMode 
		{
			ChatMode = 1,
			FileMode = 2
		}
		

		delegate void NetDelegate();      
		delegate void MessageHandler(string msg);
		
		NetDelegate netDelegate;
		AsyncCallback asynSendFile;
		AsyncCallback asynCallDelegate;
		MessageHandler sendFileDelegate;
	   
		UserMode currentMode = UserMode.ChatMode;  //当前模式
		string fileToSend = "";      //要发送 或 存储的文件名
		const int bufferSize = 4194304;  //文件传送缓存大小
		int fileSize = 0;                //原始文件大小
		int bytesWriten = 0;           // 已经写入的字节数
		bool CanWirterFile = false;   //是否可以开始写文件 同步用
		bool IsConnected = false;     //是否已连接上
		byte []mbuffer = new byte[512];  //消息缓存区
		byte []fbuffer = new byte[bufferSize]; //文件缓存区
		
		Socket mainSocket;     //监听
		Socket acceptSocket;   //用来发送数据的套接字
        
		 
   
		//回调结束
		void EndCall(System.IAsyncResult ar)
		{
		 
		}
		  
		//非托管调用
		[DllImport("winmm.dll")]
		static extern bool PlaySound(string file,System.IntPtr handle,int flag);
		
		
		public MainForm() 
		{ 
			InitializeComponent();
		}

		 
		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(MainForm));
			this.btnCreate = new System.Windows.Forms.Button();
			this.txtInput = new System.Windows.Forms.TextBox();
			this.lState = new System.Windows.Forms.Label();
			this.btnSend = new System.Windows.Forms.Button();
			this.richMessageBox = new System.Windows.Forms.RichTextBox();
			this.txUserName = new System.Windows.Forms.TextBox();
			this.btnConnect = new System.Windows.Forms.Button();
			this.txtIp = new System.Windows.Forms.TextBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.txtPort = new System.Windows.Forms.TextBox();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.mmiSendFile = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			this.SuspendLayout();
			// 
			// btnCreate
			// 
			this.btnCreate.Location = new System.Drawing.Point(272, 212);
			this.btnCreate.Name = "btnCreate";
			this.btnCreate.Size = new System.Drawing.Size(68, 24);
			this.btnCreate.TabIndex = 0;
			this.btnCreate.Text = "建立主机";
			this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
			// 
			// txtInput
			// 
			this.txtInput.Location = new System.Drawing.Point(76, 180);
			this.txtInput.MaxLength = 200;
			this.txtInput.Multiline = true;
			this.txtInput.Name = "txtInput";
			this.txtInput.Size = new System.Drawing.Size(352, 24);
			this.txtInput.TabIndex = 2;
			this.txtInput.Text = "测试消息";
			// 
			// lState
			// 
			this.lState.Location = new System.Drawing.Point(16, 216);
			this.lState.Name = "lState";
			this.lState.Size = new System.Drawing.Size(228, 16);
			this.lState.TabIndex = 3;
			this.lState.Text = "监听服务器服务IP           使用端口";
			// 
			// btnSend
			// 
			this.btnSend.Location = new System.Drawing.Point(356, 212);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(72, 24);
			this.btnSend.TabIndex = 4;
			this.btnSend.Text = "发送消息";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// richMessageBox
			// 
			this.richMessageBox.AcceptsTab = true;
			this.richMessageBox.BackColor = System.Drawing.SystemColors.Info;
			this.richMessageBox.HideSelection = false;
			this.richMessageBox.Location = new System.Drawing.Point(8, 0);
			this.richMessageBox.Name = "richMessageBox";
			this.richMessageBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
			this.richMessageBox.Size = new System.Drawing.Size(420, 168);
			this.richMessageBox.TabIndex = 5;
			this.richMessageBox.Text = "";
			this.richMessageBox.TextChanged += new System.EventHandler(this.richMessageBox_TextChanged);
			// 
			// txUserName
			// 
			this.txUserName.Location = new System.Drawing.Point(12, 180);
			this.txUserName.Multiline = true;
			this.txUserName.Name = "txUserName";
			this.txUserName.Size = new System.Drawing.Size(56, 24);
			this.txUserName.TabIndex = 6;
			this.txUserName.Text = "默认用户名";
			// 
			// btnConnect
			// 
			this.btnConnect.Location = new System.Drawing.Point(272, 248);
			this.btnConnect.Name = "btnConnect";
			this.btnConnect.Size = new System.Drawing.Size(68, 24);
			this.btnConnect.TabIndex = 7;
			this.btnConnect.Text = "连接";
			this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
			// 
			// txtIp
			// 
			this.txtIp.Location = new System.Drawing.Point(16, 244);
			this.txtIp.Name = "txtIp";
			this.txtIp.Size = new System.Drawing.Size(112, 21);
			this.txtIp.TabIndex = 8;
			this.txtIp.Text = "192.168.1.134";
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(356, 248);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(72, 23);
			this.btnClose.TabIndex = 9;
			this.btnClose.Text = "断开";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// txtPort
			// 
			this.txtPort.Location = new System.Drawing.Point(192, 244);
			this.txtPort.Name = "txtPort";
			this.txtPort.Size = new System.Drawing.Size(44, 21);
			this.txtPort.TabIndex = 10;
			this.txtPort.Text = "88";
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItem1,
																					 this.menuItem2});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mmiSendFile});
			this.menuItem1.Text = "文件(&F)";
			// 
			// mmiSendFile
			// 
			this.mmiSendFile.Index = 0;
			this.mmiSendFile.Text = "发送文件(&S)";
			this.mmiSendFile.Click += new System.EventHandler(this.mmiSendFile_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3});
			this.menuItem2.Text = "帮助(&H)";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "帮助(&B)";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// MainForm
			// 
			this.AcceptButton = this.btnSend;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(438, 279);
			this.Controls.Add(this.txtPort);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.txtIp);
			this.Controls.Add(this.btnConnect);
			this.Controls.Add(this.txUserName);
			this.Controls.Add(this.richMessageBox);
			this.Controls.Add(this.btnSend);
			this.Controls.Add(this.lState);
			this.Controls.Add(this.txtInput);
			this.Controls.Add(this.btnCreate);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Menu = this.mainMenu;
			this.Name = "MainForm";
			this.Text = "局域网聊天程序";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.Closed += new System.EventHandler(this.Form1_Closed);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.btnSend.Enabled = false;
			this.btnClose.Enabled = false;
		}

	  
		
		//处理是否有连接
		void CatchConnection()
		{
			while(!this.Disposing && this.mainSocket.Poll(500,SelectMode.SelectRead))
			{      }
			this.IsConnected = true;
			this.acceptSocket = this.mainSocket.Accept();
			this.btnSend.Enabled = true;
			this.netDelegate = new NetDelegate(this.GetMessage);
			this.asynCallDelegate = new AsyncCallback(this.EndCall);
			this.netDelegate.BeginInvoke(this.asynCallDelegate,(object)"ddd");
		}
		 
		 
		
		//开始监听 
		void BeginListen()
		{
			this.Text = "在端口: "+this.txtPort.Text+" 监听...";
			try
			{
				this.mainSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
				IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString()),int.Parse(this.txtPort.Text));
				this.mainSocket.Bind(ipe);
				this.mainSocket.Listen(10);

				this.btnClose.Enabled = true;
				this.btnCreate.Enabled = false;
				this.btnConnect.Enabled = false;

				this.netDelegate = new NetDelegate(this.CatchConnection);
				this.asynCallDelegate = new AsyncCallback(this.EndCall);
				this.netDelegate.BeginInvoke(this.asynCallDelegate,(object)"ddd");
			}
			catch(Exception e)
			{
				this.CloseAll();
				MessageBox.Show(e.Message);
				return ;
			}
		}
		
		//发送标志性消息
		void SendTagMessage(string tag)
		{
			byte []array = System.Text.Encoding.UTF7.GetBytes(tag);

⌨️ 快捷键说明

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