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

推荐订阅源

SecWiki News
SecWiki News
量子位
The Cloudflare Blog
美团技术团队
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
博客园 - 司徒正美
宝玉的分享
宝玉的分享
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Secure Thoughts
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
Jina AI
Jina AI
博客园 - 聂微东
A
Arctic Wolf
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
T
Tailwind CSS Blog
The Hacker News
The Hacker News
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
TaoSecurity Blog
TaoSecurity Blog
Project Zero
Project Zero
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cloudbric
Cloudbric
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Troy Hunt's Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V2EX - 技术
V2EX - 技术
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog

博客园 - Borcala

我的文件 谈谈How to Programming-------各大数据库中的SQL语法Help权威解释,不会让人产生歧义! .NET环境下水晶报表使用总结 .net(C#)开发小技巧漫谈 奥本海默(J. Robert Oppenheimer) 怎样玩魔方集锦(转录) WEB编程开发常用的代码 ( 选择自 AppleBBS 的 Blog) - Borcala 一套较完整的技术框架 软件开发的六大阶段 (指针经典原创) 如何分层架构(杂记) 用户中心 - 博客园 用户中心 - 博客园 用户中心 - 博客园 ASP.NET 常用的33种代码(转,收藏一下,以备后查) C# Socket与实现 C#编程规范 unix、Linux知多少 解析.Net框架下的XML编程技术 java面试常见疑惑解答
简单的C# Socket编程(2)
Borcala · 2008-01-22 · via 博客园 - Borcala

只是一个简单的示例。

Server,服务器代码。
使用Socket套接字连接。

Client,客户端程序,在文本框中输入字符,将在列表框显示。

  1using System;
  2using System.Text;
  3using System.Drawing;
  4using System.Collections;
  5using System.ComponentModel;
  6using System.Windows.Forms;
  7using System.Net;
  8using System.Net.Sockets;
  9using System.IO;
 10
 11namespace SocketSample
 12{
 13    public class Sample : System.Windows.Forms.Form
 14    {
 15        private System.Windows.Forms.Button btS;
 16        private System.Windows.Forms.TextBox t1;
 17        private NetworkStream networkStream ;
 18        private StreamReader streamReader ;
 19        private StreamWriter streamWriter ;
 20        ArrayList sb;
 21        TcpClient myclient;
 22        bool flag=false;
 23        private System.Windows.Forms.ListBox t2;
 24
 25        private System.ComponentModel.Container components = null;
 26
 27        public Sample()
 28        {
 29            sb = new ArrayList();
 30            InitializeComponent();
 31            if(!flag)
 32                Connect();
 33
 34            //get a Network stream from the server
 35            networkStream = myclient.GetStream();
 36            streamReader = new StreamReader(networkStream);
 37            streamWriter = new StreamWriter(networkStream);
 38            ShowMessage();
 39        }

 40
 41        protected override void Dispose( bool disposing )
 42        {
 43            if( disposing )
 44            {
 45                if(components != null)
 46                {
 47                    components.Dispose();
 48                }

 49            }

 50            base.Dispose( disposing );
 51        }

 52
 53        #region Windows 窗体设计器生成的代码
 54        /// <summary>
 55        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 56        /// 此方法的内容。
 57        /// </summary>

 58        private void InitializeComponent()
 59        {
 60            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Sample));
 61            this.t1 = new System.Windows.Forms.TextBox();
 62            this.btS = new System.Windows.Forms.Button();
 63            this.t2 = new System.Windows.Forms.ListBox();
 64            this.SuspendLayout();
 65            // 
 66            // t1
 67            // 
 68            this.t1.Location = new System.Drawing.Point(2432);
 69            this.t1.Name = "t1";
 70            this.t1.Size = new System.Drawing.Size(28021);
 71            this.t1.TabIndex = 0;
 72            this.t1.Text = "";
 73            this.t1.TextChanged += new System.EventHandler(this.t1_TextChanged);
 74            // 
 75            // btS
 76            // 
 77            this.btS.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btS.BackgroundImage")));
 78            this.btS.Enabled = false;
 79            this.btS.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
 80            this.btS.Location = new System.Drawing.Point(32032);
 81            this.btS.Name = "btS";
 82            this.btS.TabIndex = 1;
 83            this.btS.Text = "Send";
 84            this.btS.Click += new System.EventHandler(this.btS_Click);
 85            // 
 86            // t2
 87            // 
 88            this.t2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
 89            this.t2.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
 90            this.t2.ItemHeight = 15;
 91            this.t2.Location = new System.Drawing.Point(2464);
 92            this.t2.Name = "t2";
 93            this.t2.Size = new System.Drawing.Size(368212);
 94            this.t2.TabIndex = 2;
 95            // 
 96            // Sample
 97            // 
 98            this.AutoScaleBaseSize = new System.Drawing.Size(614);
 99            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
100            this.ClientSize = new System.Drawing.Size(416297);
101            this.Controls.Add(this.t2);
102            this.Controls.Add(this.btS);
103            this.Controls.Add(this.t1);
104            this.Name = "Sample";
105            this.Text = "Sample";
106            this.ResumeLayout(false);
107
108        }

109        #endregion

110
111        public static void Main()
112        {
113            Sample df=new Sample();
114            df.FormBorderStyle=FormBorderStyle.Fixed3D;
115            Application.Run(df);
116        }

117
118        protected void Connect()
119        {            
120            //connect to the "localhost" at the give port
121            //if you have some other server name then you can use that instead of "localhost"
122    
123            try
124            {
125                sb.Add("Conneting to Server");
126                myclient = new TcpClient("localhost"1234);
127                sb.Add("Conneted,Please enter something in the textbox");
128            }

129            catch
130            {
131                sb.Add(string.Format("Failed to connect to server at {0}:1234""localhost"));
132            }

133            flag = true;
134        }

135
136        protected void ShowMessage()
137        {
138            for(int i=0;i<sb.Count;i++)
139            {
140                t2.Items.Add((object)sb[i].ToString());
141            }

142            sb.Clear();
143        }

144
145        private void t1_TextChanged(object sender, System.EventArgs e)
146        {
147            if(t1.Text == "" )
148                btS.Enabled = false;
149            else
150                btS.Enabled=true;
151        }

152
153        private void btS_Click(object sender, System.EventArgs e)
154        {
155            if(t1.Text=="")
156            {
157                sb.Add( "Please enter something in the textbox.");
158                t1.Focus();
159                return ;
160            }

161            string s;
162            try
163            {            
164                streamWriter.WriteLine(t1.Text);
165                Console.WriteLine("Sending Message");
166                streamWriter.Flush();
167                s= streamReader.ReadLine();
168                Console.WriteLine("Reading Message") ;
169                Console.WriteLine(s) ;
170                sb.Add(s);
171                t1.Text = "";
172                t1.Focus();
173                ShowMessage();
174            }

175            catch
176            {
177                MessageBox.Show("Error.");
178            }
    
179        }

180    }

181}

182