国产探花免费观看_亚洲丰满少妇自慰呻吟_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; 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗定市| 墨竹工卡县| 吉水县| 牟定县| 西乌| 白水县| 德江县| 大名县| 红安县| 纳雍县| 阿拉善左旗| 白玉县| 盐池县| 吉安市| 西华县| 休宁县| 吉木乃县| 潼南县| 保靖县| 土默特左旗| 琼结县| 霞浦县| 鄄城县| 乐业县| 霍城县| 泗洪县| 景东| 肇州县| 永寿县| 抚宁县| 尉氏县| 夏邑县| 枝江市| 团风县| 定远县| 淮滨县| 大渡口区| 巢湖市| 新宁县| 自贡市| 南郑县|