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

📄 frmserver.cs

📁 Winform_OnlineTraning 我们培训机构的在线培训部分
💻 CS
字号:
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.IO;

namespace Example_2
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmServer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnActivate;
		private System.Windows.Forms.Label lblMessage;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		//User Defined variables

		private TcpListener objTcpListener;
		private Socket objSocketForClient;
		private NetworkStream objNetworkStream;
		private StreamReader objStreamReader;
		private System.Windows.Forms.Label lblStatus;
		private StreamWriter objStreamWriter;

		public frmServer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnActivate = new System.Windows.Forms.Button();
			this.lblMessage = new System.Windows.Forms.Label();
			this.lblStatus = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// btnActivate
			// 
			this.btnActivate.Location = new System.Drawing.Point(152, 128);
			this.btnActivate.Name = "btnActivate";
			this.btnActivate.TabIndex = 0;
			this.btnActivate.Text = "激活(&A)";
			this.btnActivate.Click += new System.EventHandler(this.btnActivate_Click);
			// 
			// lblMessage
			// 
			this.lblMessage.Location = new System.Drawing.Point(8, 40);
			this.lblMessage.Name = "lblMessage";
			this.lblMessage.Size = new System.Drawing.Size(224, 80);
			this.lblMessage.TabIndex = 1;
			// 
			// lblStatus
			// 
			this.lblStatus.Location = new System.Drawing.Point(8, 16);
			this.lblStatus.Name = "lblStatus";
			this.lblStatus.TabIndex = 2;
			this.lblStatus.Text = "状态:";
			// 
			// frmServer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(248, 165);
			this.Controls.Add(this.lblStatus);
			this.Controls.Add(this.lblMessage);
			this.Controls.Add(this.btnActivate);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.MaximizeBox = false;
			this.Name = "frmServer";
			this.Text = "服务器";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmServer());
		}

		private void btnActivate_Click(object sender, System.EventArgs e)
		{
			//Specifying the port
			objTcpListener = new TcpListener(8221);
			//Starting the Listener
			objTcpListener.Start();      
			objSocketForClient = objTcpListener.AcceptSocket();
			
			//if connected
			if (objSocketForClient.Connected)
			{
				//Display message
				lblMessage.Text += "客户端已连接";

				//creating streams
				objNetworkStream = new NetworkStream(objSocketForClient);
				objStreamWriter =	new StreamWriter(objNetworkStream);
 
				objStreamReader = new StreamReader(objNetworkStream);
          
				string theString = "\n"+"服务器消息";
				
				objStreamWriter.WriteLine(theString);

				lblMessage.Text += theString;
                   
				objStreamWriter.Flush();
				//Reading the client message
				theString = objStreamReader.ReadLine();
				//displaying the client message
				lblMessage.Text += ":"+ theString;
				objStreamReader.Close();
				objNetworkStream.Close();
				objStreamWriter.Close();
			}
			
			objSocketForClient.Close();
			lblMessage.Text += "\n\n"+"正退出...";

		}
	}
}

⌨️ 快捷键说明

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