欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#基于TCP協(xié)議的服務(wù)器端和客戶端通信編程的基礎(chǔ)教程

 更新時(shí)間:2016年04月25日 17:31:06   作者:劍蕭舞蝶  
這篇文章主要介紹了C#基于TCP協(xié)議的服務(wù)器端和客戶端通信編程的基礎(chǔ)教程,文中講解了C#中TCP編程主要相關(guān)的TcpListener類與TcpClient類用法,需要的朋友可以參考下

運(yùn)行在TCP之上常見的網(wǎng)絡(luò)應(yīng)用協(xié)議有比如HTTP、FTP、SMTP、POP3、IMAP。
TCP是TCP/IP體系中最重要的傳輸協(xié)議,它提供全雙工和可靠交付的服務(wù),是大多數(shù)應(yīng)用協(xié)議工作的基礎(chǔ)。
TCP是一種面向連接(連接導(dǎo)向)的,可靠的,基于字節(jié)流的傳輸層通信協(xié)議。

TCP的工作過程

  • 建立連接
  • 傳輸數(shù)據(jù)
  • 連接的終止


TCP的主要特點(diǎn)
1.TCP是面向連接的協(xié)議
2.是端到端的通信。每個(gè)TCP連接只能有兩個(gè)端點(diǎn),而且只能一對一通信,不能點(diǎn)對多的
的直接通信
3.高可靠性
4.全雙工方式傳輸
5.數(shù)據(jù)以字節(jié)流的方式傳輸
6.傳輸?shù)臄?shù)據(jù)無消息邊界

關(guān)于線程
利用TCP開發(fā)應(yīng)用程序時(shí),.net框架提供兩種工作方式,一種是同步工作方式,另一種是異步工作方式。
同步工作方式是指利用TCP編寫的程序執(zhí)行到監(jiān)聽或者接收語句,在未完成當(dāng)前工作前不再。
繼續(xù)往下執(zhí)行,線程處于阻塞狀態(tài),直到該語句完成后才能繼續(xù)執(zhí)行下一條語句。
異步工作方式是指程序執(zhí)行到監(jiān)聽或者接收語句時(shí),無論當(dāng)前工作是否完成,都會(huì)繼續(xù)往下執(zhí)行。

TcpListener與TcpClient類常用方法與屬性

TCPListener類用于監(jiān)聽客戶端連接請求,TCPClient類用于提供本地主機(jī)和遠(yuǎn)程主機(jī)的連接信息。
兩個(gè)類都位于 System.Net.Socckets命名空間下。

1.TCPListener類常用的方法:

(1)AcceptSocket:從端口處接收一個(gè)連接并賦予它Socket對象
(2)AcceptTcpClient:從端口處接收一個(gè)連接并賦予它TCPClient對象
(3)Equals:判斷兩個(gè)TcpListener對象是否相等
(4)GetType:獲取當(dāng)前實(shí)例的類型
(5)Pending :確定是否有掛起的連接請求
(6)Start:開始接聽傳入的連接請求
(7)Stop:關(guān)閉監(jiān)聽器
(8)ToString:創(chuàng)建TcpListener對象的字符串表示


2.TcpClient常用的屬性與方法
屬性:
(1)Client:獲取或設(shè)置基礎(chǔ)套接字
(2)LingerState:獲取或設(shè)置套接字保持連接的時(shí)間
(3)NoDelay:獲取或設(shè)置一個(gè)值,該值在發(fā)送或接收緩存沖未滿時(shí)禁止延遲、
(4)ReceiveBufferSize:獲取或設(shè)置TCP接收緩存區(qū)的大小
(5)ReceiveTimetut:獲取或設(shè)置套接字接收數(shù)據(jù)的超時(shí)時(shí)間
(6)SendBufferSize:獲取或設(shè)置TCP發(fā)送緩存區(qū)的大小
(7)SendTimeout:獲取或設(shè)置套接字發(fā)送數(shù)據(jù)超時(shí)時(shí)間

方法:
(1)Close:釋放TcpClient實(shí)例,而不關(guān)閉基礎(chǔ)連接
(2)Connect:用指定的主機(jī)名和端口號將客戶端連接到TCP主機(jī)
(3)BeginConnect:開始一個(gè)遠(yuǎn)程主機(jī)連接的異步請求
(4)GetStream:獲取能夠發(fā)送和接收數(shù)據(jù)的NetworkStream對象

TCP編程的一般步驟
1.網(wǎng)絡(luò)通信的最基本的前提就是客戶端要先和服務(wù)器建立TCP連接
2.服務(wù)端要不斷的監(jiān)聽客戶端是否有連接請求、并且服務(wù)端能要識別特定的客戶端
3.連接并創(chuàng)建對應(yīng)的套接字
4.發(fā)送數(shù)據(jù)和接收數(shù)據(jù)


