-
函数指针
资源描述包含了指针排序,指针作为函数的参数,函数返回指针,指针与结构体。是一个学习C语言指针的好代码,十分适合初学者学习。
- 2022-02-20 02:49:17下载
- 积分:1
-
bitsharp 例子源码下载 bitcoin
bitsharp 例子源码下载 bitcoin
- 2014-10-23下载
- 积分:1
-
pop3client 读取邮件内容完整实例 有截图有源码下载
利用 LumiSoft.Net实现 读取邮件内容实例
- 2013-07-28下载
- 积分:1
-
C++实战源码-利用Watch调试窗口查看对象信息(入门级实例026).zip
C++实战源码-利用Watch调试窗口查看对象信息(入门级实例026).zip
- 2019-10-01下载
- 积分:1
-
wpf实现 wifi管理软件源码下载
wpf实现 wifi管理软件源码下载
- 2014-03-02下载
- 积分:1
-
c# 网络编程 实例源码(pdf)
我们知道 C#和 C++的差异之一,就是他本身没有类库,所使用的类库是.Net 框架中的类库--.Net FrameWork SDK。在.Net FrameWork SDK 中为网络编程提供了二个名称空间:"System.Net"和"System.Net.Sockets"。C#就是通过这二个名称空间中封装的类和方法实现网络通讯的。 首先我们解释一下在网络编程时候,经常遇到的几个概念:同步(synchronous)、异步(asynchronous)、阻塞(Block)和非阻塞(Unblock): 所谓同步方式,就是发送方发送数据包以后,丌等接受方响应,就接着发送下一个数据包。异步方式就是当发送方发送一个数据包以后,一直等到接受方响应后,才接着发送下一个数据包。而阻塞套接字是指执行此套接字的网络调用时,直到调用成功才返回,否则此套节字就一直阻塞在网络调用上,比如调用 StreamReader 类的 Readlin ( )方法读取网络缓冲区中的数据,如果调用的时候没有数据到达,那么此 Readlin ( )方法将一直挂在调用上,直到读到一些数据,此函数调用才返回;而非阻塞套接字是指在执行此套接字的网络调用时,丌管是否执行成功,都立即返回。同样调用 StreamReader 类的 Readlin ( )方法读取网络缓冲区中数据,丌管是否读到数据都立即返回,而丌会一直挂在此函数调用上。在 Windows网络通信软件开发中,最为常用的方法就是异步非阻塞套接字。平常所说的 C/S(客户端/服务器)结构的软件采用的方式就是异步非阻塞模式的。 其实在用 C#迚行网络编程中,我们并丌需要了解什么同步、异步、阻塞和非阻塞的原理和工作机制,因为在.Net FrameWrok SDK 中已经已经把这些机制给封装好了。下面我们就用 C#开一个具体的网络程序来说明一下问题。 一.本文中介绍的程序设计及运行环境 (1).微软视窗 2000 服务器版 (2)..Net Framework SDK Beta 2 以上版本 二.服务器端程序设计的关键步骤以及解决办法: 在下面接受的程序中,我们采用的是异步阻塞的方式。 (1).首先要要在给定的端口上面创建一个"tcpListener"对象侦听网络上面的请求。当接收到连结请求后通过调用"tcpListener"对象的"AcceptSocket"方法产生一个用于处理接入连接请求的 Socket 的实例。下面是具体实现代码: //创建一个 tcpListener 对象,此对象主要是对给定端口迚行侦听 tcpListener = new TcpListener ( 1234 ) ; //开始侦听 tcpListener.Start ( ) ; //返回可以用以处理连接的 Socket 实例 socketForClient = tcpListener.AcceptSocket ( ) ; (2).接受和发送客户端数据: 此时 Socket 实例已经产生,如果网络上有请求,在请求通过以后,Socket 实例构造一个"NetworkStream"对象,"NetworkStream"对象为网络访问提供了基础数据流。我们通过名称空间"System.IO"中封装的二个类"StreamReader"和"StreamWriter"来实现对"NetworkStream"对象的访问。其中"StreamReader"类中的 ReadLine ( )方法就是从"NetworkStream"对象中读取一行字符;"StreamWriter"类中的 WriteLine ( )方法就是对"NetworkStream"对象中写入一行字符串。从而实现在网络上面传输字符串,下面是具体的实现代码: 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 ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; } } } } catch ( Exception ey ) { MessageBox.Show ( ey.ToString ( ) ) ; } (3).最后别忘了要关闭所以流,停止侦听网络,关闭套节字,具体如下: //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; 三.C#网络编程服务器端程序的部分源代码(server.cs): 由于在此次程序中我们采用的结构是异步阻塞方式,所以在实际的程序中,为了丌影响服务器端程序的运行速度,我们在程序中设计了一个线程,使得对网络请求侦听,接受和发送数据都在线程中处理,请在下面的代码中注意这一点,下面是 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 ; //导入程序中使用到的名字空间 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 ( ) ; } //清除程序中使用的各种资源 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 = "往客户端反馈信息:" ; //同样的方式设置其他控件,这里略去 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 ( ) { //创建一个 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 ) ; //刷新当前数据流中的数据 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 ) { //关闭线程和流 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 ) { //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; } } 四.客户端程序设计的关键步骤以及解决办法: (1).连接到服务器端的指定端口: 我们采用的本地机既做服务器也做客户机,你可以通过修改 IP 地址来确定自己想要连接的服务器。我们在连接的时候采用了"TcpClient"类,此类是在较高的抽象级别(高于Socket 类)上面提供 TCP 服务。下面代码就是连接到本地机(端口为 1234),并获取响应流: //连接到服务器端口,在这里是选用本地机器作为服务器,你可以通过修改 IP 地址来改变服务器 try { myclient = new TcpClient ( "localhost" , 1234 ) ; } catch { MessageBox.Show ( "没有连接到服务器!" ) ; return ; } //创建 networkStream 对象通过网络套节字来接受和发送数据 networkStream = myclient.GetStream ( ) ; streamReader = new StreamReader ( networkStream ) ; streamWriter = new StreamWriter ( networkStream ) ; (2).实现接受和发送数据: 在接受和发送数据上面,我们依然采用了"NetworkStream"类,因为对他迚行操作比较简单,具体实现发送和接受还是通过命名空间"System.IO"中"StreamReader"类ReadLine ( )方法和"StreamWriter"类的 WriteLine ( )方法。具体的实现方法如下: if ( textBox1.Text == "" ) { MessageBox.Show ( "请确定文本框为非空!" ) ; textBox1.Focus ( ) ; return ; } try { string s ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; //从当前数据流中读取一行字符,返回值是字符串 s = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "读取服务器端发送内容:" s ) ; } catch ( Exception ee ) { MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" ee.ToString ( ) ) ; } (3).最后一步和服务器端是一样的,就是要关闭程序中创建的流,具体如下: streamReader.Close ( ) ; streamWriter.Close ( ) ; networkStream.Close ( ) ; 五.客户端的部分代码: 由于在客户端丌需要侦听网络,所以在调用上面没有程序阻塞情冴,所以在下面的代码中,我们没有使用到线程,这是和服务器端程序的一个区别的地方。总结上面的这些关键步骤,可以得到一个用 C#网络编程 完整的客户端程序(client.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 ; //导入程序中使用到的名字空间 public class Form1 : Form { private ListBox ListBox1 ; private Label label1 ; private TextBox textBox1 ; private Button button3 ; private NetworkStream networkStream ; private StreamReader streamReader ; private StreamWriter streamWriter ; TcpClient myclient ; private Label label2 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { InitializeComponent ( ) ; } //清除程序中使用的各种资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void InitializeComponent ( ) { label1 = new Label ( ) ; button3 = new Button ( ) ; ListBox1 = new ListBox ( ) ; textBox1 = new TextBox ( ) ; label2 = new Label ( ) ; SuspendLayout ( ) ; label1.Location = new Point ( 8 , 168 ) ; label1.Name = "label1" ; label1.Size = new Size ( 56 , 23 ) ; label1.TabIndex = 3 ; label1.Text = "信息:" ; //同样方法设置其他控件 AutoScaleBaseSize = new Size ( 6 , 14 ) ; ClientSize = new Size ( 424 , 205 ) ; this.Controls.Add ( button3 ) ; this.Controls.Add ( textBox1 ) ; this.Controls.Add ( label1 ) ; this.Controls.Add ( label2 ) ; 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 ) ; //连接到服务器端口,在这里是选用本地机器作为服务器,你可以通过修改 IP 地址来改变服务器 try { myclient = new TcpClient ( "localhost" , 1234 ) ; } catch { MessageBox.Show ( "没有连接到服务器!" ) ; return ; } //创建 networkStream 对象通过网络套节字来接受和发送数据 networkStream = myclient.GetStream ( ) ; streamReader = new StreamReader ( networkStream ) ; streamWriter = new StreamWriter ( networkStream ) ; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button3_Click ( object sender , System.EventArgs e ) { if ( textBox1.Text == "" ) { MessageBox.Show ( "请确定文本框为非空!" ) ; textBox1.Focus ( ) ; return ; } try { string s ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; //从当前数据流中读取一行字符,返回值是字符串 s = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "读取服务器端发送内容:" s ) ; } catch ( Exception ee ) { MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" ee.ToString ( ) ) ; } } private void Form1_Closed ( object sender , System.EventArgs e ) { streamReader.Close ( ) ; streamWriter.Close ( ) ; networkStream.Close ( ) ; } } 下图是编译上面二个程序后运行的界面: 图 01:C#编写网络程序运行界面 七.总结: 虽然在.Net FrameWrok SDK 中只为网络编程提供了二个命名空间,但这二个命名空间中的内容却是十分丰富的,C#利用这二个命名空间既可以实现同步和异步,也可以实现阻塞和非阻塞。本文通过用 C#编写一个网络上信息传输的程序,展现了其丰富的内容,由于篇幅所限,更深,更强大的功能还需要读者去实践、探索。
- 2014-07-07下载
- 积分:1
-
C#调用visionpro
C#调用visionpro
- 2020-12-08下载
- 积分:1
-
C# 制作系统服务 实例源码下载
附件中有详细的安装使用文档,大概步骤如下:1.新建Windows项目,选择"Windows服务"类型的工程。2.生成的Program.cs文件中,定义了服务启动的Main函数。 代码 namespace WindowsService1{ static class Program { /// /// 应用程序的主入口点。 /// static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } }} 3.在新建的工程中,点击Service1.cs文件,切换到代码视图,生成的代码继承于ServiceBase基类,并重载了OnStart和OnStop方法。我在这个文件中进行了一些简单的操作,就是在服务开始的时候,定义一个定时器,然后每隔1秒钟,向文件中写入当前时间。 代码 namespace WindowsService1{ public partial class Service1 : ServiceBase { Timer timer; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer = new Timer(1000); timer.Elapsed = new ElapsedEventHandler(timer_Elapsed); timer.Start(); } protected override void OnStop() { timer.Stop(); timer.Dispose(); } void timer_Elapsed(object sender, ElapsedEventArgs e) { string filePath = AppDomain.CurrentDomain.BaseDirectory "test.txt"; StreamWriter sw = null; if (!File.Exists(filePath)) { sw = File.CreateText(filePath); } else { sw = File.AppendText(filePath); } sw.Write("访问时间:" DateTime.Now.ToString() Environment.NewLine); sw.Close(); } }}4.向工程中添加一个安装程序类。 4.在新添加的安装程序类中,设定服务的名称,启动方式,账号名和密码等信息。 代码 namespace WindowsService1{ partial class Installer1 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; private System.ServiceProcess.ServiceProcessInstaller spInstaller; private System.ServiceProcess.ServiceInstaller sInstaller; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { components = new System.ComponentModel.Container(); // 创建ServiceProcessInstaller对象和ServiceInstaller对象 this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller(); this.sInstaller = new System.ServiceProcess.ServiceInstaller(); // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息 this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.spInstaller.Password = null; this.spInstaller.Username = null; // 设定服务的名称 this.sInstaller.ServiceName = "WindowsService1"; //设定服务启动的方式 this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic; this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller,this.sInstaller}); } #endregion }}5.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,我安装的是VS 2010,对应的目录为:C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe,然后执行InstallUtil.exe 待执行的exe文件的目录,如:InstallUtil.exe F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。 6.启动该服务,这时打开binDebug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。7.停止服务的操作也和简单,打开命令行工具,转到C:WindowsMicrosoft.NETFrameworkv4.0.30319目录,然后执行InstallUtil.exe - u F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe命令就可以了。
- 2015-04-21下载
- 积分:1
-
软件公司网站源码(含前后台完整源码以及数据库)
1.项目前台:LYSC.CompanyWeb.UI/Index.html2.项目后台:LYSC.CompanyWeb.UI/admin/Login/Login.aspx3.项目数据库脚本:LYSC.CompanyWeb.UI/DateFile4.项目说明以及开发流程:LYSC.CompanyWeb.UI/DateFile5.此项目是基于VS2012开发的,数据库是VS20126..NET版本是.net FrameWork 4.57.如须知道开发过程中遇到的问题,可以查看第4步的文件
- 2015-06-28下载
- 积分:1
-
C# 批量数据上传客户端例子源码 FastDFS
C# 批量数据上传客户端例子源码 FastDFS
- 2014-10-23下载
- 积分:1