国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
}

國內最大的酷站演示中心!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 唐河县| 宿松县| 大厂| 盈江县| 桂林市| 雅安市| 石嘴山市| 启东市| 黑水县| 宁德市| 宜黄县| 哈尔滨市| 惠来县| 乡城县| 抚顺市| 二手房| 合水县| 会泽县| 秦皇岛市| 抚宁县| 华安县| 辛集市| 蓝山县| 宁国市| 元谋县| 长阳| 凉城县| 慈溪市| 龙泉市| 慈利县| 阜城县| 柳林县| 乐昌市| 聂拉木县| 逊克县| 拉孜县| 阿克苏市| 天祝| 大丰市| 海口市| 望江县|