📄 frmserver.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;
namespace Example_1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmServer : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblMessage;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Label lblPort;
private System.Windows.Forms.Label lblHostID;
private System.Windows.Forms.TextBox txtHostID;
private System.Windows.Forms.TextBox txtPortNumber;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label lblConnectionStatus;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
//user defined variables
private IPAddress objIPAddress ;
private TcpListener objTcpListener;
private Socket objSocket;
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.lblMessage = new System.Windows.Forms.Label();
this.btnStart = new System.Windows.Forms.Button();
this.lblHostID = new System.Windows.Forms.Label();
this.lblPort = new System.Windows.Forms.Label();
this.txtHostID = new System.Windows.Forms.TextBox();
this.txtPortNumber = new System.Windows.Forms.TextBox();
this.btnExit = new System.Windows.Forms.Button();
this.lblConnectionStatus = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblMessage
//
this.lblMessage.Location = new System.Drawing.Point(10, 80);
this.lblMessage.Name = "lblMessage";
this.lblMessage.Size = new System.Drawing.Size(406, 86);
this.lblMessage.TabIndex = 0;
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(344, 40);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(64, 25);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "开始(&S)";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// lblHostID
//
this.lblHostID.Location = new System.Drawing.Point(10, 9);
this.lblHostID.Name = "lblHostID";
this.lblHostID.Size = new System.Drawing.Size(62, 15);
this.lblHostID.TabIndex = 2;
this.lblHostID.Text = "主机 ID:";
//
// lblPort
//
this.lblPort.Location = new System.Drawing.Point(216, 11);
this.lblPort.Name = "lblPort";
this.lblPort.Size = new System.Drawing.Size(62, 13);
this.lblPort.TabIndex = 3;
this.lblPort.Text = "端口号:";
//
// txtHostID
//
this.txtHostID.Location = new System.Drawing.Point(80, 9);
this.txtHostID.Name = "txtHostID";
this.txtHostID.Size = new System.Drawing.Size(120, 21);
this.txtHostID.TabIndex = 4;
this.txtHostID.Text = "";
//
// txtPortNumber
//
this.txtPortNumber.Location = new System.Drawing.Point(288, 8);
this.txtPortNumber.Name = "txtPortNumber";
this.txtPortNumber.Size = new System.Drawing.Size(120, 21);
this.txtPortNumber.TabIndex = 5;
this.txtPortNumber.Text = "";
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(344, 184);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(64, 25);
this.btnExit.TabIndex = 6;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lblConnectionStatus
//
this.lblConnectionStatus.Location = new System.Drawing.Point(10, 56);
this.lblConnectionStatus.Name = "lblConnectionStatus";
this.lblConnectionStatus.Size = new System.Drawing.Size(70, 16);
this.lblConnectionStatus.TabIndex = 7;
this.lblConnectionStatus.Text = "连接状态:";
//
// frmServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(424, 213);
this.Controls.Add(this.lblConnectionStatus);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.txtPortNumber);
this.Controls.Add(this.txtHostID);
this.Controls.Add(this.lblPort);
this.Controls.Add(this.lblHostID);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.lblMessage);
this.MaximizeBox = false;
this.Name = "frmServer";
this.Text = "服务器应用程序";
this.Load += new System.EventHandler(this.frmServer_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmServer());
}
private void btnStart_Click(object sender, System.EventArgs e)
{
try
{
//local machine address and use same in the client
objIPAddress = IPAddress.Parse(this.txtHostID.Text);
objTcpListener = new TcpListener(objIPAddress,Convert.ToInt32(this.txtPortNumber.Text));
objTcpListener.Start();
//Writing the status to the label control
this.lblMessage.Text = "服务器正在端口 "+this.txtPortNumber.Text + " 运行....\n";
this.lblMessage.Text += "\n本地端点是:"+ objTcpListener.LocalEndpoint;
this.lblMessage.Text += "\n等待连接...........";
objSocket = objTcpListener.AcceptSocket();
this.lblMessage.Text += "\n接受连接于 " + objSocket.RemoteEndPoint;
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}
}
private void frmServer_Load(object sender, System.EventArgs e)
{
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -