资源简介
WebSocket服务器端+Html5客户端,服务器端dotnet framework2.0,3.5,4.0,4.5都支持。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text.Regularexpressions;
using System.Security.Cryptography;
namespace WebSocket
{
class Program
{
static void Main(string[] args)
{
int port = 10;
byte[] buffer = new byte[1024];
IPEndPoint localEP = new IPEndPoint(IPAddress.Any port);
Socket listener = new Socket(localEP.Address.AddressFamily SocketType.Stream ProtocolType.Tcp);
try
{
listener.Bind(localEP);
listener.Listen(10);
Console.WriteLine(“等待客户端连接....“);
Socket sc = listener.Accept();//接受一个连接
Console.WriteLine(“接受到了客户端:“ + sc.RemoteEndPoint.ToString() + “连接....“);
//握手
int length = sc.Receive(buffer);//接受客户端握手信息
sc.Send(PackHandShakeData(GetSecKeyAccetp(buffer length)));
Console.WriteLine(“已经发送握手协议了....“);
//接受客户端数据
Console.WriteLine(“等待客户端数据....“);
length = sc.Receive(buffer);//接受客户端信息
string clientMsg = AnalyticData(buffer length);
Console.WriteLine(“接受到客户端数据:“ + clientMsg);
//发送数据
int num = 0;
while (true)
{
num++;
string sendMsg = num.ToString()+“您好,“ + clientMsg;
Console.WriteLine(“发送数据:““ + sendMsg + “” 至客户端....“);
sc.Send(PackData(sendMsg));
System.Threading.Thread.Sleep(3000);
}
//Console.WriteLine(“演示Over!“);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
///
/// 打包握手信息
///
///
///
private static byte[] PackHandShakeData(string secKeyAccept)
{
var responseBuilder = new StringBuilder();
responseBuilder.Append(“HTTP/1.1 101 Switching Protocols“ + Environment.NewLine);
responseBuilder.Append(“Upgrade: websocket“ + Environment.NewLine);
responseBuilder.Append(“Connection: Upgrade“ + Environment.NewLine);
responseBuilder.Append(“Sec-WebSocket-Accept: “ + secKeyAccept + Environment.NewLine + Environment.NewLine);
//如果把上一行换成下面两行,才是thewebsocketprotocol-17协议,但居然握手不成功,目前仍没弄明白!
//responseBuilder.Append(“Sec-WebSocket-Accept: “ + secKeyAccept + Environment.NewLine);
//responseBuilder.Append(“Sec-WebSocket-Protocol: chat“ + Environment.NewLine);
return Encoding.UTF8.GetBytes(responseBuilder.ToString());
}
///
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 120 2018-10-27 16:04 WebSocket\WebSocket\app.config
文件 7680 2018-10-27 17:16 WebSocket\WebSocket\bin\Debug\WebSocket.exe
文件 120 2018-10-27 16:04 WebSocket\WebSocket\bin\Debug\WebSocket.exe.config
文件 15872 2018-10-27 17:16 WebSocket\WebSocket\bin\Debug\WebSocket.pdb
文件 14328 2018-10-27 17:22 WebSocket\WebSocket\bin\Debug\WebSocket.vshost.exe
文件 120 2018-10-27 16:04 WebSocket\WebSocket\bin\Debug\WebSocket.vshost.exe.config
文件 490 2009-06-11 05:14 WebSocket\WebSocket\bin\Debug\WebSocket.vshost.exe.manifest
文件 2004 2018-10-27 16:11 WebSocket\WebSocket\bin\Debug\WebSocketClient.html
文件 312 2018-10-27 17:22 WebSocket\WebSocket\obj\Debug\WebSocket.csproj.FileListAbsolute.txt
文件 7680 2018-10-27 17:16 WebSocket\WebSocket\obj\Debug\WebSocket.exe
文件 15872 2018-10-27 17:16 WebSocket\WebSocket\obj\Debug\WebSocket.pdb
文件 7736 2018-10-27 17:16 WebSocket\WebSocket\Program.cs
文件 1368 2018-10-27 16:03 WebSocket\WebSocket\Properties\AssemblyInfo.cs
文件 2237 2018-10-27 16:04 WebSocket\WebSocket\WebSocket.csproj
文件 917 2018-10-27 16:03 WebSocket\WebSocket.sln
..A..H. 17920 2018-10-27 17:26 WebSocket\WebSocket.suo
目录 0 2018-10-27 16:03 WebSocket\WebSocket\obj\Debug\TempPE
目录 0 2018-10-27 17:26 WebSocket\WebSocket\bin\Debug
目录 0 2018-10-27 17:26 WebSocket\WebSocket\obj\Debug
目录 0 2018-10-27 17:26 WebSocket\WebSocket\bin
目录 0 2018-10-27 17:26 WebSocket\WebSocket\obj
目录 0 2018-10-27 17:26 WebSocket\WebSocket\Properties
目录 0 2018-10-27 17:26 WebSocket\WebSocket
目录 0 2018-10-27 17:26 WebSocket
----------- --------- ---------- ----- ----
94776 24
- 上一篇:html爱心页面
- 下一篇:html最简单的增删改查实现方式
相关资源
- websocket发送裸H264到Html5解码
- 调用电子口岸IC卡/UKEY控件WebSocket方式
- libwebsocket的API说明文档
- libwebsockets win7 vs2017 编译工程
- websocket 聊天室146392
- Signalr即时通讯.Net MVC Demo
- HTML5 WebSocket权威指南(带标签)
- nodejs html5 websocket画笔 煎蛋在线画布
- WebSocket服务器的.NET实现 SuperWebSocket的
- springboot-websocket实战案例
- fleck开源 HTML5 Websocket 服务器解析
- Node.js+ Websocket实现 Web 聊天
- ESP8266-Websocket 用于 ESP 8266的web socket库
评论
共有 条评论