C#經過屬性稱號獲取(讀取)屬性值的辦法。本站提示廣大學習愛好者:(C#經過屬性稱號獲取(讀取)屬性值的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#經過屬性稱號獲取(讀取)屬性值的辦法正文
看到這個標題,是不是很眼生?在博客園裡搜下,保證會發現關於這個東東的文章真實是太多了~~~真得是沒有寫得必要,而且我也有點懶得去揣摩字句。(看到這,一定得來個轉機的了,不然就看不到下文了,不是嗎)但是,為了自己下一篇要寫的文章做參考,還是有必要先補充一下socket根底知識。
留意:假如你曾經接觸過socket,那就沒什麼必要耽擱時間看下去了。另外,假如發現其中任何錯誤,歡送直接指出。
1.按常規先來引見下socket
Windows中的很多東西都是從Unix范疇自創過去的,Socket也是一樣。在Unix中,socket代表了一種文件描繪符(在Unix中一切都是以文件為單位),而這裡這個描繪符則是用於描繪網絡訪問的。什麼意思呢?就是順序員可以經過socket來發送和接納網絡上的數據。你也可以了解成是一個API。有了它,你就不必直接去操作網卡了,而是經過這個接口,這樣就省了很多復雜的操作。
在C#中,MS為我們提供了 System.Net.Sockets 命名空間,外面包括了Socket類。
2.有了socket,那就可以用它來訪問網絡了
不過你不要快樂得太早,要想訪問網絡,還得有些根本的條件(和編程有關的我就不提了):a. 要確定本機的IP和端口,socket只要與某一IP和端口綁定,才干發揚弱小的威力。b. 得有協議吧(否則誰認得你這發送到網絡的是什麼呀)。想要復雜的,我們可以自己來定協議。但是這個就不在這篇裡提了,我這裡引見兩種大家最熟習不過的協議:TCP & UDP。(別說你不知道,不然...不然...我不通知你)
假如具有了根本的條件,就可以開端用它們訪問網絡了。來看看步驟吧:
a. 樹立一個套接字
b. 綁定本機的IP和端口
c. 假如是TCP,由於是面向銜接的,所以要應用ListenO()辦法來監聽網絡上能否有人給自己發東西;假如是UDP,由於是無銜接的,所以來者不拒。
d. TCP狀況下,假如監聽到一個銜接,就可以運用accept來接納這個銜接,然後就可以應用Send/Receive來執行操作了。而UDP,則不需求accept, 直接運用SendTo/ReceiveFrom來執行操作。(看清楚哦,和TCP的執行辦法有區別,由於UDP不需求樹立銜接,所以在發送前並不知道對方的IP和端口,因而需求指定一個發送的節點才干停止正常的發送和接納)
e. 假如你不想持續發送和接納了,就不要糜費資源了。能close的就close吧。
假如看了下面文字,你還不清楚的話,就來看看圖好了:

面向銜接的套接字零碎調用時序

