Socket套接字实现服务器端连接

  1 using  System;
  2 using  System.Drawing;
  3 using  System.Collections;
  4 using  System.ComponentModel;
  5 using  System.Windows.Forms;
  6 using  System.Data;
  7 //  新添加的命令空间
  8 using  System.Net;
  9 using  System.Net.Sockets;
 10 using  System.Threading;
 11 using  System.Text;
 12 using  System.IO;
 13
 14 namespace  SocketServer
 15 {
 16    /**//// <summary>
 17    /// Socket套接字实现服务器端连接。
 18    /// </summary>

 19    public class Form1 : System.Windows.Forms.Form
 20    {
 21        private System.Windows.Forms.RichTextBox richTextBox1;
 22        private System.Windows.Forms.Button button1;
 23        // 客户端节点
 24        private IPEndPoint      client;
 25        // 服务器端Socket
 26        private Socket          server;
 27        // 服务器端监听线程
 28        private Thread          thdSvr;
 29        // 网络数据流
 30        NetworkStream            stream;
 31        // 写数据流
 32        TextWriter                writer;
 33        // 读数据流
 34        TextReader                reader;
 35        /**//// <summary>
 36        /// 必需的设计器变量。
 37        /// </summary>

 38        private System.ComponentModel.Container components = null;
 39        // 监听线程
 40        private void ThreadServer()
 41        {
 42            client = new IPEndPoint(IPAddress.Any, 34567);
 43            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 44            server.Blocking = true;
 45            server.Bind(client);
 46            server.Listen(0);
 47            while(true)
 48            {
 49                Socket t = server.Accept();
 50                if (t != null)
 51                {
 52                    stream = new NetworkStream(t);
 53                    writer = new StreamWriter(stream);
 54                    reader = new StreamReader(stream);
 55                    richTextBox1.Text += "接收到一个连接\n";
 56                    writer.WriteLine("欢迎连接到服务器!");
 57                    writer.Flush();
 58                    writer.WriteLine("您现在可以说话了");
 59                    writer.Flush();
 60                    richTextBox1.Text += reader.ReadLine();
 61                    richTextBox1.Text += "\n";
 62                    reader.Close();
 63                    writer.Close();
 64                    stream.Close();
 65                    t.Close();
 66                }

 67            }

 68        }

 69        public Form1()
 70        {
 71            //
 72            // Windows 窗体设计器支持所必需的
 73            //
 74            InitializeComponent();
 75            //
 76            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 77            //
 78        }

 79
 80        /**//// <summary>
 81        /// 清理所有正在使用的资源。
 82        /// </summary>

 83        protected override void Dispose( bool disposing )
 84        {
 85            if(server != null)
 86                server.Close();
 87            if(!button1.Enabled)
 88                if(thdSvr != null)
 89                    thdSvr.Abort();
 90            if( disposing )
 91            {
 92                if (components != null)
 93                {
 94                    components.Dispose();
 95                }

 96            }

 97            base.Dispose( disposing );
 98        }

 99
100        Windows Form Designer generated code#region Windows Form Designer generated code
101        /**//// <summary>
102        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
103        /// 此方法的内容。
104        /// </summary>

105        private void InitializeComponent()
106        {
107            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
108            this.button1 = new System.Windows.Forms.Button();
109            this.SuspendLayout();
110            //
111            // richTextBox1
112            //
113            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
114            this.richTextBox1.Name = "richTextBox1";
115            this.richTextBox1.Size = new System.Drawing.Size(416256);
116            this.richTextBox1.TabIndex = 0;
117            this.richTextBox1.Text = "";
118            //
119            // button1
120            //
121            this.button1.Location = new System.Drawing.Point(152264);
122            this.button1.Name = "button1";
123            this.button1.TabIndex = 1;
124            this.button1.Text = "启动服务器";
125            this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
126            this.button1.Click += new System.EventHandler(this.button1_Click);
127            //
128            // Form1
129            //
130            this.AutoScaleBaseSize = new System.Drawing.Size(818);
131            this.ClientSize = new System.Drawing.Size(416296);
132            this.Controls.AddRange(new System.Windows.Forms.Control[] {
133                                                                          this.button1,
134                                                                          this.richTextBox1}
);
135            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
136            this.Name = "Form1";
137            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
138            this.Text = "服务器";
139            this.ResumeLayout(false);
140
141        }

142        #endregion

143
144        /**//// <summary>
145        /// 应用程序的主入口点。
146        /// </summary>

147        [STAThread]
148        static void Main()
149        {
150            Application.Run(new Form1());
151        }

152        // 开始服务器端的运行。
153        private void button1_Click(object sender, System.EventArgs e)
154        {
155            thdSvr = new Thread(new ThreadStart(this.ThreadServer));
156            thdSvr.Start();
157            button1.Enabled = false;
158        }

159    }

160}

161

转载于:https://www.cnblogs.com/gofficer/archive/2007/08/24/868031.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值