編寫服務(wù)器端程序的一般步驟
1.創(chuàng)建一個(gè)TcpListener對象,然后調(diào)用該對象的Start方法在指定的端口進(jìn)行監(jiān)聽
2.在單獨(dú)的線程中,首先循環(huán)調(diào)用AcceptTcpClient方法接收客戶端的連接請求
,從該方法中的返回結(jié)果中得到與該客戶端對應(yīng)的TcpClient對象,并利用該對象
的GetStream方法得到NetworkStream。然后再利用該對象得到其他使用更方便的
對象,比如BinaryReader對象、BinaryWrite對象,為進(jìn)一步與對方通信做準(zhǔn)備。
3.每得到一個(gè)新的TcpClient對象,就創(chuàng)建一個(gè)與客戶對應(yīng)的線程,在線程與對應(yīng)的
客戶進(jìn)行通信。
4.根據(jù)傳送信息的情況確定是否關(guān)閉與客戶連接。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
using System.Net.Sockets; 
using System.Net; 
using System.IO; 
 
namespace TCP 
{ 
  public partial class TcpListenerTest : Form 
  { 
    public TcpListenerTest() 
    { 
      InitializeComponent(); 
    } 
    //聲明 
    private IPAddress localAddress;// IP地址 IPAddress類 在命名空間 using System.Net下 
    private const int port = 58080;//端口 
    private TcpListener tcpListener;//監(jiān)聽套接字 TcpLestener與TcpClient類 在命名空間 using System.Net.Sockets下 
    private TcpClient tcpClient;//服務(wù)端與客戶端建立連接 
    private NetworkStream newworkStream;//利用NetworkStream對象與遠(yuǎn)程主機(jī)發(fā)送數(shù)據(jù)或接收數(shù)據(jù)\ 
    private BinaryReader binaryReader;//讀取 BinaryReader與BinaryWriter類在命名空間using System.IO下 
    private BinaryWriter binaryWrite;//寫入 
    private void Form1_Load(object sender, EventArgs e) 
    { 
      IPAddress[] listenerIp = Dns.GetHostAddresses("www.baidu.com");//返回主機(jī)指定的IP地址 
      localAddress = listenerIp[0]; //初始化IP地址為本地地址 
      tcpListener = new TcpListener(localAddress,port);//創(chuàng)建TcpListener對象 
      tcpListener.Start();//開始監(jiān)聽 
 
      Thread thread = new Thread(AcceptClientConnect);//啟動(dòng)一個(gè)線程接收請求 
      thread.Start();//啟動(dòng)線程 
    } 
    //發(fā)起請求 
    private void AcceptClientConnect() 
    {  
      while(true) 
      { 
        try 
        { 
          tcpClient = tcpListener.AcceptTcpClient();//從端口接收一個(gè)連接,并賦予它TcpClient對象 
          if (tcpClient != null) 
          { 
            newworkStream = tcpClient.GetStream(); 
            binaryReader = new BinaryReader(newworkStream); 
            binaryWrite = new BinaryWriter(newworkStream); 
          } 
 
        } 
        catch 
        { 
          break; 
        } 
      } 
    } 
  } 
} 

TCP編程 客戶端程序一般步驟
1.利用TcpClient的構(gòu)造函數(shù)創(chuàng)建一個(gè)TcpClient對象
2.使用Connect方法與服務(wù)器建立連接
3.利用TcpClient對象的GetStream對象得到網(wǎng)絡(luò)流,然后利用該網(wǎng)絡(luò)流與服務(wù)器進(jìn)行數(shù)據(jù)傳輸
4.創(chuàng)建一個(gè)線程監(jiān)聽指定的端口,循環(huán)接收并處理服務(wù)器發(fā)送過來的信息。
5.完成工作之后,想服務(wù)器發(fā)送關(guān)閉信息,并關(guān)閉與服務(wù)器的連接。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net.Sockets; 
using System.Net; 
using System.IO; 
 
namespace TCP 
{ 
  public partial class TcpClientTest : Form 
  { 
    public TcpClientTest() 
    { 
      InitializeComponent(); 
    } 
    private TcpClient tcpClient;//聲明 
    private IPAddress iAdress;//IP地址 
    private const int port=58080; //端口  
    private NetworkStream networkStream;//網(wǎng)絡(luò)流 
    private BinaryReader binayRead;//讀取 
    private BinaryWriter binayWrite;//寫入 
    private void TcpClient_Load(object sender, EventArgs e) 
    { 
      IPAddress[] ipAddress = Dns.GetHostAddresses("www.baidu.com");//返回主機(jī)指定的IP地址 
      iAdress = ipAddress[0]; //初始化IP地址為本地地址 
      IPEndPoint ipoint = new IPEndPoint(iAdress,port);//將網(wǎng)絡(luò)端點(diǎn)表示為IP和端口號 
      tcpClient = new TcpClient(ipoint);//實(shí)例化TcpClient類 
      //最方便 TcpClient tcpClient=new TcpClient("www.baidu.com",port); 
 
      if (tcpClient != null) 
      { 
        networkStream = tcpClient.GetStream();//得到網(wǎng)絡(luò)流 
        binayRead = new BinaryReader(networkStream); 
        binayWrite = new BinaryWriter(networkStream); 
      } 
    } 
 
    private void ReMessage() 
    { 
      while (true) 
      { 
        try 
        { 
          string rcMsg = binayRead.ReadString();//從網(wǎng)絡(luò)流中讀取數(shù)據(jù) 
          if (rcMsg != null) 
          { 
            MessageBox.Show(rcMsg); 
          } 
          else 
          { 
            break; 
          } 
        } 
        catch 
        { 
  
        } 
      } 
    } 
  } 
} 

相關(guān)文章

最新評論