無銜接的套接字零碎調用時序
3.開端入手敲~~代碼(復雜的代碼)
首先我們來寫個面向銜接的
TCPServer
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcpserver
{
/// <summary>
/// Class1 的摘要闡明。
/// </summary>
class server
{
/// <summary>
/// 使用順序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此處添加代碼以啟動使用順序
//
int recv;//用於表示客戶端發送的信息長度
byte[] data=new byte[1024];//用於緩存客戶端所發送的信息,經過socket傳遞的信息必需為字節數組
IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050);//本機預運用的IP和端口
Socket newsock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
newsock.Bind(ipep);//綁定
newsock.Listen(10);//監聽
Console.WriteLine("waiting for a client");
Socket client=newsock.Accept();//當有可用的客戶端銜接嘗試時執行,並前往一個新的socket,用於與客戶端之間的通訊
IPEndPoint clientip=(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("connect with client:"+clientip.Address+" at port:"+clientip.Port);
string welcome="welcome here!";
data=Encoding.ASCII.GetBytes(welcome);
client.Send(data,data.Length,SocketFlags.None);//發送信息
while(true)
{//用死循環來不時的從客戶端獲取信息
data=new byte[1024];
recv=client.Receive(data);
Console.WriteLine("recv="+recv);
if (recv==0)//當信息長度為0,闡明客戶端銜接斷開
break;
Console.WriteLine(Encoding.ASCII.GetString(data,0,recv));
client.Send(data,recv,SocketFlags.None);
}
Console.WriteLine("Disconnected from"+clientip.Address);
client.Close();
newsock.Close();
}
}
}
TCPClient
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcpclient
{
/// <summary>
/// Class1 的摘要闡明。
/// </summary>
class client
{
/// <summary>
/// 使用順序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此處添加代碼以啟動使用順序
//
byte[] data=new byte[1024];
Socket newclient=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
Console.Write("please input the server ip:");
string ipadd=Console.ReadLine();
Console.WriteLine();
Console.Write("please input the server port:");
int port=Convert.ToInt32(Console.ReadLine());
IPEndPoint ie=new IPEndPoint(IPAddress.Parse(ipadd),port);//服務器的IP和端口
try
{
//由於客戶端只是用來向特定的服務器發送信息,所以不需求綁定本機的IP和端口。不需求監聽。
newclient.Connect(ie);
}
catch(SocketException e)
{
Console.WriteLine("unable to connect to server");
Console.WriteLine(e.ToString());
return;
}
int recv = newclient.Receive(data);
string stringdata=Encoding.ASCII.GetString(data,0,recv);
Console.WriteLine(stringdata);
while(true)
{
string input=Console.ReadLine();
if(input=="exit")
break;
newclient.Send(Encoding.ASCII.GetBytes(input));
data=new byte[1024];
recv=newclient.Receive(data);
stringdata=Encoding.ASCII.GetString(data,0,recv);
Console.WriteLine(stringdata);
}
Console.WriteLine("disconnect from sercer");
newclient.Shutdown(SocketShutdown.Both);
newclient.Close();
}
}
}
上面在給出無銜接的(真實是太懶了,上面這個是直接復制他人的)
UDPServer
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SimpleUdpSrvr
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);//定義一網絡端點
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//定義一個Socket
newsock.Bind(ipep);//Socket與本地的一個終結點相關聯
Console.WriteLine("Waiting for a client..");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//定義要發送的計算機的地址
EndPoint Remote = (EndPoint)(sender);//
recv = newsock.ReceiveFrom(data, ref Remote);//承受數據
Console.WriteLine("Message received from{0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetBytes(data,0,recv));
string welcome = "Welcome to my test server!";
data = Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data = new byte[1024];
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}
}
}
UDPClient
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SimpleUdpClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];//定義一個數組用來做數據的緩沖區
string input, stringData;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello,are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);//將數據發送到指定的終結點
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);//承受來自服務器的數據
Console.WriteLine("Message received from{0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true)//讀取數據
{
input = Console.ReadLine();//從鍵盤讀取數據
if (input == "text")//完畢標志
{
break;
}
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);//將數據發送到指定的終結點Remote
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);//從Remote承受數據
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("Stopping client");
server.Close();
}
}
}
下面的示例只是復雜的使用了socket來完成通訊,你也可以完成異步socket、IP組播 等等。
MS還為我們提供了幾個助手類:TcpClient類、TcpListener類、UDPClient類。這幾個類簡化了一些操作,所以你也可以應用這幾類來寫下面的代碼,但我團體還是比擬習氣直接用socket來寫。
既然快寫完了,那我就再多啰嗦幾句。在需求即時呼應的軟件中,我團體更傾向運用UDP來完成通訊,由於相比TCP來說,UDP占用更少的資源,且呼應速度快,延時低。至於UDP的牢靠性,則可以經過在使用層加以控制來滿足。當然假如牢靠性要求高的環境下,還是建議運用TCP。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。