websocket服務端 c#示例**
usingsystem;
using
system.collections.generic;
using
system.linq;
using
system.text;
using
system.net.sockets;
using
system.net;
using
system.threading;
using
system.text.regularexpressions;
using
system.security.cryptography;
namespace
websocketdemo
catch
(exception e)
}//////
打包握手資訊
/// ///
///private
static
byte packhandshakedata(string
seckeyaccept)
//////
生成sec-websocket-accept
/// ///
客戶端握手資訊
///sec-websocket-accept
private
static
string getseckeyaccetp(byte handshakebytes, int
byteslength)
byte encryptionstring = sha1.create().computehash(encoding.ascii.getbytes(key + "
258eafa5-e914-47da-95ca-c5ab0dc85b11
"));
return
convert.tobase64string(encryptionstring);
}//////
解析客戶端資料報
/// ///
伺服器接收的資料報
///有效資料長度
///private
static
string analyticdata(byte recbytes, int
recbytelength)
bool fin = (recbytes[0] & 0x80) == 0x80; //
1bit,1表示最後一幀
if (!fin)
bool mask_flag = (recbytes[1] & 0x80) == 0x80; //
是否包含掩碼
if (!mask_flag)
int payload_len = recbytes[1] & 0x7f; //
資料長度
byte masks = new
byte[4
];
byte
payload_data;
if (payload_len == 126
)
else
if (payload_len == 127
)
uint64 len = bitconverter.touint64(uint64bytes, 0
); payload_data = new
byte
[len];
for (uint64 i = 0; i < len; i++)
}else
for (var i = 0; i < payload_len; i++)
return
encoding.utf8.getstring(payload_data);
}//////
打包伺服器資料
/// ///
資料 ///
資料報 private
static
byte packdata(string
message)
else
if (temp.length < 0xffff
)
else
return
contentbytes;
} }
}
html5 客戶端示例**
doctype html
>
<
html
>
<
head
>
<
meta
charset
="utf-8"
/>
<
title
>websockets客戶端示例
title
>
head
>
<
script
>
varwebsocket;
function
connect()
websocket.onmessage
=function
(msg)
websocket.onclose
=function
()
}catch
(exception)
}function
send()
try
catch
(exception)
document.getelementbyid(
"text
").value=""
;}function
disconnect()
script
>
<
body
>
<
h1>websocket客戶端示例
h1>
<
div
id="message"
>
div>
<
p>請輸入一些文字
p>
<
input
id="text"
type
="text"
>
<
button
id="connect"
onclick
="connect();"
>建立連線
button
>
<
button
id="send"
onclick
="send();"
>傳送資料
button
>
<
button
id="disconnect"
onclick
="disconnect();"
>斷開連線
button
>
body
>
html
>
/.net-demo
socket 模擬POST傳送資料
筆者之前使用過mfc的cinternetsession來模擬post傳送資料,但一直在想,mfc中的類都是經過封裝的,如果我自己通過最原始的socket傳送資料是不是可行呢?所以經過一 早上的努力,還是成功了,說明這也不難哈。我先說說自己的過程吧 首先要構造自己的http頭部,那麼首先要看標準的ht...
模擬傳送簡訊及步驟
1.傳送簡訊的主要 string mobile edittext findviewbyid r.id.mobile gettext tostring string content edittext findviewbyid r.id.content gettext tostring smsmanag...
curl模擬傳送post請求
curl模擬傳送post請求 初始化 curl curl init 設定抓取的url curl setopt curl,curlopt url,設定標頭檔案的資訊作為資料流輸出 curl setopt curl,curlopt header,1 設定獲取的資訊以檔案流的形式返回,而不是直接輸出。cu...