如何进行C#网络编程服务器端程序的实现源码浅析
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,本篇文章为大家展示了如何进行C#网络编程服务器端程序的实现源码浅析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。C#网络编程服务器端程序实现源码是怎么样的呢?
千家信息网最后更新 2025年02月01日如何进行C#网络编程服务器端程序的实现源码浅析
本篇文章为大家展示了如何进行C#网络编程服务器端程序的实现源码浅析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
C#网络编程服务器端程序实现源码是怎么样的呢?让我们来看看其中重要的一部分:
由于在此次程序中我们采用的结构是异步阻塞方式,所以在实际的程序中,为了不影响服务器端程序的运行速度,我们在程序中设计了一个线程,使得对网络请求侦听,接受和发送数据都在线程中处理,请在下面的代码中注意这一点,下面是C#网络编程服务器端程序的完整代码:
//server.cs using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Net.Sockets ; using System.IO ; using System.Threading ; using System.Net ; //C#网络编程服务器端程序 //导入程序中使用到的名字空间 public class Form1 : Form { private ListBox ListBox1 ; private Button button2 ; private Label label1 ; private TextBox textBox1 ; private Button button1 ; private Socket socketForClient ; private NetworkStream networkStream ; private TcpListener tcpListener ; private StreamWriter streamWriter ; private StreamReader streamReader ; private Thread _thread1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { InitializeComponent ( ) ; } //C#网络编程服务器端程序 //清除程序中使用的各种资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void InitializeComponent ( ) { label1 = new Label ( ) ; button2 = new Button ( ) ; button1 = new Button ( ) ; ListBox1 = new ListBox ( ) ; textBox1 = new TextBox ( ) ; SuspendLayout ( ) ; label1.Location = new Point ( 8 , 168 ) ; label1.Name = "label1" ; label1.Size = new Size ( 120 , 23 ) ; label1.TabIndex = 3 ; label1.Text = "往客户端反馈信息:" ; //C#网络编程服务器端程序 //同样的方式设置其他控件,这里略去 this.Controls.Add ( button1 ) ; this.Controls.Add ( textBox1 ) ; this.Controls.Add ( label1 ) ; this.Controls.Add ( button2 ) ; this.Controls.Add ( ListBox1 ) ; this.MaximizeBox = false ; this.MinimizeBox = false ; this.Name = "Form1" ; this.Text = "C#的网络编程服务器端!" ; this.Closed += new System.EventHandler ( this.Form1_Closed ) ; this.ResumeLayout ( false ) ; } private void Listen ( ) { //C#网络编程服务器端程序 //创建一个tcpListener对象,此对象主要是对给定端口进行侦听 tcpListener = new TcpListener ( 1234 ) ; //开始侦听 tcpListener.Start ( ) ; //返回可以用以处理连接的Socket实例 socketForClient = tcpListener.AcceptSocket ( ) ; try { //如果返回值是"true",则产生的套节字已经接受来自远方的连接请求 if ( socketForClient.Connected ) { ListBox1.Items.Add ( "已经和客户端成功连接!" ) ; while ( true ) { //创建networkStream对象通过网络套节字来接受和发送数据 networkStream = new NetworkStream ( socketForClient ) ; //从当前数据流中读取一行字符,返回值是字符串 streamReader = new StreamReader ( networkStream ) ; string msg = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "收到客户端信息:" + msg ) ; streamWriter = new StreamWriter ( networkStream ) ; if ( textBox1.Text != "" ) { ListBox1.Items.Add ( "往客户端反馈信息:" + textBox1.Text ) ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 //C#网络编程服务器端程序 streamWriter.Flush ( ) ; } } } } catch ( Exception ey ) { MessageBox.Show ( ey.ToString ( ) ) ; } } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { ListBox1.Items .Add ( "服务已经启动!" ) ; _thread1 = new Thread ( new ThreadStart ( Listen ) ) ; _thread1.Start ( ) ; } private void button2_Click ( object sender , System.EventArgs e ) { //C#网络编程服务器端程序 //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; } private void Form1_Closed ( object sender , System.EventArgs e ) { //C#网络编程服务器端程序 //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; } }
C#网络编程服务器端程序的实现源码就向你介绍到这里,希望对你了解和学习C#网络编程服务器端程序有所帮助。
上述内容就是如何进行C#网络编程服务器端程序的实现源码浅析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
程序
网络
服务
服务器
网络编程
编程
数据
C#
源码
客户
客户端
线程
信息
字符
对象
数据流
一行
代码
内容
字符串
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海海事大学数据库怎么进
华为服务器报警代码h04
汕尾卫星软件开发供应商家
软件开发需要电脑什么配置
先通网络技术学院
软件开发资格证书
离岸公司管理境内服务器
易语言服务器怎么发数据
连云港服务器机柜采购
网络技术春考理论大纲
数据库怎么查询特定的数据
湖南正规软件开发应用
数据库表中如何设置唯一性
北京昌平新华互联网科技
什么是网络技术转让
新员工网络安全答案
大型网络安全培训课件
中电29所软件开发
中学生网络安全倡议书
数据库运维技术协议
数据库中定义主键是用来实施
海淀区信息化软件开发服务电话
安恒数据库审计
网络安全工作学期初工作计划
镇江云服务器供应商
200m宽带服务器租用价格
文科学软件开发
网络技术小组名字
苏州企业软件开发咨询报价
工程咨询 网络安全