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

首頁 > 編程 > .NET > 正文

[ASP.NET]使用C#開發Socket通訊

2024-07-10 13:00:16
字體:
來源:轉載
供稿:網友
下面的示例顯示如何使用 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;
}

國內最大的酷站演示中心!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铅山县| 高尔夫| 宁海县| 都兰县| 高邑县| 广安市| 凌源市| 甘谷县| 唐山市| 高雄县| 萝北县| 邳州市| 洮南市| 尉犁县| 孟村| 象州县| 巫山县| 顺昌县| 新乡市| 秭归县| 汪清县| 美姑县| 汉川市| 裕民县| 江都市| 兴山县| 林州市| 景德镇市| 光山县| 夏邑县| 安康市| 丹寨县| 边坝县| 都江堰市| 青川县| 额尔古纳市| 平顶山市| 长岭县| 天津市| 阆中市| 大方县|