.net 中使用socket (c#)
生活随笔
收集整理的這篇文章主要介紹了
.net 中使用socket (c#)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前幾天在網上看到關于使用socket 編寫聊天程序的一個例子,學習了一下,網上的例子是VB.NET的,自己改寫成了C#的?大同小異,只作為記錄?:
發送端================================ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace CSClientTest { ???? ///<summary> ???? /// sendform 的摘要說明。 ???? ///</summary> ???? public class sendform : System.Windows.Forms.Form ???? { ???????? private System.Windows.Forms.TextBox textBox1; ???????? private System.Windows.Forms.Button button1; ???????? private System.Windows.Forms.Label label1; ???????? ///<summary> ???????? ///必需的設計器變量。 ???????? ///</summary> ???????? private System.ComponentModel.Container components = null; ???????? public sendform() ???????? { ????????????? // ????????????? // Windows 窗體設計器支持所必需的 ????????????? // ????????????? InitializeComponent(); ????????????? // ????????????? // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 ????????????? // ???????? } ???????? ///<summary> ???????? ///清理所有正在使用的資源。 ???????? ///</summary> ???????? protected override void Dispose( bool disposing ) ???????? { ????????????? if( disposing ) ????????????? { ?????????????????? if(components != null) ?????????????????? { ?????????????????????? components.Dispose(); ?????????????????? } ????????????? } ????????????? base.Dispose( disposing ); ???????? } ???????? #region Windows 窗體設計器生成的代碼 ???????? ///<summary> ???????? ///設計器支持所需的方法 - 不要使用代碼編輯器修改 ???????? ///此方法的內容。 ???????? ///</summary> ???????? private void InitializeComponent() ???????? { ????????????? this.textBox1 = new System.Windows.Forms.TextBox(); ????????????? this.button1 = new System.Windows.Forms.Button(); ????????????? this.label1 = new System.Windows.Forms.Label(); ????????????? this.SuspendLayout(); ????????????? // ????????????? // textBox1 ????????????? // ????????????? this.textBox1.Location = new System.Drawing.Point(96, 144); ????????????? this.textBox1.Name = "textBox1"; ????????????? this.textBox1.Size = new System.Drawing.Size(200, 21); ???? ???????? this.textBox1.TabIndex = 0; ????????????? this.textBox1.Text = ""; ????????????? // ????????????? // button1 ????????????? // ????????????? this.button1.Location = new System.Drawing.Point(320, 144); ????????????? this.button1.Name = "button1"; ????????????? this.button1.TabIndex = 1; ????????????? this.button1.Text = "sender"; ????????????? this.button1.Click += new System.EventHandler(this.button1_Click); ????????????? // ????????????? // label1 ????????????? // ????????????? this.label1.Location = new System.Drawing.Point(0, 0); ????????????? this.label1.Name = "label1"; ????????????? this.label1.Size = new System.Drawing.Size(608, 112); ????????????? this.label1.TabIndex = 2; ????????????? // ????????????? // sendform ????????????? // ????????????? this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); ????????????? this.ClientSize = new System.Drawing.Size(616, 238); ????????????? this.Controls.Add(this.label1); ????????????? this.Controls.Add(this.button1); ????????????? this.Controls.Add(this.textBox1); ????????????? this.Name = "sendform"; ????????????? this.Text = "sendform"; ????????????? this.Load += new System.EventHandler(this.sendform_Load); ????????????? this.ResumeLayout(false); ???????? } ???????? #endregion ???????? private void sendform_Load(object sender, System.EventArgs e) ???????? { ????????????? ???????? } ???????? private void button1_Click(object sender, System.EventArgs e) ???????? { ????????????? //定義一個socket對象 ????????????? System.Net.Sockets.Socket socket= new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp); ????????????? ????????????? //定義一個字節數組 ????????????? byte[] b = new byte[1024]; ????????????? ????????????? //從文本框中獲得數據轉換為字節數組后存入b ????????????? b=System.Text.Encoding.UTF8.GetBytes(textBox1.Text); ????????????? ????????????? //定義目的端的IP和端口 ????????????? System.Net.IPEndPoint ep = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"),1022); ????????????? try ????????????? { ?????????????????? //連接到目的端 ?????????????????? socket.Connect(ep); ?????????????????? //用socket的send方法發送數據,該方法返回發送的數據的字節數 ?????????????????? label1.Text="數據已發送,總共:"+socket.Send(b).ToString()+"字節"; ?????????????????? ?????????????????? //禁止并關閉socket ?????????????????? socket.Shutdown(System.Net.Sockets.SocketShutdown.Both); ?????????????????? socket.Close(); ????????????? } ????????????? catch(System.Exception ex) ????????????? { ?????????????????? label1.Text=ex.Message.ToString(); ????????????? } ???????? } ???? } } 接收端
================================ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace CSClientTest { ???? ///<summary> ???? /// receive 的摘要說明。 ???? ///</summary> ???? public class receive : System.Windows.Forms.Form ???? { ???????? private System.Windows.Forms.Button button1; ???????? private System.Windows.Forms.TextBox textBox1; ???????? ///<summary> ???????? ///必需的設計器變量。 ???????? ///</summary> ???????? private System.ComponentModel.Container components = null; ???????? //定義一個全局的socket以便監聽和接收數據 ???????? System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp); ???????? public receive() ???????? { ????????????? // ????????????? // Windows 窗體設計器支持所必需的 ????????????? // ????????????? InitializeComponent(); ????????????? // ????????????? // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 ????????????? // ???????? } ???????? ///<summary> ???????? ///清理所有正在使用的資源。 ???????? ///</summary> ???????? protected override void Dispose( bool disposing ) ???????? { ????????????? if( disposing ) ????????????? { ?????????????????? if(components != null) ?????????????????? { ?????????????????????? components.Dispose(); ?????????????????? } ????????????? } ????????????? base.Dispose( disposing ); ???????? } ???????? #region Windows 窗體設計器生成的代碼 ???????? ///<summary> ???????? ///設計器支持所需的方法 - 不要使用代碼編輯器修改 ???????? ///此方法的內容。 ???????? ///</summary> ???????? private void InitializeComponent() ???????? { ????????????? this.button1 = new System.Windows.Forms.Button(); ????????????? this.textBox1 = new System.Windows.Forms.TextBox(); ????????????? this.SuspendLayout(); ????????????? // ????????????? // button1 ????????????? // ????????????? this.button1.Location = new System.Drawing.Point(448, 184); ????????????? this.button1.Name = "button1"; ????????????? this.button1.TabIndex = 0; ????????????? this.button1.Text = "接受"; ????????????? this.button1.Click += new System.EventHandler(this.button1_Click); ????????????? // ????????????? // textBox1 ????????????? // ????????????? this.textBox1.Location = new System.Drawing.Point(224, 184); ????????????? this.textBox1.Name = "textBox1"; ???? ???????? this.textBox1.Size = new System.Drawing.Size(208, 21); ????????????? this.textBox1.TabIndex = 1; ????????????? this.textBox1.Text = ""; ????????????? // ????????????? // receive ????????????? // ????????????? this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); ????????????? this.ClientSize = new System.Drawing.Size(664, 266); ????????????? this.Controls.Add(this.textBox1); ????????????? this.Controls.Add(this.button1); ????????????? this.Name = "receive"; ????????????? this.Text = "receive"; ????????????? this.Load += new System.EventHandler(this.receive_Load); ????????????? this.ResumeLayout(false); ???????? } ???????? #endregion ???????? private void receive_Load(object sender, System.EventArgs e) ???????? { ????????????? //定義本地接收端IP和端口 ????????????? System.Net.IPEndPoint ep = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"),1022); ????????????? ????????????? //socket綁定本地接收端 ????????????? socket.Bind(ep); ????????????? //監聽,監聽掛起10 ????????????? socket.Listen(10); ???????? } ???????? private void button1_Click(object sender, System.EventArgs e) ???????? { ????????????? try ????????????? { ?????????????????? if(socket.Blocking) ?????????????????? { ?????????????????????? //為新建連接創建新的 Socket ?????????????????????? System.Net.Sockets.Socket socket1=socket.Accept(); ?????????????????????? //定義一個字節數組 ?????????????????????? byte[] b = new byte[1024]; ????????????? ?????????????????????? //將接收到的字節數組存入b中 ?????????????????????? socket1.Receive(b); ?????????????????????? //將B中的數據轉換為字符串后顯示到textbox ?????????????????????? textBox1.Text = System.Text.Encoding.UTF8.GetString(b); ?????????????????????? //禁止并關閉這個新的socket連接 ?????????????????????? socket1.Shutdown(System.Net.Sockets.SocketShutdown.Both); ?????????????????????? socket1.Close(); ?????????????????? } ????????????? } ????????????? catch ????????????? { ?????????????????? textBox1.Text = "無數據"; ????????????? } ???????? } ???????? ???? } }
轉載于:https://www.cnblogs.com/ZetaChow/archive/2006/07/03/2237425.html
總結
以上是生活随笔為你收集整理的.net 中使用socket (c#)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net打包自动安装数据库
- 下一篇: 操作系统学习笔记-02-1.2-什么是操