使用C#開發(fā)Socket通訊的方法
更新時(shí)間:2007年04月16日 00:00:00 作者:
下面的示例顯示如何使用 Socket 類向 HTTP 服務(wù)器發(fā)送數(shù)據(jù)和接收響應(yīng)。
[C#]
public string DoSocketGet(string server)
{
//Sets up variables and a string to write to the server
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;
// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get the first IPAddress in the list using DNS.
IPAddress hostadd = Dns.Resolve(server).AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
//Creates the Socket for sending data over TCP.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
// Connects to the host using IPEndPoint.
s.Connect(EPhost);
if (!s.Connected)
{
strRetPage = "Unable to connect to host";
return strRetPage;
}
// Sends the GET text to the host.
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
// Receives the page, looping until all bytes are received
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
//如果想立即關(guān)閉連接則調(diào)用 s.Close();
return strRetPage;
}
[C#]
public string DoSocketGet(string server)
{
//Sets up variables and a string to write to the server
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;
// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get the first IPAddress in the list using DNS.
IPAddress hostadd = Dns.Resolve(server).AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
//Creates the Socket for sending data over TCP.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
// Connects to the host using IPEndPoint.
s.Connect(EPhost);
if (!s.Connected)
{
strRetPage = "Unable to connect to host";
return strRetPage;
}
// Sends the GET text to the host.
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
// Receives the page, looping until all bytes are received
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
//如果想立即關(guān)閉連接則調(diào)用 s.Close();
return strRetPage;
}
相關(guān)文章
C#復(fù)合模式(Composite Pattern)實(shí)例教程
這篇文章主要介紹了C#復(fù)合模式(Composite Pattern),以實(shí)例形式講述了復(fù)合模式在樹形結(jié)構(gòu)中的應(yīng)用,需要的朋友可以參考下2014-09-09
使用 BenchmarkDotNet 對 C# 代碼進(jìn)行基準(zhǔn)測試
這篇文章主要介紹了使用 BenchmarkDotNet 對 C# 代碼進(jìn)行基準(zhǔn)測試,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#實(shí)現(xiàn)單鏈表(線性表)完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)單鏈表(線性表)的方法,結(jié)合完整實(shí)例形式分析了單鏈表的原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06
Unity使用DoTween實(shí)現(xiàn)拋物線效果
這篇文章主要為大家詳細(xì)介紹了Unity使用DoTween實(shí)現(xiàn)拋物線效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)??吹接型瑢W(xué)在討論Framework 4 的新特性,新方法,于是想寫個(gè)程序找出framework4.0中新增的方法和類2013-05-05
淺析C# web訪問mysql數(shù)據(jù)庫-整理歸納總結(jié)
本篇文章是對C#中的web訪問mysql數(shù)據(jù)庫的一些知識點(diǎn)進(jìn)行了整理歸納總結(jié),需要的朋友可以參考下2013-07-07

