?? form1.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
// 新添加的命令空間
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.IO;
namespace SocketServer
{
/// <summary>
/// 服務器端。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
// 客戶端節點
private IPEndPoint client;
// 服務器端Socket
private Socket server;
// 服務器端監聽線程
private Thread thdSvr;
// 網絡數據流
NetworkStream stream;
// 寫數據流
TextWriter writer;
// 讀數據流
TextReader reader;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
// 監聽線程
private void ThreadServer()
{
client = new IPEndPoint(IPAddress.Any, 34567);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Blocking = true;
server.Bind(client);
server.Listen(0);
while(true)
{
Socket t = server.Accept();
if (t != null)
{
stream = new NetworkStream(t);
writer = new StreamWriter(stream);
reader = new StreamReader(stream);
richTextBox1.Text += "接收到一個連接...\n";
writer.WriteLine("歡迎連接到服務器!");
writer.Flush();
writer.WriteLine("您現在可以說話了...");
writer.Flush();
richTextBox1.Text += reader.ReadLine();
richTextBox1.Text += "\n";
reader.Close();
writer.Close();
stream.Close();
t.Close();
}
}
}
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(server != null)
server.Close();
if(!button1.Enabled)
if(thdSvr != null)
thdSvr.Abort();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(416, 256);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 264);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "啟動服務器";
this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(416, 296);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.richTextBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "服務器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// 開始服務器端的運行。
private void button1_Click(object sender, System.EventArgs e)
{
thdSvr = new Thread(new ThreadStart(this.ThreadServer));
thdSvr.Start();
button1.Enabled = false;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -