📄 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;
namespace ServerSock
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private IPAddress myIP=IPAddress.Parse("127.0.0.1");//服务器端IP
private IPEndPoint MyServer;//服务器端
private Socket sock;//服务器端套接字
private Socket accSock;//客户端套接字
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(560, 128);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(0, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器IP";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(0, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 2;
this.label2.Text = "监听端口";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBox3);
this.groupBox2.Location = new System.Drawing.Point(280, 8);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(264, 120);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "服务器状态";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(0, 24);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(256, 88);
this.textBox3.TabIndex = 0;
this.textBox3.Text = "";
this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.richTextBox1);
this.groupBox3.Controls.Add(this.label4);
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Controls.Add(this.richTextBox2);
this.groupBox3.Location = new System.Drawing.Point(0, 128);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(568, 136);
this.groupBox3.TabIndex = 1;
this.groupBox3.TabStop = false;
//
// label3
//
this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(8, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 23);
this.label3.TabIndex = 1;
this.label3.Text = "接受信息";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(8, 88);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 23);
this.label4.TabIndex = 2;
this.label4.Text = "发送信息";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(88, 8);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(456, 56);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(88, 72);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(456, 56);
this.richTextBox2.TabIndex = 4;
this.richTextBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 288);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(144, 288);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "发送消息";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(272, 288);
this.button3.Name = "button3";
this.button3.TabIndex = 4;
this.button3.Text = "停止监听";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 342);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "服务器端";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void textBox3_TextChanged(object sender, System.EventArgs e)
{
}
/// <summary>
/// 开始监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
try
{
myIP=IPAddress.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("你输入的IP地址不正确,请重新输入!");
}
try
{
Thread thread=new Thread(new ThreadStart(accp));;
thread.Start();
}
catch(Exception ee)
{
textBox3.AppendText(ee.Message);
}
}
/// <summary>
/// 线程同步
/// </summary>
private void accp()
{
MyServer=new IPEndPoint(myIP,int.Parse(textBox2.Text));;
sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.Bind(MyServer);
textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"开始监听.....\r\n");
while(true)
{
accSock=sock.Accept();
if(accSock.Connected)
{
textBox3.AppendText("与客户建立连接");
Thread thread=new Thread(new ThreadStart(round));
thread.Start();
}
}
}
private void round()
{
while(true)
{
Byte[] Rec=new byte[64];
NetworkStream netStream=new NetworkStream(accSock);
netStream.Read(Rec,0,Rec.Length);
string RecMessage=System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage+"\r\n");
}
}
/// <summary>
/// 发送信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e)
{
try
{
Byte[] sendByte=new byte[64];
string send=richTextBox2.Text+"\r\n";
NetworkStream netStream=new NetworkStream(accSock);
sendByte=System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
netStream.Write(sendByte,0,sendByte.Length);
}
catch
{
MessageBox.Show("连接尚未建立!");
}
}
/// <summary>
/// 停止监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
try
{
sock.Close();
textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"监听停止!\r\n");;
}
catch
{
MessageBox.Show("监听尚未开始,关闭无效!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -