C# 2.0 套接字编程实例初探

2016-02-19 15:08 2 1 收藏

今天图老师小编要向大家分享个C# 2.0 套接字编程实例初探教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - 编程语言 】

  首先从原理上解释一下采用Socket接口的网络通讯,这里以最常用的C/S模式作为范例,首先,服务端有一个进程(或多个进程)在指定的端口等待客户来连接,服务程序等待客户的连接信息,一旦连接上之后,就可以按设计的数据交换方法和格式进行数据传输。客户端在需要的时刻发出向服务端的连接请求。这里为了便于理解,提到了一些调用及其大致的功能。使用socket调用后,仅产生了一个可以使用的socket描述符,这时还不能进行通信,还要使用其他的调用,以使得socket所指的结构中使用的信息被填写完。

  在使用TCP协议时,一般服务端进程先使用socket调用得到一个描述符,然后使用bind调用将一个名字与socket描述符连接起来,对于Internet域就是将Internet地址联编到socket。之后,服务端使用listen调用指出等待服务请求队列的长度。然后就可以使用accept调用等待客户端发起连接,一般是阻塞等待连接,一旦有客户端发出连接,accept返回客户的地址信息,并返回一个新的socket描述符,该描述符与原先的socket有相同的特性,这时服务端就可以使用这个新的socket进行读写操作了。一般服务端可能在accept返回后创建一个新的进程进行与客户的通信,父进程则再到accept调用处等待另一个连接。客户端进程一般先使用socket调用得到一个socket描述符,然后使用connect向指定的服务器上的指定端口发起连接,一旦连接成功返回,就说明已经建立了与服务器的连接,这时就可以通过socket描述符进行读写操作了。

  .NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  Socket类 这个低层的类用于管理连接,WebRequest,TcpClient和UdpClient在内部使用这个类。

  NetworkStream类 这个类是从Stream派生出来的,它表示来自网络的数据流

  TcpClient类 允许创建和使用TCP连接

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  TcpListener类 允许监听传入的TCP连接请求

  UdpClient类 用于UDP客户创建连接(UDP是另外一种TCP协议,但没有得到广泛的使用,主要用于本地网络)

  下面我们来看一个基于Socket的双机通信代码的C#版本

  首先创建Socket对象的实例,这可以通过Socket类的构造方法来实现:

  

public Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType); 

  其中,addressFamily 参数指定 Socket 使用的寻址方案,socketType 参数指定 Socket 的类型,protocolType 参数指定 Socket 使用的协议。

  下面的示例语句创建一个 Socket,它可用于在基于 TCP/IP 的网络(如 Internet)上通讯。

  

Socket temp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

  若要使用 UDP 而不是 TCP,需要更改协议类型,如下面的示例所示:

  

Socket temp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 

  一旦创建 Socket,在客户端,你将可以通过Connect方法连接到指定的服务器(你可以在Connect方法前Bind端口,就是以指定的端口发起连接,如果不事先Bind端口号的话,系统会默认在1024到5000随机绑定一个端口号),并通过Send方法向远程服务器发送数据,而后可以通过Receive从服务端接收数据;而在服务器端,你需要使用Bind方法绑定所指定的接口使Socket与一个本地终结点相联,并通过Listen方法侦听该接口上的请求,当侦听到用户端的连接时,调用Accept完成连接的操作,创建新的Socket以处理传入的连接请求。使用完 Socket 后,使用 Close 方法关闭 Socket。

  可以看出,以上许多方法包含EndPoint类型的参数,在Internet中,TCP/IP 使用一个网络地址和一个服务端口号来唯一标识设备。网络地址标识网络上的特定设备;端口号标识要连接到的该设备上的特定服务。网络地址和服务端口的组合称为终结点,在 .NET 框架中正是由 EndPoint 类表示这个终结点,它提供表示网络资源或服务的抽象,用以标志网络地址等信息。.Net同时也为每个受支持的地址族定义了 EndPoint 的子代;对于 IP 地址族,该类为 IPEndPoint。IPEndPoint 类包含应用程序连接到主机上的服务所需的主机和端口信息,通过组合服务的主机IP地址和端口号,IPEndPoint 类形成到服务的连接点。

  用到IPEndPoint类的时候就不可避免地涉及到计算机IP地址,System.Net命名空间中有两种类可以得到IP地址实例:

  IPAddress类:IPAddress 类包含计算机在 IP 网络上的地址。其Parse方法可将 IP 地址字符串转换为 IPAddress 实例。下面的语句创建一个 IPAddress 实例:

  

IPAddress myIP = IPAddress.Parse("192.168.0.1"); 

  需要知道的是:Socket 类支持两种基本模式:同步和异步。其区别在于:在同步模式中,按块传输,对执行网络操作的函数(如 Send 和 Receive)的调用一直等到所有内容传送操作完成后才将控制返回给调用程序。在异步模式中,是按位传输,需要指定发送的开始和结束。同步模式是最常用的模式,我们这里的例子也是使用同步模式。

  下面看一个完整的例子,client向server发送一段测试字符串,server接收并显示出来,给予client成功响应。

  

//client端using System;using System.Text;using System.IO;using System.Net;using System.Net.Sockets;namespace socketsample{ class Class1 {  static void Main()  {   try   {    int port = 2000;    string host = "127.0.0.1";    IPAddress ip = IPAddress.Parse(host);    IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket    Console.WriteLine("Conneting...");    c.Connect(ipe);//连接到服务器    string sendStr = "hello!This is a socket test";    byte[] bs = Encoding.ASCII.GetBytes(sendStr);    Console.WriteLine("Send Message");    c.Send(bs, bs.Length, 0);//发送测试信息    string recvStr = "";    byte[] recvBytes = new byte[1024];    int bytes;    bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);    Console.WriteLine("Client Get Message:{0}", recvStr);//显示服务器返回信息    c.Close();   }   catch (ArgumentNullException e)   {    Console.WriteLine("ArgumentNullException: {0}", e);   }   catch (SocketException e)   {    Console.WriteLine("SocketException: {0}", e);   }   Console.WriteLine("Press Enter to Exit");   Console.ReadLine();  } }}//server端using System;using System.Text;using System.IO;using System.Net;using System.Net.Sockets;namespace Project1{ class Class2 {  static void Main()  {   try   {    int port = 2000;    string host = "127.0.0.1";    IPAddress ip = IPAddress.Parse(host);    IPEndPoint ipe = new IPEndPoint(ip, port);    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类    s.Bind(ipe);//绑定2000端口    s.Listen(0);//开始监听    Console.WriteLine("Wait for connect");    Socket temp = s.Accept();//为新建连接创建新的Socket。    Console.WriteLine("Get a connect");    string recvStr = "";    byte[] recvBytes = new byte[1024];    int bytes;    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);    Console.WriteLine("Server Get Message:{0}",recvStr);//把客户端传来的信息显示出来    string sendStr = "Ok!Client Send Message Sucessful!";    byte[] bs = Encoding.ASCII.GetBytes(sendStr);    temp.Send(bs, bs.Length, 0);//返回客户端成功信息    temp.Close();    s.Close();   }   catch (ArgumentNullException e)   {    Console.WriteLine("ArgumentNullException: {0}", e);   }   catch (SocketException e)   {    Console.WriteLine("SocketException: {0}", e);   }   Console.WriteLine("Press Enter to Exit");   Console.ReadLine();  } }}

  上面的例子是用的Socket类,System.Net.Socket命名空间还提供了两个抽象高级类TCPClient和UDPClient和用于通讯流处理的NetWorkStream,让我们看下例子

  客户端

  

TcpClient tcpClient=new TcpCLient(主机IP,端口号);NetworkStream ns=tcp.Client.GetStream();

  服务端

  

TcpListener tcpListener=new TcpListener(监听端口);tcpListener.Start();TcpClient tcpClient=tcpListener.AcceptTcpClient();NetworkStream ns=tcpClient.GetStream();

  服务端用TcpListener监听,然后把连接的对象实例化为一个TcpClient,调用TcpClient.GetStream()方法,返回网络流实例化为一个NetworlStream流,下面就是用流的方法进行Send,Receive

  如果是UdpClient的话,就直接UdpClient实例化,然后调用UdpClient的Send和Receive方法,需要注意的事,UdpClient没有返回网络流的方法,就是说没有GetStream方法,所以无法流化,而且使用Udp通信的时候,不要服务器监听。

  现在我们大致了解了.Net Socket通信的流程,下面我们来作一个稍微复杂点的程序,一个广播式的C/S聊天程序。

  客户端设计需要一个1个ListBox,用于显示聊天内容,一个TextBox输入你要说的话,一个Button发送留言,一个Button建立连接。

  点击建立连接的Button后出来一个对话框,提示输入连接服务器的IP,端口,和你的昵称,启动一个接受线程,负责接受从服务器传来的信息并显示在ListBox上面。

  服务器端2个Button,一个启动服务,一个T掉已建立连接的客户端,一个ListBox显示连接上的客户端的Ip和端口。

  比较重要的地方是字符串编码的问题,需要先把需要传送的字符串按照UTF8编码,然后接受的时候再还原成为GB2312,不然中文显示会是乱码。

  还有一个就是接收线程,我这里简单写成一个While(ture)循环,不断判断是否有信息流入,有就接收,并显示在ListBox上,这里有问题,在.Net2.0里面,交错线程修改窗体空间属性的时候会引发一个异常,不可以直接修改,需要定义一个委托来修改。

  当客户端需要断开连接的时候,比如点击窗体右上角的XX,就需要定义一个this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Closing);(.Net2.0是FormClosing系统事件),在Closing()函数里面,发送Close字符给服务端,服务器判断循环判断所有的连接上的客户端传来的信息,如果是以Close开头,断开与其的连接。看到这里,读者就会问了,如果我在聊天窗口输入Close是不是也断开连接呢?不是的,在聊天窗口输入的信息传给服务器的时候开头都要加上Ip信息和昵称,所以不会冲突。

  客户端

  

// Form1.Designer.csnamespace ClientTest{  partial class Form1  {    /// summary    /// Required designer variable.    /// /summary    private System.ComponentModel.IContainer components = null;    /// summary    /// Clean up any resources being used.    /// /summary    /// param name="disposing"true if managed resources should be disposed; otherwise, false./param    protected override void Dispose(bool disposing)    {      if (disposing && (components != null))      {        components.Dispose();      }      base.Dispose(disposing);    }    #region Windows Form Designer generated code    /// summary    /// Required method for Designer support - do not modify    /// the contents of this method with the code editor.    /// /summary    private void InitializeComponent()    {      this.listBox1 = new System.Windows.Forms.ListBox();      this.button1 = new System.Windows.Forms.Button();      this.textBox1 = new System.Windows.Forms.TextBox();      this.button3 = new System.Windows.Forms.Button();      this.SuspendLayout();      //      // listBox1      //      this.listBox1.FormattingEnabled = true;      this.listBox1.ItemHeight = 12;      this.listBox1.Location = new System.Drawing.Point(12, 12);      this.listBox1.Name = "listBox1";      this.listBox1.Size = new System.Drawing.Size(268, 148);      this.listBox1.TabIndex = 0;      //      // button1      //      this.button1.Location = new System.Drawing.Point(12, 166);      this.button1.Name = "button1";      this.button1.Size = new System.Drawing.Size(268, 35);      this.button1.TabIndex = 1;      this.button1.Text = "连接";      this.button1.UseVisualStyleBackColor = true;      this.button1.Click += new System.EventHandler(this.button1_Click);      //      // textBox1      //      this.textBox1.Location = new System.Drawing.Point(12, 207);      this.textBox1.Multiline = true;      this.textBox1.Name = "textBox1";      this.textBox1.Size = new System.Drawing.Size(211, 54);      this.textBox1.TabIndex = 3;      //      // button3      //      this.button3.Location = new System.Drawing.Point(229, 207);      this.button3.Name = "button3";      this.button3.Size = new System.Drawing.Size(51, 54);      this.button3.TabIndex = 4;      this.button3.Text = "发言";      this.button3.UseVisualStyleBackColor = true;      this.button3.Click += new System.EventHandler(this.button3_Click);      //      // Form1      //      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;      this.ClientSize = new System.Drawing.Size(292, 273);      this.Controls.Add(this.button3);      this.Controls.Add(this.textBox1);      this.Controls.Add(this.button1);      this.Controls.Add(this.listBox1);      this.Name = "Form1";      this.Text = "客户端";      this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Closing);      this.Load += new System.EventHandler(this.Form1_Load);      this.ResumeLayout(false);      this.PerformLayout();    }    #endregion    private System.Windows.Forms.ListBox listBox1;    private System.Windows.Forms.Button button1;    private System.Windows.Forms.TextBox textBox1;    private System.Windows.Forms.Button button3;  }}//Form1.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.IO;using System.Threading;namespace ClientTest{  public partial class Form1 : Form  {    Socket client;    public string host = "";//要连接的服务器IP    int port = 2000;    Thread Data;    public string nick = "";//给自己起一个昵称,呵呵    Encoding utf8 = Encoding.UTF8;    Encoding gb2312 = Encoding.GetEncoding("GB2312");    delegate void SetSafe(string text);    public Form1()    {      InitializeComponent();    }    private void button1_Click(object sender, EventArgs e)//连接服务器    {      Form2 frm2 = new Form2();      frm2.ShowDialog();//显示一个对话框,输入服务器IP和自己的昵称      host = frm2.textBox2.Text;      nick=frm2.textBox1.Text;      try      {        client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);        client.Connect(host, port);//连接服务器      }      catch (Exception ex)      {        MessageBox.Show(ex.Message);      }    }    private void Form1_Load(object sender, EventArgs e)//窗体载入时候开启一个数据处理线程,处理从服务器得到的数据    {      Data = new Thread(new ThreadStart(DataStart));      Data.Start();    }    public void DataStart()//只要能从服务器得到数据,就显示在ListBox上,这个过程一直循环    {      while (true)      {        try        {          NetworkStream ns = new NetworkStream(client);          StreamReader reader = new StreamReader(ns);          string result =reader.ReadLine();          byte[] u = utf8.GetBytes(result);          byte[] gb = Encoding.Convert(utf8, gb2312, u);          string sGb = gb2312.GetString(gb);          AddList(sGb);          reader.Close();          ns.Close();        }        catch { }        Thread.Sleep(1000);      }    }    private void button3_Click(object sender, EventArgs e)//发言    {      try      {        string sendString = "("+ client.LocalEndPoint.ToString()+")"+nick+"说"+textBox1.Text + "rn";        byte[] butf = utf8.GetBytes(sendString);        client.Send(butf);//按照昵称(IP)说的话的格式发送给服务器      }      catch { }    }    private void AddList(object text)    {      // InvokeRequired required compares the thread ID of the      // calling thread to the thread ID of the creating thread.      // If these threads are different, it returns true.      if (this.listBox1.InvokeRequired)      {        SetSafe d = new SetSafe(AddList);        this.Invoke(d, new object[] { text });      }      else      {        this.listBox1.Items.Add(text);      }    }    private void Closing(object sender, System.EventArgs e)//断开连接,发送服务器"Close"信息    {      try      {        Data.Abort();        Byte[] buf = System.Text.Encoding.UTF8.GetBytes("Close" + "rn");        client.Send(buf);      }      catch { }    }  }}//Form2.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace ClientTest{  public partial class Form2 : Form  {    public Form2()    {      InitializeComponent();    }    private void button1_Click(object sender, EventArgs e)    {      this.Hide();         }  }}服务端// Form1.Designer.csnamespace ServerTest{  partial class Form1  {    /// summary    /// Required designer variable.    /// /summary    private System.ComponentModel.IContainer components = null;    /// summary    /// Clean up any resources being used.    /// /summary    /// param name="disposing"true if managed resources should be disposed; otherwise, false./param    protected override void Dispose(bool disposing)    {      if (disposing && (components != null))      {        components.Dispose();      }      base.Dispose(disposing);    }    #region Windows Form Designer generated code    /// summary    /// Required method for Designer support - do not modify    /// the contents of this method with the code editor.    /// /summary    private void InitializeComponent()    {      this.button1 = new System.Windows.Forms.Button();      this.listBox1 = new System.Windows.Forms.ListBox();      this.button2 = new System.Windows.Forms.Button();      this.SuspendLayout();      //      // button1      //      this.button1.Location = new System.Drawing.Point(12, 12);      this.button1.Name = "button1";      this.button1.Size = new System.Drawing.Size(130, 44);      this.button1.TabIndex = 0;      this.button1.Text = "开启服务";      this.button1.UseVisualStyleBackColor = true;      this.button1.Click += new System.EventHandler(this.button1_Click);      //      // listBox1      //      this.listBox1.FormattingEnabled = true;      this.listBox1.ItemHeight = 12;      this.listBox1.Location = new System.Drawing.Point(12, 62);      this.listBox1.Name = "listBox1";      this.listBox1.Size = new System.Drawing.Size(268, 196);      this.listBox1.TabIndex = 1;      //      // button2      //      this.button2.Location = new System.Drawing.Point(150, 12);      this.button2.Name = "button2";      this.button2.Size = new System.Drawing.Size(130, 44);      this.button2.TabIndex = 2;      this.button2.Text = "T人";      this.button2.UseVisualStyleBackColor = true;      this.button2.Click += new System.EventHandler(this.button2_Click);      //      // Form1      //      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;      this.ClientSize = new System.Drawing.Size(292, 273);      this.Controls.Add(this.button2);      this.Controls.Add(this.listBox1);      this.Controls.Add(this.button1);      this.Name = "Form1";      this.Text = "服务端";      this.ResumeLayout(false);      this.FormClosing+=new System.Windows.Forms.FormClosingEventHandler(Closing);    }    #endregion    private System.Windows.Forms.Button button1;    private System.Windows.Forms.ListBox listBox1;    private System.Windows.Forms.Button button2;  }}//Form1.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;using System.IO;using System.Net.Sockets;using System.Collections;namespace ServerTest{  public partial class Form1 : Form  {    int port = 2000;//绑定本机端口2000    TcpListener listener;    delegate void SetSafe(string text);    ArrayList conList=new ArrayList();    Encoding utf8 = Encoding.UTF8;    Encoding gb2312 = Encoding.GetEncoding("GB2312");    Thread listenerThread;    Thread DataThread;    public Form1()    {      InitializeComponent();    }    public void AcceptRequest() //连接处理线程    {      try      {        while (true)        {          if (listener.Pending())//是否有等待连接队列          {            Socket s = listener.AcceptSocket();//建立连接            conList.Add(s);//添加当前连接到已连接列表            if (s.Connected)            {              string ServerEndPoint = "欢迎登陆服务器" + s.LocalEndPoint.ToString() + "rn";              byte[] bgb = gb2312.GetBytes(ServerEndPoint);              byte[] butf = Encoding.Convert(gb2312, utf8, bgb);              s.Send(butf);//发送欢迎登陆信息              string mpoint = s.RemoteEndPoint.ToString();              AddList(mpoint);//把当前客户端IP,Port添加到ListBox显示            }          }        }      }      catch { }      Thread.Sleep(1000);    }    public void ReceiveData()//数据处理线程    {        while (true)        {          try          {            ArrayList CloseSocketList = new ArrayList();//关闭连接列表            CloseSocketList.Clear();//清空关闭连接列表            foreach (Socket s in conList)//循环每一个已建立的连接            {              NetworkStream ns = new NetworkStream(s);              StreamReader reader = new StreamReader(ns);              if (ns.DataAvailable)              {                string result = reader.ReadLine();                byte[] u = utf8.GetBytes(result);                byte[] gb = Encoding.Convert(utf8, gb2312, u);                string sGb = gb2312.GetString(gb);                if (sGb.StartsWith("Close"))//如果收到Close信息                {                  CloseSocketList.Add(s);//把当前连接添加到关闭连接列表                }                else                {                  foreach (Socket p in conList)//把从一个连接得到的信息发给所有的连接的客户端,就好比聊天室的聊天大厅,公共聊天                  {                    string sendString = result + "rn";                    byte[] butf = utf8.GetBytes(sendString);                    p.Send(butf);                  }                }              }              reader.Close();              ns.Close();            }            foreach (Socket s in CloseSocketList)//关闭关闭连接列表里面的连接            {              DelList(s.RemoteEndPoint.ToString());//从ListBox删除关闭的连接              s.Close();              conList.Remove(s);            }          }          catch { }          Thread.Sleep(1000);        }    }    private void button1_Click(object sender, EventArgs e)//启动服务器    {      listener = new TcpListener(port);//开启2000端口的监听      listener.Start();      listenerThread = new Thread(new ThreadStart(AcceptRequest));//启动连接处理线程      listenerThread.Start();      DataThread = new Thread(new ThreadStart(ReceiveData));//启动数据处理线程      DataThread.Start();    }    private void AddList(object text)    {      // InvokeRequired required compares the thread ID of the      // calling thread to the thread ID of the creating thread.      // If these threads are different, it returns true.      if (this.listBox1.InvokeRequired)      {        SetSafe d = new SetSafe(AddList);        this.Invoke(d, new object[] { text });      }      else      {        this.listBox1.Items.Add(text);      }    }    private void DelList(object text)    {      // InvokeRequired required compares the thread ID of the      // calling thread to the thread ID of the creating thread.      // If these threads are different, it returns true.      if (this.listBox1.InvokeRequired)      {        SetSafe d = new SetSafe(DelList);        this.Invoke(d, new object[] { text });      }      else      {        this.listBox1.Items.Remove(text);      }    }    private void button2_Click(object sender, EventArgs e)//踢指定的客户端连接    {      try      {        string temp = listBox1.SelectedItem.ToString();//选择要踢的客户端        DelList(temp);//ListBox删除它        foreach (Socket f in conList)        {          if ((f.RemoteEndPoint.ToString()) == temp)//找到那个要踢的客户端          {            string Message = "你被服务器T了" + "rn";            byte[] bgb = gb2312.GetBytes(Message);            byte[] butf = Encoding.Convert(gb2312, utf8, bgb);            f.Send(butf);//告诉他他被踢了            conList.Remove(f);//把它从已经连接列表中删除,断开连接            f.Close();          }        }      }      catch { }    }    private void Closing(object sender, EventArgs e)//窗体关闭,断开所有连接,关闭所有线程    {      try      {        foreach (Socket o in conList)        {          o.Close();        }        listenerThread.Abort();        DataThread.Abort();      }      catch { }    }  }}这个例子比较简单,实际在使用中会比较复杂,比如说我们的服务端要实现很多功能,举个网络游戏的例子,你要行走,要攻击,要加血,要回蓝,这每一个动作都对应一个命令,是以命令+内容+结束符的格式传送给服务器的,服务器在接收到信息,再对信息分类处理。下面我看一个这样的例子,是一个FTP的例子。客户端可以显示服务器的文件列表,可以选择要下载的文件,可以上传文件,服务器可以显示客户端的连接IP,可以断开指定的客户端。客户端//Form1.Designer.Csnamespace FTPClientTest{  partial class Form1  {    /// summary    /// Required designer variable.    /// /summary    private System.ComponentModel.IContainer components = null;    /// summary    /// Clean up any resources being used.    /// /summary    /// param name="disposing"true if managed resources should be disposed; otherwise, false./param    protected override void Dispose(bool disposing)    {      if (disposing && (components != null))      {        components.Dispose();      }      base.Dispose(disposing);    }    #region Windows Form Designer generated code    /// summary    /// Required method for Designer support - do not modify    /// the contents of this method with the code editor.    /// /summary    private void InitializeComponent()    {      this.listBox1 = new System.Windows.Forms.ListBox();      this.button1 = new System.Windows.Forms.Button();      this.button2 = new System.Windows.Forms.Button();      this.button3 = new System.Windows.Forms.Button();      this.button4 = new System.Windows.Forms.Button();      this.button5 = new System.Windows.Forms.Button();      this.SuspendLayout();      //      // listBox1      //      this.listBox1.FormattingEnabled = true;      this.listBox1.ItemHeight = 12;      this.listBox1.Location = new System.Drawing.Point(11, 9);      this.listBox1.Name = "listBox1";      this.listBox1.Size = new System.Drawing.Size(271, 160);      this.listBox1.TabIndex = 0;      //      // button1      //      this

