C#基於TCP協定的辦事器端和客戶端通訊編程的基本教程。本站提示廣大學習愛好者:(C#基於TCP協定的辦事器端和客戶端通訊編程的基本教程)文章只能為提供參考,不一定能成為您想要的結果。以下是C#基於TCP協定的辦事器端和客戶端通訊編程的基本教程正文
運轉在TCP之上罕見的收集運用協定有好比HTTP、FTP、SMTP、POP3、IMAP。
TCP是TCP/IP系統中最主要的傳輸協定,它供給全雙工和靠得住交付的辦事,是年夜多半運用協定任務的基本。
TCP是一種面向銜接(銜接導向)的,靠得住的,基於字撙節的傳輸層通訊協定。
TCP的任務進程
TCP的重要特色
1.TCP是面向銜接的協定
2.是端到真個通訊。每一個TCP銜接只能有兩個端點,並且只能一對一通訊,不克不及點對多的
的直接通訊
3.高靠得住性
4.全雙工方法傳輸
5.數據以字撙節的方法傳輸
6.傳輸的數據無新聞界限
關於線程
應用TCP開辟運用法式時,.net框架供給兩種任務方法,一種是同步任務方法,另外一種是異步任務方法。
同步任務方法是指應用TCP編寫的法式履行到監聽或許吸收語句,在未完成以後任務前不再。
持續往下履行,線程處於壅塞狀況,直到該語句完成後能力持續履行下一條語句。
異步任務方法是指法式履行到監聽或許吸收語句時,不管以後任務能否完成,都邑持續往下履行。
TcpListener與TcpClient類經常使用辦法與屬性
TCPListener類用於監聽客戶端銜接要求,TCPClient類用於供給當地主機和長途主機的銜接信息。
兩個類都位於 System.Net.Socckets定名空間下。
1.TCPListener類經常使用的辦法:
(1)AcceptSocket:從端口處吸收一個銜接並付與它Socket對象
(2)AcceptTcpClient:從端口處吸收一個銜接並付與它TCPClient對象
(3)Equals:斷定兩個TcpListener對象能否相等
(4)GetType:獲得以後實例的類型
(5)Pending :肯定能否有掛起的銜接要求
(6)Start:開端接聽傳入的銜接要求
(7)Stop:封閉監聽器
(8)ToString:創立TcpListener對象的字符串表現
2.TcpClient經常使用的屬性與辦法
屬性:
(1)Client:獲得或設置基本套接字
(2)LingerState:獲得或設置套接字堅持銜接的時光
(3)NoDelay:獲得或設置一個值,該值在發送或吸收緩存沖未滿時制止延遲、
(4)ReceiveBufferSize:獲得或設置TCP吸收緩存區的年夜小
(5)ReceiveTimetut:獲得或設置套接字吸收數據的超不時間
(6)SendBufferSize:獲得或設置TCP發送緩存區的年夜小
(7)SendTimeout:獲得或設置套接字發送數據超不時間
辦法:
(1)Close:釋放TcpClient實例,而不封閉基本銜接
(2)Connect:用指定的主機名和端標語將客戶端銜接到TCP主機
(3)BeginConnect:開端一個長途主機銜接的異步要求
(4)GetStream:獲得可以或許發送和吸收數據的NetworkStream對象
TCP編程的普通步調
1.收集通訊的最根本的條件就是客戶端要先和辦事器樹立TCP銜接
2.辦事端要赓續的監聽客戶端能否有銜接要求、而且辦事端能要辨認特定的客戶端
3.銜接並創立對應的套接字
4.發送數據和吸收數據
編寫辦事器端法式的普通步調
1.創立一個TcpListener對象,然後挪用該對象的Start辦法在指定的端口停止監聽
2.在零丁的線程中,起首輪回挪用AcceptTcpClient辦法吸收客戶真個銜接要求
,從該辦法中的前往成果中獲得與該客戶端對應的TcpClient對象,並應用該對象
的GetStream辦法獲得NetworkStream。然後再應用該對象獲得其他應用更便利的
對象,好比BinaryReader對象、BinaryWrite對象,為進一步與對方通訊做預備。
3.每獲得一個新的TcpClient對象,就創立一個與客戶對應的線程,在線程與對應的
客戶停止通訊。
4.依據傳送信息的情形肯定能否封閉與客戶銜接。
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;//監聽套接字 TcpLestener與TcpClient類 在定名空間 using System.Net.Sockets下
private TcpClient tcpClient;//辦事端與客戶端樹立銜接
private NetworkStream newworkStream;//應用NetworkStream對象與長途主機發送數據或吸收數據\
private BinaryReader binaryReader;//讀取 BinaryReader與BinaryWriter類在定名空間using System.IO下
private BinaryWriter binaryWrite;//寫入
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] listenerIp = Dns.GetHostAddresses("www.百度.com");//前往主機指定的IP地址
localAddress = listenerIp[0]; //初始化IP地址為當地地址
tcpListener = new TcpListener(localAddress,port);//創立TcpListener對象
tcpListener.Start();//開端監聽
Thread thread = new Thread(AcceptClientConnect);//啟動一個線程吸收要求
thread.Start();//啟動線程
}
//提議要求
private void AcceptClientConnect()
{
while(true)
{
try
{
tcpClient = tcpListener.AcceptTcpClient();//從端口吸收一個銜接,並付與它TcpClient對象
if (tcpClient != null)
{
newworkStream = tcpClient.GetStream();
binaryReader = new BinaryReader(newworkStream);
binaryWrite = new BinaryWriter(newworkStream);
}
}
catch
{
break;
}
}
}
}
}
TCP編程 客戶端法式普通步調
1.應用TcpClient的結構函數創立一個TcpClient對象
2.應用Connect辦法與辦事器樹立銜接
3.應用TcpClient對象的GetStream對象獲得收集流,然後應用該收集流與辦事器停止數據傳輸
4.創立一個線程監聽指定的端口,輪回吸收並處置辦事器發送過去的信息。
5.完成任務以後,想辦事器發送封閉信息,並封閉與辦事器的銜接。
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;//收集流
private BinaryReader binayRead;//讀取
private BinaryWriter binayWrite;//寫入
private void TcpClient_Load(object sender, EventArgs e)
{
IPAddress[] ipAddress = Dns.GetHostAddresses("www.百度.com");//前往主機指定的IP地址
iAdress = ipAddress[0]; //初始化IP地址為當地地址
IPEndPoint ipoint = new IPEndPoint(iAdress,port);//將收集端點表現為IP和端標語
tcpClient = new TcpClient(ipoint);//實例化TcpClient類
//最便利 TcpClient tcpClient=new TcpClient("www.百度.com",port);
if (tcpClient != null)
{
networkStream = tcpClient.GetStream();//獲得收集流
binayRead = new BinaryReader(networkStream);
binayWrite = new BinaryWriter(networkStream);
}
}
private void ReMessage()
{
while (true)
{
try
{
string rcMsg = binayRead.ReadString();//從收集流中讀取數據
if (rcMsg != null)
{
MessageBox.Show(rcMsg);
}
else
{
break;
}
}
catch
{
}
}
}
}
}