用Socket实现的简单客户端和服务端程序(二)
2009-11-08 21:07
519 查看
在“用Socket实现的简单客户端和服务端程序(一)“的基础上
本实例是把Socket的收发用一个程序来实现的下面的是代码
SocketAppWinForm.Desinger.cs的代码如下
主要的逻辑控制在SocketAppWinForm.cs文件
本程序是在Windows 7 下用visusal stdio 2008调试通过。
本实例是把Socket的收发用一个程序来实现的下面的是代码
SocketAppWinForm.Desinger.cs的代码如下
namespace SocketAppWinForm { 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.StartListenButton = new System.Windows.Forms.Button(); this.IPTextBox = new System.Windows.Forms.TextBox(); this.PortTextBox = new System.Windows.Forms.TextBox(); this.ServerStateRichTextBox = new System.Windows.Forms.RichTextBox(); this.ReceiveMessageRichTextBox = new System.Windows.Forms.RichTextBox(); this.SendMessageRichTextBox = new System.Windows.Forms.RichTextBox(); this.SendMessageButton = new System.Windows.Forms.Button(); this.StopListenButton = 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.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // StartListenButton // this.StartListenButton.Location = new System.Drawing.Point(90, 418); this.StartListenButton.Name = "StartListenButton"; this.StartListenButton.Size = new System.Drawing.Size(75, 23); this.StartListenButton.TabIndex = 0; this.StartListenButton.Text = "开始监听"; this.StartListenButton.UseVisualStyleBackColor = true; this.StartListenButton.Click += new System.EventHandler(this.StartListenButton_Click); // // IPTextBox // this.IPTextBox.Location = new System.Drawing.Point(90, 56); this.IPTextBox.Name = "IPTextBox"; this.IPTextBox.Size = new System.Drawing.Size(153, 21); this.IPTextBox.TabIndex = 1; this.IPTextBox.Text = "127.0.0.1"; // // PortTextBox // this.PortTextBox.Location = new System.Drawing.Point(90, 131); this.PortTextBox.Name = "PortTextBox"; this.PortTextBox.Size = new System.Drawing.Size(153, 21); this.PortTextBox.TabIndex = 2; this.PortTextBox.Text = "80"; // // ServerStateRichTextBox // this.ServerStateRichTextBox.Location = new System.Drawing.Point(304, 56); this.ServerStateRichTextBox.Name = "ServerStateRichTextBox"; this.ServerStateRichTextBox.Size = new System.Drawing.Size(333, 132); this.ServerStateRichTextBox.TabIndex = 3; this.ServerStateRichTextBox.Text = ""; // // ReceiveMessageRichTextBox // this.ReceiveMessageRichTextBox.Location = new System.Drawing.Point(105, 205); this.ReceiveMessageRichTextBox.Name = "ReceiveMessageRichTextBox"; this.ReceiveMessageRichTextBox.Size = new System.Drawing.Size(532, 77); this.ReceiveMessageRichTextBox.TabIndex = 4; this.ReceiveMessageRichTextBox.Text = ""; // // SendMessageRichTextBox // this.SendMessageRichTextBox.Location = new System.Drawing.Point(105, 316); this.SendMessageRichTextBox.Name = "SendMessageRichTextBox"; this.SendMessageRichTextBox.Size = new System.Drawing.Size(532, 82); this.SendMessageRichTextBox.TabIndex = 5; this.SendMessageRichTextBox.Text = ""; // // SendMessageButton // this.SendMessageButton.Location = new System.Drawing.Point(247, 418); this.SendMessageButton.Name = "SendMessageButton"; this.SendMessageButton.Size = new System.Drawing.Size(75, 23); this.SendMessageButton.TabIndex = 6; this.SendMessageButton.Text = "发送信息"; this.SendMessageButton.UseVisualStyleBackColor = true; this.SendMessageButton.Click += new System.EventHandler(this.SendMessageButton_Click); // // StopListenButton // this.StopListenButton.Location = new System.Drawing.Point(414, 418); this.StopListenButton.Name = "StopListenButton"; this.StopListenButton.Size = new System.Drawing.Size(75, 23); this.StopListenButton.TabIndex = 7; this.StopListenButton.Text = "停止监听"; this.StopListenButton.UseVisualStyleBackColor = true; this.StopListenButton.Click += new System.EventHandler(this.StopListenButton_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(31, 59); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 8; this.label1.Text = "IP地址:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(31, 334); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 9; this.label2.Text = "发送信息:"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(31, 224); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(65, 12); this.label3.TabIndex = 10; this.label3.Text = "接收信息:"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(31, 134); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 11; this.label4.Text = "端口号:"; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(302, 41); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(65, 12); this.label5.TabIndex = 12; this.label5.Text = "主机状态:"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(690, 487); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.StopListenButton); this.Controls.Add(this.SendMessageButton); this.Controls.Add(this.SendMessageRichTextBox); this.Controls.Add(this.ReceiveMessageRichTextBox); this.Controls.Add(this.ServerStateRichTextBox); this.Controls.Add(this.PortTextBox); this.Controls.Add(this.IPTextBox); this.Controls.Add(this.StartListenButton); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button StartListenButton; private System.Windows.Forms.TextBox IPTextBox; private System.Windows.Forms.TextBox PortTextBox; private System.Windows.Forms.RichTextBox ServerStateRichTextBox; private System.Windows.Forms.RichTextBox ReceiveMessageRichTextBox; private System.Windows.Forms.RichTextBox SendMessageRichTextBox; private System.Windows.Forms.Button SendMessageButton; private System.Windows.Forms.Button StopListenButton; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; } }
主要的逻辑控制在SocketAppWinForm.cs文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace SocketAppWinForm { public partial class Form1 : Form { private IPAddress myIP = IPAddress.Parse("127.0.0.1"); public IPEndPoint MyServer; private Socket socke; private Socket accSocke; private Socket sendSocke; private bool check = true; public Form1() { InitializeComponent(); } private void round() { while (true) { int readBytes = 0; byte[] RecBuffer = new byte[64]; NetworkStream netStream = new NetworkStream(accSocke); readBytes = netStream.Read(RecBuffer, 0, RecBuffer.Length); if (readBytes <= 0) { break; } string RecMessage = System.Text.Encoding.UTF8.GetString(RecBuffer); ReceiveMessageRichTextBox.AppendText(RecMessage + "/r/n"); } } //线程同步的方法 public void accp() { MyServer = new IPEndPoint(myIP, Int32.Parse(PortTextBox.Text.Trim())); socke = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socke.Bind(MyServer); socke.Listen(50); Control.CheckForIllegalCrossThreadCalls = false; ServerStateRichTextBox.AppendText("主机:" + IPTextBox.Text + ",端口:" + PortTextBox.Text + " 开始监听....../r/n"); while (true) { accSocke = socke.Accept(); if (accSocke.Connected) { ServerStateRichTextBox.AppendText("与客户建立连接/n"); while (true) { int readBytes = 0; byte[] RecBuffer = new byte[64]; NetworkStream netStream = new NetworkStream(accSocke); readBytes = netStream.Read(RecBuffer, 0, RecBuffer.Length); if (readBytes <= 0) { break; } string RecMessage = System.Text.Encoding.UTF8.GetString(RecBuffer); ReceiveMessageRichTextBox.AppendText(RecMessage + "/r/n"); } } } } catch(Exception se) { MessageBox.Show(se.Message); } } //监听事件 private void StartListenButton_Click(object sender, EventArgs e) { try { myIP = Dns.GetHostByName(IPTextBox.Text.Trim()).AddressList[0]; } catch (Exception ex) { MessageBox.Show("您输入的IP地址格式不正确,请重新输入!"); } try { Thread thread = new Thread(new ThreadStart(accp)); thread.Start(); //accp(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void SendMessageButton_Click(object sender, EventArgs e) { sendSocke = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { sendSocke.Connect(MyServer); byte[] sendBuffer = new byte[64]; string sendMesage = SendMessageRichTextBox.Text.ToString(); NetworkStream NetSteam = new NetworkStream(sendSocke); sendBuffer = System.Text.Encoding.UTF8.GetBytes(sendMesage.ToCharArray()); NetSteam.Write(sendBuffer, 0, sendBuffer.Length); sendSocke.Shutdown(SocketShutdown.Both); sendSocke.Close(); } catch (Exception ex) { MessageBox.Show("连接尚未建立,发送失败!"); } } private void StopListenButton_Click(object sender, EventArgs e) { try { accSocke.Shutdown(SocketShutdown.Both); accSocke.Close(); socke.Close(); ServerStateRichTextBox.AppendText("主机:" + IPTextBox.Text.Trim() + ",端口:" + PortTextBox.Text.Trim() + " 监听关闭...../r/n"); } catch (Exception ex) { MessageBox.Show("监听尚未开始,关闭无效!"); } } private void Form1_Load(object sender, EventArgs e) { } } }
本程序是在Windows 7 下用visusal stdio 2008调试通过。
相关文章推荐
- 用Socket实现的简单客户端和服务端程序(一)
- 01 TCP Socket 编程_一个简单的服务端程序和客户端程序实现
- Java中利用socket实现简单的服务端与客户端的通信(入门级)
- 运用Java中socket编程实现简单的服务器客户端收发信息程序
- 利用socket TCP协议实现客户端与服务端简单通信
- linux网络编程之用socket实现简单客户端和服务端的通信(基于TCP)
- Java中利用socket实现简单的服务端与客户端的通信(中级)——实现任意双向通信
- python实现socket客户端和服务端简单示例
- 利用Tcp和socket实现的客户端与服务端的简单通信
- Socket编程简单实现服务端客户端连接
- Socket编程服务端和客户端互相通信,实现简单的聊天
- 实例:linux下实现简单的socket 客户端 服务端
- linux网络编程之用socket实现简单客户端和服务端的通信(基于TCP)
- nodejs socket实现的服务端和客户端简单通信
- 一个简单的socket服务端和客户端程序:客户端发送数据给服务端,服务端接收到数据后再给客户端发送数据
- linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)
- Socket——服务端与客户端交互的简单实现
- Socket实现客户端和服务端 简单的入门实例
- VC写的socket程序实现一个简单Echo服务器端和客户端程序
- 简单的socket连接,实现服务端可以获得多个客户端连接(备份,方便以后查询)