来源:https://www.tulaoshi.com/n/20160219/1609005.html

延伸阅读
在2005年底微软公司正式发布了C# 2.0,与C# 1.x相比,新版本增加了很多新特性,其中最重要的是对泛型的支持。通过泛型,我们可以定义类型安全的数据结构,而无需使用实际的数据类型。这能显著提高性能并得到更高质量的代码。泛型并不是什么新鲜的东西,他在功能上类似于C++的模板,模板多年前就已存在C++上了,并且在C++上有大量成熟应用。...
  开发应用程序逻辑 1. 在Visual Studio 2005中打开My Documents文件夹下的\Microsoft Press\Visual CSharp Step by Step\Chapter 3\DailyRate子文件夹中的DailyRate项目。 2. 在“解决方案资源管理器”中,双击Program.cs文件,以便在“代码和文本编辑器”窗口...
    我感觉声音的播放比较简单。我们从播放声音开始。为什么我这么觉得?我也不知道。 这里是展示最最最最最简单的DirectX播放声音的例子,我尽量省略了无关的代码。最后的代码只有19行,够简单了吧? 准备工作: 1.安装了DirectX SDK(有9个DLL文件)。这里我们只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound...
像Java一样,C#提供了一整套相当丰富的类库、方法以及事件以供开发者使用。C#还引入了GDI+,它是由GDI演变而来的,具有比GDI更强大的功能而且简化了程序员的编程工作。所以开发者运用这些,就可以很方便的开发出具有强大图形图像功能的应用程序了。本文,笔者就通过一些实例像读者介绍一下C#中的图形编程的基本知识。 简单实例: ...
摘要:本文介绍了Excel对象、C#中的受管代码和非受管代码,并介绍了COM组件在.NET环境中的使用。 关键词:受管代码;非受管代码;Excel对象;动态连接库 引言 Excel是微软公司办公自动化套件中的一个软件,他主要是用来处理电子表格。Excel以其功能强大,界面友好等受到了许多用户的欢迎。在设计应用系统时,对于不同的用户...

经验教程

929

收藏

61
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部