通过socket TCP方式建立链接,聊天软件--24章
服务器
namespace Win_Talk { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(6, 145); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 23; this.label3.Text = "内容:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(6, 24); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 22; this.label2.Text = "信息列表:"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(72, 10); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 21; this.label1.Text = "用户名:"; // // button3 // this.button3.Location = new System.Drawing.Point(304, 206); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 20; this.button3.Text = "关闭"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button2 // this.button2.Location = new System.Drawing.Point(220, 206); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 19; this.button2.Text = "发送"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click_1); // // textBox3 // this.textBox3.Location = new System.Drawing.Point(48, 142); this.textBox3.Multiline = true; this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(331, 55); this.textBox3.TabIndex = 18; // // textBox2 // this.textBox2.Enabled = false; this.textBox2.Location = new System.Drawing.Point(6, 42); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(376, 92); this.textBox2.TabIndex = 17; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(131, 7); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(188, 21); this.textBox1.TabIndex = 16; // // button1 // this.button1.Location = new System.Drawing.Point(325, 5); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(57, 23); this.button1.TabIndex = 15; this.button1.Text = "登录"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click_1); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(395, 236); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Frm_Server"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Threading; using System.Net.Sockets; namespace Win_Talk { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } #region//定义变量 IPAddress HostIP = IPAddress.Parse("127.0.0.1"); IPEndPoint point; Socket socket; bool flag = true; Socket acceptedSocket; #endregion #region//声名委托 delegate void SetTextCallback(string text); private void SetText(string text) { textBox2.AppendText(text + "\r\n"); } #endregion #region//进程方法 private void Proccess() { if (acceptedSocket.Connected) { while (flag) { byte[] receiveByte = new byte[64]; acceptedSocket.Receive(receiveByte, receiveByte.Length, 0); string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte); this.Invoke(new SetTextCallback(SetText), new object[] { strInfo }); } } } #endregion private void button3_Click(object sender, EventArgs e) { socket.Close(); acceptedSocket.Close(); } private void button2_Click_1(object sender, EventArgs e) { try { Byte[] sendByte = new Byte[64]; string sendStr = this.textBox1.Text + ":" + this.textBox3.Text + "\r\n"; sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray()); acceptedSocket.Send(sendByte, sendByte.Length, 0); } catch { } } private void button1_Click_1(object sender, EventArgs e) { HostIP = IPAddress.Parse("127.0.0.1"); try { point = new IPEndPoint(HostIP, Int32.Parse("11000")); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(point); socket.Listen(50); acceptedSocket = socket.Accept(); Thread thread = new Thread(new ThreadStart(Proccess)); thread.Start(); } catch (Exception ey) { MessageBox.Show(ey.Message); } } } }
2、客户端
namespace win_talkClient { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button3 // this.button3.Location = new System.Drawing.Point(310, 206); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 11; this.button3.Text = "关闭"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button2 // this.button2.Location = new System.Drawing.Point(226, 206); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 10; this.button2.Text = "发送"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // textBox3 // this.textBox3.Location = new System.Drawing.Point(54, 142); this.textBox3.Multiline = true; this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(331, 55); this.textBox3.TabIndex = 9; // // textBox2 // this.textBox2.Enabled = false; this.textBox2.Location = new System.Drawing.Point(12, 42); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(376, 92); this.textBox2.TabIndex = 8; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(137, 7); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(188, 21); this.textBox1.TabIndex = 7; // // button1 // this.button1.Location = new System.Drawing.Point(331, 5); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(57, 23); this.button1.TabIndex = 6; this.button1.Text = "登录"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(78, 10); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 12; this.label1.Text = "用户名:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 24); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 13; this.label2.Text = "信息列表:"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(12, 145); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 14; this.label3.Text = "内容:"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(401, 232); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Frm_Clinet"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Threading; using System.Net.Sockets; namespace win_talkClient { public partial class Form1 : Form { #region//声名变量 IPAddress HostIP = IPAddress.Parse("127.0.0.1"); IPEndPoint point; Socket socket; bool flag = true; #endregion #region//声名委托 delegate void SetTextCallback(string text); private void SetText(string text) { textBox2.AppendText(text + "\r\n"); } #endregion #region//进程 private void Proccess() { if (socket.Connected) { while (flag) { byte[] receiveByte = new byte[64]; socket.Receive(receiveByte, receiveByte.Length, 0); string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte); this.Invoke(new SetTextCallback(SetText),new object[]{strInfo}); } } } #endregion private void button2_Click(object sender, EventArgs e) { try { Byte[] sendByte = new Byte[64]; string sendStr = this.textBox1.Text + ":" + this.textBox3.Text+"\r\n"; sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray()); socket.Send(sendByte, sendByte.Length, 0); } catch { } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { HostIP = IPAddress.Parse("127.0.0.1"); try { point = new IPEndPoint(HostIP, Int32.Parse("11000")); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(point); Thread thread = new Thread(new ThreadStart(Proccess)); thread.Start(); } catch(Exception ey) { MessageBox.Show("服务器没有开启\r\n"+ey.Message); } } private void button3_Click(object sender, EventArgs e) { socket.Close(); } } }

浙公网安备 33010602011771号