国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

使用C#開發Socket通訊的方法

2020-01-24 03:49:02
字體:
來源:轉載
供稿:網友
下面的示例顯示如何使用 Socket 類向 HTTP 服務器發送數據和接收響應。 

[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); 

//如果想立即關閉連接則調用 s.Close(); 
return strRetPage; 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南康市| 和硕县| 焦作市| 吉首市| 林芝县| 邯郸市| 云霄县| 津南区| 潍坊市| 习水县| 兴和县| 陈巴尔虎旗| 蓬莱市| 美姑县| 漾濞| 温州市| 丽江市| 仲巴县| 墨江| 都江堰市| 黑龙江省| 弋阳县| 大方县| 武川县| 大埔区| 开阳县| 凤冈县| 曲沃县| 凤山县| 武功县| 溧阳市| 西平县| 弋阳县| 上犹县| 石楼县| 凤翔县| 正阳县| 淅川县| 新宾| 文水县| 沧州市|