惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - Gofficer

决战紫禁之巅 为学 C#网页自动登录和提交POST信息的多种方法 一个C#写的调用外部进程类 快速实现在Windows应用程序中支持拖拽的TreeView控件(C#) PPT转图片 开发人员,敢问路在何方? C# 实现注销、关机、重启电脑功能 ultraGrid 控件中,实现单元格内容换行显示 如何用一条sql取得第10到第20条的记录? 用Sandcastle一键生成CHM帮助文档 实现服务器端与客户端对话 C#中访问WEB页面 使用代理服务器 自定义Ping方法 HTTP请求和应答 Socket套接字实现客户端连接 启动和停止本地系统进程 异常处理
Socket套接字实现服务器端连接
Gofficer · 2007-08-24 · via 博客园 - Gofficer

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7// 新添加的命令空间
  8using System.Net;
  9using System.Net.Sockets;
 10using System.Threading;
 11using System.Text;
 12using System.IO;
 13
 14namespace 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
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