实例解析SOCKET编程模型(2)
<br> 以下是同步聊天程序的客户端源代码。其中的一些过程可以参考服务器端源码<br> <br> using System;<br> using System.Drawing;<br> using System.Collections;<br> using System.ComponentModel;<br> using System.Windows.Forms;<br> using System.Data;<br> using System.Net;<br> using System.Net.Sockets;<br> using System.Threading;<br> using System.Text;<br> namespace 聊天_socket_client<br> {<br> /// <summary><br> /// Form1 的摘要说明。<br> /// </summary><br> public class Form1 : System.Windows.Forms.Form<br> {<br> private System.Windows.Forms.Label label4;<br> private System.Windows.Forms.Label label3;<br> private System.Windows.Forms.Label label2;<br> private System.Windows.Forms.Label label1;<br> private System.Windows.Forms.Button btnStop;<br> private System.Windows.Forms.Button btnSend;<br> private System.Windows.Forms.TextBox txtPort;<br> private System.Windows.Forms.TextBox txtServer;<br> private System.Windows.Forms.RichTextBox rtbSend;<br> private System.Windows.Forms.RichTextBox rtbReceive;<br> private System.Windows.Forms.StatusBar statusBar1;<br> private System.Windows.Forms.Button btnConnect;<br> private IPAddress hostIPAddress;<br> private IPEndPoint Server;<br> private Socket sock;<br> /// <summary><br> /// 必需的设计器变量。<br> /// </summary><br> private System.ComponentModel.Container components = null;<br> <br> public Form1()<br> {<br> //<br> // Windows 窗体设计器支持所必需的<br> //<br> InitializeComponent();<br> <br> //<br> // TODO: 在 InitializeComponent 调用后添加任何构造函数代码<br> //<br> }<br> <br> /// <summary><br> /// 清理所有正在使用的资源。<br> /// </summary><br> protected override void Dispose( bool disposing )<br> {<br> if( disposing )<br> {<br> if (components != null) <br> {<br> components.Dispose();<br> }<br> }<br> base.Dispose( disposing );<br> }<br> <br> #region Windows 窗体设计器生成的代码<br> /// <summary><br> /// 设计器支持所需的方法 - 不要使用代码编辑器修改<br> /// 此方法的内容。<br> /// </summary><br> private void InitializeComponent()<br> {<br> this.label4 = new System.Windows.Forms.Label();<br> this.label3 = new System.Windows.Forms.Label();<br> this.label2 = new System.Windows.Forms.Label();<br> this.label1 = new System.Windows.Forms.Label();<br> this.btnStop = new System.Windows.Forms.Button();<br> this.btnSend = new System.Windows.Forms.Button();<br> this.btnConnect = new System.Windows.Forms.Button();<br> this.txtPort = new System.Windows.Forms.TextBox();<br> this.txtServer = new System.Windows.Forms.TextBox();<br> this.rtbSend = new System.Windows.Forms.RichTextBox();<br> this.rtbReceive = new System.Windows.Forms.RichTextBox();<br> this.statusBar1 = new System.Windows.Forms.StatusBar();<br> this.SuspendLayout();<br> // <br> // label4<br> // <br> this.label4.Location = new System.Drawing.Point(16, 152);<br> this.label4.Name = "label4";<br> this.label4.Size = new System.Drawing.Size(64, 23);<br> this.label4.TabIndex = 22;<br> this.label4.Text = "发送信息:";<br> // <br> // label3<br> // <br> this.label3.Location = new System.Drawing.Point(16, 64);<br> this.label3.Name = "label3";<br> this.label3.Size = new System.Drawing.Size(64, 23);<br> this.label3.TabIndex = 21;<br> this.label3.Text = "接收信息:";<br> // <br> // label2<br> // <br> this.label2.Location = new System.Drawing.Point(216, 16);<br> this.label2.Name = "label2";<br> this.label2.Size = new System.Drawing.Size(64, 23);<br> this.label2.TabIndex = 20;<br> this.label2.Text = "监听端口:";<br> // <br> // label1<br> // <br> this.label1.Location = new System.Drawing.Point(16, 16);<br> this.label1.Name = "label1";<br> this.label1.Size = new System.Drawing.Size(56, 23);<br> this.label1.TabIndex = 19;<br> this.label1.Text = "服务器:";<br> // <br> // btnStop<br> // <br> this.btnStop.Location = new System.Drawing.Point(256, 256);<br> this.btnStop.Name = "btnStop";<br> this.btnStop.TabIndex = 18;<br> this.btnStop.Text = "关闭连接";<br> this.btnStop.Click= new System.EventHandler(this.btnStop_Click);<br> // <br> // btnSend<br> // <br> this.btnSend.Location = new System.Drawing.Point(144, 256);<br> this.btnSend.Name = "btnSend";<br> this.btnSend.TabIndex = 17;<br> this.btnSend.Text = "发送信息";<br> this.btnSend.Click= new System.EventHandler(this.btnSend_Click);<br> // <br> // btnConnect<br> // <br> this.btnConnect.Location = new System.Drawing.Point(32, 256);<br> this.btnConnect.Name = "btnConnect";<br> this.btnConnect.TabIndex = 16;<br> this.btnConnect.Text = "请求连接";<br> this.btnConnect.Click= new System.EventHandler(this.btnConnect_Click);<br> // <br> // txtPort<br> // <br> this.txtPort.Location = new System.Drawing.Point(288, 16);<br> this.txtPort.Name = "txtPort";<br> this.txtPort.Size = new System.Drawing.Size(48, 21);<br> this.txtPort.TabIndex = 15;<br> this.txtPort.Text = "19811";<br> // <br> // txtServer<br> // <br> this.txtServer.Location = new System.Drawing.Point(72, 16);<br> this.txtServer.Name = "txtServer";<br> this.txtServer.TabIndex = 14;<br> this.txtServer.Text = "127.0.0.1";<br> // <br> // rtbSend<br> // <br> this.rtbSend.Location = new System.Drawing.Point(80, 152);<br> this.rtbSend.Name = "rtbSend";<br> this.rtbSend.Size = new System.Drawing.Size(264, 96);<br> this.rtbSend.TabIndex = 13;<br> this.rtbSend.Text = "";<br> // <br> // rtbReceive<br> // <br> this.rtbReceive.Location = new System.Drawing.Point(80, 56);<br> this.rtbReceive.Name = "rtbReceive";<br> this.rtbReceive.Size = new System.Drawing.Size(264, 96);<br> this.rtbReceive.TabIndex = 12;<br> this.rtbReceive.Text = "";<br> // <br> // statusBar1<br> // <br> this.statusBar1.Location = new System.Drawing.Point(0, 287);<br> this.statusBar1.Name = "statusBar1";<br> this.statusBar1.ShowPanels = true;<br> this.statusBar1.Size = new System.Drawing.Size(360, 22);<br> this.statusBar1.TabIndex = 23;<br> this.statusBar1.Text = "statusBar1";<br> // <br> // Form1<br> // <br> this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<br> this.ClientSize = new System.Drawing.Size(360, 309);<br> this.Controls.Add(this.statusBar1);<br> this.Controls.Add(this.label4);<br> this.Controls.Add(this.label3);<br> this.Controls.Add(this.label2);<br> this.Controls.Add(this.label1);<br> this.Controls.Add(this.btnStop);<br> this.Controls.Add(this.btnSend);<br> this.Controls.Add(this.btnConnect);<br> this.Controls.Add(this.txtPort);<br> this.Controls.Add(this.txtServer);<br> this.Controls.Add(this.rtbSend);<br> this.Controls.Add(this.rtbReceive);<br> this.Name = "Form1";<br> this.Text = "聊天程序-客户端";<br> this.Closing= new System.ComponentModel.CancelEventHandler(this.Form1_Closing);<br> this.ResumeLayout(false);<br> <br> }<br> #endregion<br> <br> /// <summary><br> /// 应用程序的主入口点。<br> /// </summary><br> <br> static void Main() <br> {<br> Application.Run(new Form1());<br> }<br> <br> private voi <
页:
[1]